I really need your help guys. I am not able to access post data in php from ajax. I have no idea what's going on here. I tried searching for solutions in stackoverflow but none of them helped me. Here is my script,..
<script>
$('form.ajax').on('submit',function() {
var that = $(this),
url = that.attr('action'),
type = that.attr('method'),
data = {};
that.find('[name]').each(function(index, value) {
var that = $(this),
name = that.attr('name'),
value = that.val();
data[name] = value;
console.log(data.name);
});
$.ajax({
url: url,
type: type,
data: data,
success: function(response) {
}
});
return false;
});
</script>
here is my php code,
<?php
$con=mysqli_connect("localhost","username","password","databasename");
if(mysqli_connect_errno())
{
echo "failed to connect to MySQL:"+mysqli_connect_error();
die();
}
$myQuery = "SELECT * FROM Invoice";
$result1 = mysqli_query($con,$myQuery);
if(isset($_POST['name'])){
$name = $_POST['name'];
$query = "SELECT * FROM Invoice WHERE InvoiceNumber LIKE '%".$name."'";
$result1 = mysqli_query($con,$query);
}
$data1 = array();
while($row = mysqli_fetch_array($result1)) {
$row_data = array(
'InvoiceNumber' => $row['InvoiceNumber'],
'InvoicePONumber' => $row['InvoicePONumber']
);
array_push($data1,$row_data);
}
echo json_encode($data1);
?>
The default request method for $.ajax
is GET, so you will never get any results in $_POST
var. instead you should use $.post
or add the request type to your ajax
call.
$.ajax({
...
type: 'POST'
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.