1

need your help for this ... My homepage have 3 divs, #Header, #Content, #Footer. All the other pages are being opened inside the #Content div. In one of those pages I have a form with two select lists and one submit button. Just want to click the button and then return another page into the #Content div, showing the values that I select before. Like this:

The origin is: 1
The destiny is: 1

But this code returns the following ...

Notice: Undefined variable: origin in ...
Notice: Undefined variable: destiny in ...

Note: This is working if I don't open the page inside the #Content div

my Html:

<form id="myform" name="myform" action="values.php" method="POST">
    <select id="origin" name="origin">
        <option value="0" selected>-- Select Origin --</option>
        <option value="1">Portugal</option></select>
    <select id="destiny" name="destiny">
        <option value="0" selected>-- Select Destiny --</option>
        <option value="1">Lisboa</option></select>
    <input id="btSubmit" name="btSubmit" type="submit" value="search!">
</form>

my Function:

$(document).ready(function(){
    $('#btSubmit').click(function(e) {
        e.preventDefault();
        var url = $('#myform').attr('action');
        var method = $('#myform').attr('method');
    $.ajax({
        type: method,
        url: url,
        data: $('#myform').serialize(),
        success: $('#content').load(url)
        });
    });
});

my values.php page:

<?php
    if(isset($_POST['origin']) || isset($_POST['destiny'])) 
    {
        $origin = $_POST['origin'];
        $destiny = $_POST['destiny'];
    }
    echo 'The origin is:' . $origin . '<br>';
    echo 'The destiny is:' . $destiny;
?>
0

3 Answers 3

3

You should not call load again - you have already called it essentially with $.ajax and received the results. So you need just display them in the content:

success: function (data) {
    $('#content').html(data);
}
0
0

You should use success callback function correctly. Accept response in callback method and set it in your div

success: function (data) {
    $('#content').html(data);
}

Additionally, You should perform your operation with form submit event.

$('form#myform').on('submit', function (e) {

instead of

$('#btSubmit').click(function(e) {
1
  • thanks for your help. Regarding the change to the Submit button, it doesn't work ... When I press the button, nothing happens, not eaven a message error ... Commented Jan 31, 2014 at 16:03
0

As Andrei mentioned you have to use

success: function (data) {
    $('#content').html(data);
}

because calling success: $('#content').load(url) triggers a new GET request. When GET request reaches php code $_POST is not set and your variables are not initialized so you get the message from php:

Notice: Undefined variable: origin in 

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.