Reputation: 43
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;
?>
Upvotes: 1
Views: 1901
Reputation: 3580
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
Upvotes: 0
Reputation: 133403
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) {
Upvotes: 0
Reputation: 56688
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);
}
Upvotes: 3