2014-07-24 110 views
-4

好吧,這讓我瘋了,我有這個工作,但它似乎沒有抓住我的ajax調用在我的PHP文件中的任何變量..我回聲,只看到沒有價值。 ..任何人都可以解釋什麼即時做錯了,我找不到任何錯誤,,Ajax調用將不起作用

var owner_name = $('#owner_name').html(); 
var owner_email = $('#owner_email').html(); 
var owner_mobile = $('#owner_mobile').html(); 
var owner_phone = $('#owner_phone').html(); 

function jobcardCreate() { 

$.ajax({ 
    dataType: 'json', 
    url: './ajax/jobcard-send.php', 
    type: 'POST', 
    data: { 
     owner_name: owner_name, 
     owner_email: owner_email, 
     owner_mobile: owner_mobile, 
     owner_phone: owner_phone, 
     },  
    success: function(data) { 

    } 
}); 

} 

jobcard-send.php:

<?php 

    include("../includes/connection.php"); 

    $owner_name = $_POST['owner_name']; 
    $owner_email = $_POST['owner_email']; 
    $owner_mobile = $_POST['owner_mobile']; 
    $owner_phone = $_POST['owner_phone']; 

    // Insert new data into users table 
    $query = " INSERT INTO"; 
    $query .= " owner"; 
    $query .= " (owner_name, owner_email, owner_mobile, owner_phone)"; 
    $query .= " VALUES"; 
    $query .= " ('{$owner_name}', '{$owner_email}', '{$owner_mobile}', '{$owner_phone}')"; 

    echo $owner_name; 
    echo $query; 

    $result = mysqli_query($connection, $query); 

    //Check if there was an error with the query 
    if ($result) { 
     echo json_encode(array("status" => "$owner_name")); 
    } else { 
     echo "Database query failed, " . mysqli_error($connection) . ""; 
    } 

?> 

浪費了很多很多的時間在這個已經,之後我曾經有過工作,:/

這是htm L:

<div class="card-content one-third"> 
     <div class="list-title thin">Owner Contact</div> 
     <ul class="detail-list"> 
      <li class="button rounded"><a class="list-heading">Name:</a> <a id="owner_name" class="list-result-edit" contenteditable="true"></a></li> 
      <li class="button rounded"><a class="list-heading">Email:</a> <a id="owner_email" class="list-result-edit" contenteditable="true"></a></li> 
      <li class="button rounded"><a class="list-heading">Mobile No:</a> <a id="owner_mobile" class="list-result-edit" contenteditable="true"></a></li> 
      <li class="button rounded"><a class="list-heading">Phone No:</a> <a id="owner_phone" class="list-result-edit" contenteditable="true"></a></li> 
     </ul> 
    </div> 
+3

您是否在瀏覽器控制檯中觀看過AJAX請求/響應? #owner_name et.al.輸入框?如果是這樣,你應該使用'.val()'而不是'.html()' –

+0

是的,它運行查詢罰款,並將新的數據添加到數據庫..但只是空白..因爲它不拉在我的POST變量從ajax調用。 – 22Ryann

+1

**危險**:您很容易[SQL注入攻擊](http://bobby-tables.com/)**,您需要[防禦](http://stackoverflow.com/questions/ 60174/best-way-to-prevent-sql -injection-in-php)自己從。 – Quentin

回答

2

var owner_name = $('#owner_name').html();和朋友當時讓你的錨的innerHTML腳本運行時,也就是當頁面加載(這樣用戶才能輸入任何內容)。

移動你的嘗試去閱讀innerHTML,所以它們是裏面的jobcardCreate函數。

+0

我試過這個,沒有成功。讓我現在再試一次 – 22Ryann

+0

Works here:http://jsbin.com/tumibufi/1/edit?html,output – Quentin

+0

好的,所以現在它在功能的內部和外部都工作..是否有可能存在問題在服務器端?通常我看不到代碼的問題,但當我測試時會得到意想不到的結果。 也許我應該只在Windows環境下的Xammp上運行。而不是直播。? – 22Ryann