reyjoel reyjoel - 4 months ago 17
MySQL Question

update database with jquery and php

i try to update/edit using jquery and then save it to database with the help of php

the problem is i cant pass the new data to php or php didnt receive data from jquery

here is my form

<form method="POST" name="postForm" action="update.php">
<div id="edit">
First name:<input type="text" id="firstname" value="<?php echo $row['firstname']; ?>">
</div>
</form>
<button id="button">Edit</button>


and this is my script of jquery

jQuery("#edit").dialog({
modal: true,
resizable: false,
draggable: false,
autoOpen: false,
width: 400,
buttons: [
{
text: "Ok",
click: function() {
jQuery.ajax({
type: "POST",
dataType: "json",
url: "update.php",
async:true,
data: { dataToUpdate: jQuery('#edit').attr('value') }
});
}
},
{
text: "Cancel",
click: function() {
jQuery(this).dialog( "close" );
}
}
]
});

// Link to open the dialog
jQuery("#button").click(function(event) {
jQuery("#edit").dialog("open");
event.preventDefault();
});


and this is my PHP

$id = $_GET['id']; // i dont know if i should put it or not to identify the info i want to edit
$firstname = $_POST['firstname'];

$sql = "UPDATE info SET
firstname='$firstname' WHERE id=$id";

$result = mysql_query($sql);

echo json_encode($result);

Answer

You have missed the name attribute in your form's input tag that's why PHP didn't receive the variable...

<input type="text" id="firstname" name="firstname" value="<?php echo $row['firstname']; ?>">

Or make sure you are passing the value properly using JQuery.

Comments