4

I am brand new to jquery and trying to modify a basic script from php academy.

the jquery script is:

<script type="text/javascript">
function get() {
    $.post('getpeopleinjobs.php', {
        postvarfname: form.firstname.value,
        postvarlname: form.d = surname.value
    }, function(output) {
        $('#age').html(output).show();
    });
}
</script>

and my form code is:

    <form name="form">
    <input type="text" id="firstname">
    <input type="text" id="surname">
    <input type="button" value="get" onclick="get();">
    </form>

This worked perfectly when I was passing just one variable and my code snippet was:

$.post('getpeopleinjobs.php', {
    postvarfname: form.firstname.value
}

I then tried to add a second variable with

$.post('getpeopleinjobs.php', {
    postvarfname: form.firstname.value,
    postvarlname: form.d = surname.value
}

It does not work now in Internet Explorer but interestingly enough it does work in Chrome?

Any advice for the beginner?=

1
  • 2
    Are you sure you mean to have that d= sitting in the second value? So, it should read $.post ('getpeopleinjobs.php',{postvarfname: form.firstname.value,postvarlname: form.surname.value} Commented Jul 11, 2012 at 16:26

3 Answers 3

8

You have the concept down correctly but there appears to be a minor mistake

//Posted Code
$.post ('getpeopleinjobs.php',{postvarfname: form.firstname.value,postvarlname: form.d=surname.value}
// Fixed Code
$.post ('getpeopleinjobs.php',{postvarfname: form.firstname.value, postvarlname: form.surname.value}
Sign up to request clarification or add additional context in comments.

Comments

5

You have a typo in your post code:

$.post('getpeopleinjobs.php',{
    postvarfname: form.firstname.value,
    postvarlname: form.surname.value
}, function () { /* ... */ });

Should work fine.

Comments

5

It looks like you have an issue in your script. Remove the = sign:

$.post ('getpeopleinjobs.php',{postvarfname: form.firstname.value,postvarlname: form.surname.value});

You may also simply want to write the script as:

$.post ('getpeopleinjobs.php',$(form).serialize());

This way if you add additional fields to the form you don't have to modify the script.

2 Comments

Hi John, can you elaborate on the serialze function? will this send all form data to the next page? Thanks
Yeah, the serialize function serializes all children input elements into the url-encoded text string api.jquery.com/serialize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.