dongshixingga7900 2013-05-14 21:21
浏览 85
已采纳

JQGrid,如何将JSON字符串发布到PHP来处理并发送到数据库?

Ok I have multiselected rows that I want to send to database onclick. Creates string of selected data rows and firebug shows they post. However I can't get it to post to the php and send to database as new entries. Please help.Also i would like to delete rows from database after sent to different table. Please i need help life and death.

jqgrid[edit] Finally got it posting into the database. Only problem is that it posts each selection 7 times. I want it to post only one for each selection. Any 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 to the 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 to db.");




//First decode the array
$arr = $_POST["json"];
$decarr = json_decode($arr, true);

$count = count($decarr);

$values = array(); // This will hold our array values so we do one single insert

for ($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 the records   

$sql = "INSERT INTO finish (id, name, id_continent, lastvisit,cdate,ddate, email)
VALUES ".implode(',', $values);

$result = mysql_query($sql, $con) or die(mysql_error());  


?>
  • 写回答

1条回答 默认 最新

  • dongzhou5344 2013-05-16 01:27
    关注

    Simply removed the excess post defs.

    //First decode the array
    $arr = $_POST["json"];
    $decarr = json_decode($arr, true);
    $count = count($decarr);
    
    
    for ($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 the records   
    
    $sql = "INSERT INTO finish (id, name, id_continent, lastvisit,cdate,ddate, email)
    VALUES ".implode(',', $values);
    
    $result = mysql_query($sql, $con) or die(mysql_error());  
    
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?