I am trying to get the information from my ajax into a PHP $_POST so that i can update my database.
HTML
<form id="23" method="post">
<select name="admin" onchange="chosenadmin(23)">
<option value="rick">rick</option>
<option value="john">john</option>
<option value="dick">dick</option>
</select>
</form>
AJAX
function changeadmin(verkochtid){
id = verkochtid;
console.log(id);
$.ajax({
url: 'winkels.php',
id: id,
type: 'POST',
data: $('#'+id).serialize(),
success: function(data, id){
console.log(this.data);
console.log(this.id);
}
});
};
PHP
if (isset($_POST["serialize"])) {
$data = $_POST["serialize"];
$medewerker = $data["chosen_admmin"];
$verkoopid = $data["id"];
$sql = "UPDATE verkocht SET medewerker_verwerkt = '$medewerker' WHERE verkocht_id='$verkoopid'";
echo $sql;
};
the PHP will never be executed but in the console log i get to see the id of the form and admin=rick.
I can tell from this that AJAX gets the information and procces it, but how do i set it in my PHP?
The data in the $_POST will have the index of the input name fields in your html when you serialize it.
You can use a hidden input field to get the ID serialized
<form id="23" method="post">
<select name="admin" onchange="chosenadmin(23)">
<option value="rick">rick</option>
<option value="john">john</option>
<option value="dick">dick</option>
</select>
<input type="hidden" name="id" value="23" />
</form>
Then you can do the following in you php:
if (isset($_POST["admin"]) && isset($_POST["id"])) {
$admin = $_POST["admin"];
$id = $data["id"];
$sql = "UPDATE verkocht SET medewerker_verwerkt = '$admin' WHERE verkocht_id='$id'";
echo $sql;
}
You can do the following in ajax.php to check the structure of the $_POST.
echo '<pre>';
print_r($_POST);
echo '</pre>';
die;
You should also pay attention to SQL injections.
function name onchange event of select is different! you have written onchange="chosenadmin(23)"
and for AJAX call the name is function changeadmin(verkochtid){...}
To get the value in post you must use name in PHP
HTML
<form id="23" name="frm_admin" method="post">
<select name="admin" onchange="chosenadmin(23)">
<option value="rick">rick</option>
<option value="john">john</option>
<option value="dick">dick</option>
</select>
<input type="hidden" name="id" value="23" />
</form>
AJAX
function chosenadmin(verkochtid){
id = verkochtid;
console.log(id);
$.ajax({
url: 'winkels.php',
id: id,
type: 'POST',
data: $('#'+id).serialize(),
success: function(data, id){
console.log(this.data);
console.log(this.id);
}
});
};
PHP
if (isset($_POST["frm_admin"])) {
$medewerker = $_POST["admin"];
$verkoopid = $data["id"];
$sql = "UPDATE verkocht SET medewerker_verwerkt = '$medewerker' WHERE verkocht_id='$verkoopid'";
echo $sql;
};
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.