2013-03-06 32 views
2

我得到顯示此數據字符串的成功警報,但由於某種原因,我的php代碼沒有執行,否則它確實不成功,我不確定哪一個是。謝謝。提交表單時沒有使用AJAX和JQuery刷新頁面時出現問題

inputPlantSite=Syncrude+Base+Mine&inputRouteNumber=aafdfadsfadsafds&inputStopNumber=asfdafdsasfdafds&inputLatitude=57.90609313959766&inputLongitude=-113.4063720703125&inputStreetName=asfdafdsasfdafds&inputArrivalTime=asfdafdsasfdafds&inputDayOfWeek=Weekday&inputDirectionOfBus=Town+to+Site&inputWhatShift=asfdafdsasfdafds&markerVariable=addmarker 

這裏是我的阿賈克斯/ jQuery的

$(function() { 
    $("#submit_marker").click(function(e) { e.preventDefault(); 
    var inputPlantSite = $("#inputPlantSite").val(); 
    var inputRouteNumber = $("#inputRouteNumber").val(); 
    var inputStopNumber = $("#inputStopNumber").val(); 
    var inputLatitude = $("#inputLatitude").val(); 
    var inputLongitude = $("#inputLongitude").val(); 
    var inputStreetName = $("#inputStreetName").val(); 
    var inputArrivalTime = $("#inputArrivalTime").val(); 
    var inputDayOfWeek = $("#inputDayOfWeek").val(); 
    var inputDirectionOfBus = $("#inputDirectionOfBus").val(); 
    var inputWhatShift = $("#inputWhatShift").val(); 
    var inputAddMarker = $("#inputAddMarker").val(); 
    var inputUpdateMarker = $("#inputUpdateMarker").val(); 
    var inputDeleteMarker = $("#inputDeleteMarker").val(); 
    var dataString = $('#formManageMarker').serialize(); 
    alert(dataString); 

    $.ajax({ 
     type: "POST", 
     url: "php/addmarker.php", 
     data: dataString, 
dataType: "json", 
     success: function() { 
     alert("Successfully added marker"); 
     } 
    }); 
    return false; 
    }); 
}); 

,這裏是我的表單:

<form name="formManageMarker" method="post" action=""> 
     <select name="inputPlantSite" class="input-block-level" id="inputPlantSite"> 
     <option value="Syncrude Base Mine">Syncrude Base Mine</option> 
     <option value="Syncrude Base Plant">Syncrude Base Plant</option> 
     <option value="Syncrude Aurora Base Mine">Syncrude Aurora Base Mine</option> 
     <option value="Syncrude Aurora Base Plant">Syncrude Aurora Base Plant</option> 
     <option value="Suncor Base Mine">Suncor Base Mine</option> 
     <option value="Suncor Base Plant">Suncor Base Plant</option> 
     <option value="Suncor Firebag">Suncor Firebag</option> 
     <option value="Albian Sands">Albian Sands</option> 
     </select> 
     <input type="text" class="input-block-level" placeholder="Route Number e.g. 1" name="inputRouteNumber" id="inputRouteNumber"> 
     <input type="text" class="input-block-level" placeholder="Stop Number e.g. 1" name="inputStopNumber" id="inputStopNumber"> 
     <input type="text" class="input-block-level" placeholder="Latitude" name="inputLatitude" id="inputLatitude"> 
     <input type="text" class="input-block-level" placeholder="Longitude" name="inputLongitude" id="inputLongitude"> 
     <input type="text" class="input-block-level" placeholder="Street Name" name="inputStreetName" id="inputStreetName"> 
     <input type="text" class="input-block-level" placeholder="Arrival Time e.g. 6:00am" name="inputArrivalTime" id="inputArrivalTime"> 
     <select name="inputDayOfWeek" class="input-block-level" id="inputDayOfWeek"> 
      <option value="Weekday">Weekday</option> 
      <option value="Weekend">Weekend</option> 
      <option value="ADO">ADO</option> 
     </select> 
     <select name="inputDirectionOfBus" class="input-block-level" id="inputDirectionOfBus"> 
     <option value="Town to Site">Town To Site</option> 
     <option value="Site To Town">Site To Town</option> 
     </select> 
     <input type="text" name="inputWhatShift" id="inputWhatShift" class="input-block-level" placeholder="What Shift e.g. Day Night"> 
     <select name="markerVariable" class="input-block-level"> 
      <option value="addmarker" class="alert-success">Add Marker</option> 
      <option value="updatemarker" class="alert-info">Update Marker</option> 
      <option value="deletemarker" class="alert-error">Delete Marker</option> 
     </select> 

     <button type="submit" name="addmarker" id="submit_marker" class="btn btn-info btn-large">Create Marker</button> 
    </form> 

PHP文件:

<?php 
require_once('./config.inc.php'); 

$plantsite = $_POST['inputPlantSite']; 
$routenumber = $_POST['inputRouteNumber']; 
$stopnumber = $_POST['inputStopNumber']; 
$latitude = $_POST['inputLatitude']; 
$longitude = $_POST['inputLongitude']; 
$streetname = $_POST['inputStreetName']; 
$markertitle = "Stop $stopnumber Route $routenumber"; 
$arrivaltime = $_POST['inputArrivalTime']; 
$directionofbus = $_POST['inputDirectionOfBus']; 
$whatshift = $_POST['inputWhatShift']; 
$inputSearchSite = $_POST['inputSearchSite']; 
$inputSearchRoute = $_POST['inputSearchRoute']; 
$dayofweek = $_POST['inputDayOfWeek']; 
$inputMarkerVariable = $_POST['markerVariable']; 
$success = 0; 

/* Connect to Database */ 
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_BASE); 
if (mysqli_connect_errno()) { 
    printf('Connect failed: %s\n', mysqli_connect_error()); 
    exit(); 
} 
if ($stmt = $mysqli -> prepare("SELECT Latitude, Longitude, Title, Arrival_Time, Direction_Of_Bus, What_Shift FROM Routes")) { 
    $stmt -> execute(); 
    $stmt -> bind_result($gLatitude, $gLongitude, $gTitle, $gArrivalTime, $gDirectionOfBus, $gwhatShift); 


} 

if ($inputMarkerVariable == "addmarker") { 
    /* Check fields */ 
    if ((!empty($stopnumber)) && (!empty($whatshift)) && (!empty($routenumber)) && (!empty($latitude)) 
     && (!empty($longitude)) && (!empty($streetname)) && (!empty($arrivaltime))) { 

    /* Connect to the Database */ 

    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_BASE); 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 
    /* Get ready to query database */ 

    if ($stmt = $mysqli -> prepare("INSERT INTO Routes (Plant_Site, Route_Number, Stop_Number, Street_Name, 
            Latitude, Longitude, Title, Arrival_Time, Day_Of_Week, 
            Added_By, Direction_Of_Bus, What_Shift, Updated_At, Created_At) 
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())")) { 

     /* Bind Parameters */ 

     $stmt -> bind_param('ssssssssssss', $plantsite, $routenumber, $stopnumber, $streetname, 
          $latitude, $longitude, $markertitle, 
          $arrivaltime, $dayofweek, $sesh_email, $directionofbus, $whatshift); 

     /* Exceute and send it to database */ 

     $stmt -> execute(); 

     $success = 1; 
     json_encode(true); 
     json_encode(false); 

     $stmt -> close(); 


     if ($stmt = $mysqli -> prepare("SELECT Latitude, Longitude, Title, Arrival_Time, Direction_Of_Bus, What_Shift FROM Routes")) { 
     $stmt -> execute(); 
     $stmt -> bind_result($gLatitude, $gLongitude, $gTitle, $gArrivalTime, $gDirectionOfBus, $gwhatShift); 




} 


    } else { 
     printf('Couldnt make a connection'); 
    } 
    } else { $alertmarker = "Not all fields are filled out"; } 
} 


if (isset($_POST['inputSortMarkers'])) { 
    $mysqli -> mysqli(DB_HOST, DB_USER, DB_PASS, DB_BASE); 
    if (mysqli_connect_errno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
     exit(); 
    } 

    /* Get Ready to Sort Markers */ 

    if ($stmt = $mysqli -> prepare("SELECT Latitude, Longitude, Title, Arrival_Time, Direction_Of_Bus, What_Shift FROM Routes WHERE Plant_Site = ? AND Route_Number = ?")) { 
     $stmt -> bind_param('si', $inputSearchSite, $inputSearchRoute); 
     $stmt -> execute(); 
     $stmt -> bind_result($gLatitude, $gLongitude, $gTitle, $gArrivalTime, $gDirectionOfBus, $gwhatShift); 
     printf('Worked'); 
    } 

} 




?> 
+0

是'警報(dataString);返回false;'可以在你的腳本? 'return false'將終止函數的執行。 – 2013-03-06 04:20:48

+0

@SheikhHeera它是,但只用於調試,你可以看到每個變量不是隻加載第一個 – Datsik 2013-03-06 04:22:45

+0

我不確定你的問題是什麼。什麼是您需要幫助的實際問題? – 2013-03-06 04:28:46

回答

2

你的代碼中有return false,所以下面的代碼將不會執行。您可以使用$('form').serialize();發送數據字符串。

並使用下面的代碼。

$("#submit_marker").click(function(e) { e.preventDefault(); 

上面的代碼將迫使button僅執行click事件代碼。

+0

你能否看到我的原始文章中更新的腳本,並告訴我爲什麼它沒有被添加到數據庫中,請記住成功警報正在觸發,那麼這是否意味着這是PHP端的問題還是? – Datsik 2013-03-06 04:45:26

+0

@XCritics如果它顯示你成功添加標記,那麼服務器端可能有錯誤...但如果你的服務器端腳本失敗,那麼我不認爲它會提示成功代碼..但我可以看到PHP代碼。 – 2013-03-06 04:48:36

+0

它現在在主文章的主體中 – Datsik 2013-03-06 04:50:33

0

嘗試編碼每個變量被串連成dataString:

dataString = 'inputPlantSite=' + encodeURIComponent(inputPlantSite) + '&..=' + encodeURIComponent(..) 
0

嘗試序列化您的信息,這樣的..

var serialized = $(dataString).serialize(); 

$.ajax({ 
     type: "POST", 
     url: "php/addmarker.php", 
     data: serialized, 
     success: function() { 
     alert("Successfully added marker"); 
     } 

的.serialize()方法創建標準的URL編碼符號的文本字符串。它在代表一組表單元素的jQuery對象上運行。

+0

'datastring'不是表單元素的集合....這個概念不起作用 – charlietfl 2013-03-06 04:26:13

+0

您是否檢查過這個帖子? http://stackoverflow.com/questions/2231810/escaping-jquery-data-being-sent-via-post – lemil77 2013-03-06 04:33:19

+0

在該帖子中顯示的formselector與你的答案中的datastring不同。嘗試一下...在jsfiddle.net中創建一個演示,並嘗試序列化'datastring' – charlietfl 2013-03-06 04:36:52

0

試試這個:

$.post('php/addmarker.php',{ 
    //param : value 
    inputPlantSite : inputPlantSite, 
    inputRouteNumber : inputRouteNumber, 
    inputStopNumber : inputStopNumber, 
    inputLatitude : inputLatitude, 
    inputLongitude : inputLongitude, 
    inputStreetName : inputStreetName, 
    inputArrivalTime : inputArrivalTime, 
    inputDayOfWeek : inputDayOfWeek, 
    inputDirectionOfBus : inputDirectionOfBus, 
    inputWhatShift : inputWhatShift, 
    inputAddMarker : inputAddMarker, 
    inputUpdateMarker : inputUpdateMarker, 
    inputDeleteMarker : inputDeleteMarker 
    },function(data){ 
    console.log(data); 
    alert("Successfully added marker"); 
    }); 
相關問題