I'm a beginner so tell me if i'm doing something fundamentally wrong. I am trying to post data with ajax. The post itself works, but it is not passing any data.
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$.ajax({
type: 'POST',
url: 'processwifi.php',
data:{'postid': postid},
success: function(response){
alert('it works');
}
});
});
});
</script>
And the processwifi.php
<?php
$id = isset($_POST["postid"]);
$link = new mysqli("127.0.0.1","***","***","secretariaat");
if (!$link) {
echo "Error: Unable to connect to MySQL." . PHP_EOL;
echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
exit;
}
$sql = "UPDATE wifi SET gebruikt='1' WHERE wifi.id='$id'";
if ($link->query($sql) === TRUE) {
echo "Update of record is '$id' successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
mysqli_close($link);
//Redirect result page
//header('HTTP/1.1 301 Moved Permanently');
//header('Location: wificode.php');
?>
Could anyone help me ?
Many thanks in advance.
You need to define the variable postid
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
var postid = 10; // define postid here
$.ajax({
type: 'POST',
url: 'processwifi.php',
data:{'postid': postid},
success: function(response){
alert('it works');
}
});
});
});
</script>
The problem is that you have not yet stated what postid
is supposed to indicate. You're trying to obtain a value from a variable / field / property
which is never declared and set.
In the following example I've set the value of postid
to 1
. https://jsfiddle.net/n5rc6u3g/
When trying to debug the "Network" tab, you'll notice the sent Form Data indicates postid
as 1
.
Also, there is a fundamental mistake in your backend code. You're trying to obtain the postid
using the method isset($_POST["postid"])
The signature from the isset
method is:
bool isset ( mixed $var [, mixed $... ] )
Meaning it returns a boolean
, being either true
or false
. Which in turn indicated whether the value is set and not null. http://php.net/manual/en/function.isset.php