2012-08-23 86 views
0

我真的很努力得到這個工作,所以任何幫助將不勝感激!發送jquery值到PHP頁面

基本上我有顯示與雙擊事件預訂如下一個jqGrid的:

ondblClickRow: function(rowid) 
    { 
     rowData = $("#bookings").getRowData(rowid); 
        var brData = rowData['bookref']; 

     getGridRow(brData); 
    }, 

這被傳遞給getGridRow功能:

function getGridRow(brData) { 

    $.ajax({ 
     url: 'bookings-dialog.php', 
     data: {'rowdata' : brData }, 
     dataType: 'json', //this is what we expect our returned data as 
    error: function(){ 
        alert("It failed"); 
        $('#cp-div-error').html(''); 
        $('#cp-div-error').append('<p>There was an error inserting the data, please try again later.</p>'); 
        $('#cp-div-error').dialog('open'); 
      }, 
     success: function(data){ 
     //empty our dialog so we don't end up with duplicate content 
     $('.cp-booking-info').empty(); 
     //we have told the browser to expect JSON back, no need to do any parsing 
     //the date 
     $('.cp-booking-info').append('<p class="pno-margin">Booking Date: '+data.bookref+'</p>'); 

     //now let's manipulate our dialog and open it. 
     $("#cp-bookings-dialog").dialog({ 
      show: { effect: 'drop', direction: "up" }, 
      hide: 'slide', 
      height: 625, 
      width: 733, 
      title: 'Booking Reference: - '+ brData 
     }); 
     } 

    }); 

這是預訂,dialog.php:

<?php 

require_once('deployment.php'); 
require_once('bootstrp/all.inc.php'); 
require_once('models/sql.php'); 
require_once('models/bookingdocket.php'); 

    $pdo = new SQL(); 
    $dbh = $pdo->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass); 

    try { 
      $rowdata = $_POST['rowdata']; 

      $query = ("SELECT * FROM tblbookings WHERE bookref = '$rowdata'"); 

     $stmt = $dbh->prepare($query); 

     $stmt->execute(); 

     $row = $stmt->fetch(PDO::FETCH_BOTH); 

      BookingDocket::set_id($row['id']); 
      BookingDocket::set_bookref($row['bookref']); 
      BookingDocket::set_bookdate($row['bookingdate']); 
      BookingDocket::set_returndate($row['returndate']); 
      BookingDocket::set_journeytype($row['journeytype']); 
      BookingDocket::set_passtel($row['passengertel']); 
      BookingDocket::set_returndate($row['returndate']); 

      $booking_ref = BookingDocket::get_bookref(); 


      return json_encode(array('bookref' => $booking_ref, 
            ) 
          ); 




     $stmt->closeCursor(); 

    } 

    catch (PDOException $pe) { 
     die("Error: " .$pe->getMessage(). " Query: ".$stmt->queryString); 
    } 

    $dbh = null; 





?> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Title Work</title> 
<link rel="stylesheet" type="text/css" href="css/main.css" /> 
<link rel="stylesheet" type="text/css" href="css/styles.css" /> 
<link rel="stylesheet" type="text/css" href="lib/jq-ui/jquery-ui-1.8.16.custom.css" /> 
<link rel="stylesheet" type="text/css" href="lib/jq-grid/ui.jqgrid.css" /> 
<script type="text/javascript" src="scripts/js/utils/util-sha256.js"></script> 
<script type="text/javascript" src="lib/jq-core/jquery-1.4.4.min.js" ></script> 
<script type="text/javascript" src="lib/jq-ui/jquery-ui-1.8.16.custom.min.js" ></script> 
<script type="text/javascript" src="lib/jq-grid/grid.locale-en.js"></script> 
<script type="text/javascript" src="lib/jq-grid/jquery.jqGrid.min.js" ></script> 
<script type="text/javascript" src="scripts/js/interface-datagrids.js"></script> 
<script type="text/javascript" src="scripts/js/image-preload.js"></script> 
<script type="text/javascript" src="scripts/js/general-interface.js" ></script> 
<script type="text/javascript" src="scripts/js/general-controlpanel.js" ></script> 
<script type="text/javascript" src="scripts/js/general-validation.js"></script> 

</head> 

<body> 

<div id="cp-bookings-dialog"> 
    <div class="cp-tiles-wrapper-dlg"> 
    <div class="cp-booking-info left"> 
    </div> 
    </div> 
</div> 


</body> 
</html> 

我想實現的是當你雙擊一個bo預訂參考(例如, BR12345)被傳遞到bookings-dialog.php並在查詢中使用(例如SELECT * FROM tblbookings WHERE bookref = '$rowdata')。我一直在嘗試使用JSON來實現這一目標,但是當我雙擊一行時,它現在只是失敗,我不確定原因。

有什麼建議嗎?

+0

到底是什麼故障?除此之外,你需要綁定你的變量,現在你有一個SQL注入問題。 – jeroen

+0

默認情況下,當你不在ajax中指定'request'時,'request'的類型是'GET',但是在'.php'腳本中,你正在使用'POST'。將'type'指定爲'post' – Bob

+0

正在返回的數據會以「未完成」的形式回覆任何想法? – nsilva

回答

2

嘗試:

$.ajax({ 
     type: 'POST', //ADD THIS .ajax defaults to GET and in the php you expect POST 
     url: 'bookings-dialog.php', 
     data: {'rowdata' : brData },