2013-09-21 70 views
0

我遇到了令人困惑的問題 - 我正在設法從文本框中提取一個值,並將其成功插入到我的表中,但另一個(也來自文本框)是不會進入。在AJAX調用之前,我已經提醒我的數據串以確保兩個值都是正確的,而且它們是。然而,當我查看數據庫時,只輸入名稱值並且電子郵件空白。無法將一個項目導入到MySQL數據庫db

HTML:

<div id="basic-modal-content"> 
    <h3>Please Alert me when this is Available</h3> 


    <p>Your Name: <input type="text" id="alert_name"/></p> 
    <p>Email Address: <input type="text" id="alert_email"/></p> 
    <button id="alert_submit">Submit</button> 
</div> 

的Javascript:

$('#alert_submit').click(function(){ 
    var datastring = 'name='+ $('#alert_name').val() + '&email=' + $('#alert_email').val(); 
    alert(datastring); 
     $.ajax({ 
      type: "POST", 
      url: "process_email.php", 
      data: datastring, 
      success: function(data) { 

      } 
     }); 
     alert ("We've received your request and will alert you once the directory is available. Thank you."); 
     $.modal.close(); 
}); 

PHP:

$name = $_POST['name']; 
$email = $_POST['email']; 
try { 

     $conn = new PDO('mysql:host=blah;dbname=blah', '-', '-'); 
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

     $stmt = $conn->prepare("INSERT INTO email_alerts(name, email) VALUES(':name, :email)"); 
     $stmt->execute(array('name' => $name, 'email' => $email)); 
     //$affected_rows = $stmt->rowCount(); 
     $conn = null; 
} catch(PDOException $e) { 
    echo 'ERROR: ' . $e->getMessage(); 
    $conn = null; 
} 
+2

'$ stmt-> EXE數組('name'=> $ name,'e​​mail'=> $ email));'應該是'$ stmt-> execute(array(':name'=> $ name,':email'=> $電子郵件));' –

+0

@NullPoiиteяPDO也接受沒有冒號的參數名稱,雖然沒有記錄。 – Barmar

+0

@Barmar yupp我知道,這就是爲什麼我用_shuold_而不是_must_ .. :) –

回答

0

試試這個代碼,請

$('#alert_submit').click(function(){ 

      $.ajax({ 
       type: "POST", 
       url: "process_email.php?name="+$('#alert_name').val() +"&email="+ $('#alert_email').val(), 

       success: function(data) { 

       } 
      }); 
      alert ("We've received your request and will alert you once the directory is available. Thank you."); 
      $.modal.close(); 
    }); 
+0

仍收到零的電子郵件:( –

+0

只是打印其他頁面上的值檢查天氣值是否提交DER或不 –

+0

只是echo $ _REQUEST ['name']和echo $ _REQUESR ['email'] –

相關問題