jquery - JQGrid, How to post JSON string to PHP to Process and send to Database? -
ok have multiselected rows want send database onclick. creates string of selected data rows , firebug shows post. can't post php , send database new entries. please help.also delete rows database after sent different table. please need life , death.
jqgrid[edit] got posting database. problem posts each selection 7 times. want post 1 each selection. help,please?
$(function(){ $("#list").jqgrid({ url:'request.php', editurl: "sendyo.php", datatype: 'xml', mtype: 'get', multiselect:true, multiboxonly:true, height: 450, width: 850, colnames:['id','project', 'assigned to','assign date','check date','due date','attachments'], colmodel :[ {name:'id', index:'id', width:25}, {name:'name', index:'name', width:250, align:'left',editable:true, editoptions:{ size:60} }, {name:'id_continent', index:'id_continent', width:55, align:'right',editable:true,edittype:'select', editoptions:{value: "henry:henry; ramon:ramon; paul:paul" },mtype:'post' }, {name:'lastvisit', index:'lastvisit', width:70, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm-d-y',editable:true, edittype: 'text',mtype:'post' ,editoptions:{size:10, datainit:function(elem){$(elem).datepicker({dateformat:'m/d/yy'});}}} , {name:'cdate', index:'cdate', width:70, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm-d-y', edittype: 'text',editable:true ,mtype:'post' ,editoptions:{size:10, datainit:function(elem){$(elem).datepicker({dateformat:'m/d/yy'});}}} , {name:'ddate', index:'ddate', width:70, align:'right',formatter: 'date',srcformat:'yyyy-mm-dd',newformat: 'm-d-y',date:'true',editable:true, edittype: 'text',editoptions:{size:10, datainit:function(elem){$(elem).datepicker({dateformat:'m/d/yy'});}}} , {name:'email', index:'email', width:70,align:'center',sortable:false,mtype:'post' } ], pager: '#pager', rownum:20, rowlist:[20,40,80], sortname: 'id', sortorder: 'desc', viewrecords: true, gridview: true, caption: 'pending assignments', ondblclickrow: function(rowid) { $(this).jqgrid('editgridrow', rowid, {width:450,height:400,recreateform:true,closeafteredit:true, closeonescape:true,reloadaftersubmit:false, modal:true,mtype:'post'});} }); jquery("#minibutton").click( function(){ var selectedrows = $("#list").jqgrid('getgridparam','selarrrow'); if(selectedrows.length) { for(var i=0;i<selectedrows.length; i++) { var selecteddatais = $("#list").jqgrid('getrowdata',selectedrows[i]); var rows=json.stringify(selecteddatais) var postarray = {json:rows}; $.ajax({ type: "post", url: "jsonsend.php", data: postarray, datatype: "json", error: function () { alert("an error occurred."); }, success: function (data) { } }); $( '#list' ).trigger( 'reloadgrid', [{ page: 1}] );//reload grid} }} }); });
php:
// connect mysql database server $con = mysql_connect($dbhost, $dbuser, $dbpassword); if (!$con) { die('could not connect: ' . mysql_error()); } @mysql_select_db($database,$con) or die("error connecting db."); //first decode array $arr = $_post["json"]; $decarr = json_decode($arr, true); $count = count($decarr); $values = array(); // hold our array values 1 single insert ($x=0; $x < $count; $x++){ $newrec = $decarr; $id = $newrec['id']; $id = mysql_real_escape_string($id); $name = $newrec['name']; $name = mysql_real_escape_string($name); $id_continent = $newrec['id_continent']; $id_continent = mysql_real_escape_string($id_continent); $email = $newrec['email']; $email = mysql_real_escape_string($email); $lastvisit = $newrec['lastvisit']; $lastvisit = mysql_real_escape_string($lastvisit); $cdate = $newrec['cdate']; $cdate = mysql_real_escape_string($cdate); $ddate = $newrec['ddate']; $ddate = mysql_real_escape_string($ddate); // create insert array $values[] = "('".$id."', '".$name."', '".$id_continent."', '".$lastvisit."','".$cdate."','".$ddate."','".$email."' )"; } // insert records $sql = "insert finish (id, name, id_continent, lastvisit,cdate,ddate, email) values ".implode(',', $values); $result = mysql_query($sql, $con) or die(mysql_error()); ?>
simply removed excess post defs.
//first decode array $arr = $_post["json"]; $decarr = json_decode($arr, true); $count = count($decarr); ($x=0; $x < $count; $x++){ $newrec = $decarr; $id = $newrec['id']; $name = $newrec['name']; $id_continent = $newrec['id_continent']; $email = $newrec['email']; $lastvisit = $newrec['lastvisit']; $cdate = $newrec['cdate']; $ddate = $newrec['ddate']; } // create insert array $values[] = "('".$id."', '".$name."', '".$id_continent."', '".$lastvisit."','".$cdate."','".$ddate."','".$email."' )"; // insert records $sql = "insert finish (id, name, id_continent, lastvisit,cdate,ddate, email) values ".implode(',', $values); $result = mysql_query($sql, $con) or die(mysql_error()); ?>
Comments
Post a Comment