Submit ExtJS Incoming Put Data into MySQL With PHP
// July 29th, 2010 // Code, Featured
Recently I built another ExtJS form panel containing a few form fields and an editable datagrid, and I wanted to implement the search box I used on a previous application. In theory this would’ve been a pretty simple task, taking into account I’ve already built both a search capable grid and an editable grid (I’ll be posting a tutorial on this in the near future), however, it wasn’t so easy, but I got it done and I’ll show you how.
ExtJS editable grid sends the edited rows to our submit form as a put method, so I had to figure out how to handle this with php. It wasn’t so complicated after it was all said and done, but figuring it out took a few minutes.
Since I wanted to submit both form field data and the data we selected from our searched results, I had to manipulate the json results sent to us from the grid and the form field data slightly different. To handle all data being submitted to MySQL from both the form fields and datagrid we create a submit.php file. We use a simple $_POST['fieldname'] and assign variables to handle the form fields, however, to handle the data coming from the grid we need to use json decode then assign the variables. Luckily for us, the put method is also handled with the submit.php file with php’s parse_str().
submit.php
# This is where we check if this is a put
if($_SERVER['REQUEST_METHOD'] == 'PUT') {
parse_str(file_get_contents("php://input"),$post_vars);
# This is how we handle the data coming from our grid with json_decode
# datainfo is the name of our array containing the data for the grid,
# which was loaded when we submitted the search using our search button
$obj = json_decode($post_vars['datainfo']);
$field1 = $obj->{'field_1'};
#This is how we handle the form fields
$field2 = $post_vars['field_2'];
$field3 = $post_vars['field_3'];
$field4 = $post_vars['field_4'];
// now we insert the values
$insert = "INSERT INTO table (field1, field2, field3, field4)
VALUES
('$field1', $field2', '$field3', '$field4')";
$result = mysql_query($insert);
if(!$result){
$error = mysql_error();
$error = str_replace("'", mysql_escape_string(" "), $error);
echo "{success: false, ";
echo "error: '{$error}'}";
exit();
}
else {
echo "{
'success': true,
'msg': 'New Items Inserted.'
}";
}
}
That’s it, hopefully that wasn’t too hard to follow along and I didn’t confuse too many of you. I hope this step by step tutorial has helped you some. Thank you for stopping by and please share with others, after all, code should be free.![]()






Thank You..