2016-08-03 94 views
0

我的IntelXDK HTML5移動應用程序表單向MySQL數據庫提交空值。Jquery表單向Mysql提交空值

我的HTML(單個文件的應用程序)

<label class="item item-input widget uib_w_6 d-margins" data-uib="ionic/input" data-ver="0" id="fullname"> 
    <input type="text" placeholder="Fullname" name="fullname"> 
</label> 
<label class="item item-input widget uib_w_7 d-margins" data-uib="ionic/input" data-ver="0" id="email"> 
    <input type="email" placeholder="Email" name="email"> 
</label> 
<label class="item item-input widget uib_w_8 d-margins" data-uib="ionic/input" data-ver="0" id="pass"> 
    <input type="password" placeholder="Password" name="pass"> 
</label> 
<button class="button widget uib_w_9 d-margins button-positive" data-uib="ionic/button" data-ver="0" id="signup"> 
    Create Account 
</button> 
<span class="uib_shim"></span> 

我的JS

$(document).on("click", "#signup", function (evt) { 
    /* your code goes here */ 
    //these two lines take the values from inputs using jquery 
    var input1 = $("#fullname").val(); 
    var input2 = $("#email").val(); 
    var input3 = $("#pass").val(); 

    //jquery ajax to send values to php using POST 
    $.ajax({ 
     type: 'POST', 
     url: 'http://localhost/signup.php', 
     data: { 
      fullname: input1, 
      email: input2, 
      pass: input3 
     }, 
     success: function (response) { 
      //from php using echo 
      alert(response); 
     } 
    }); 

    return false; 
}); 

而且PHP文件

<?php 
// Create connection 
$conn = new mysqli("localhost", "user", "pass", "dbname"); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$fullname = Trim(stripslashes($_POST['fullname'])); 
$email = Trim(stripslashes($_POST['email'])); 
$pass = Trim(stripslashes($_POST['pass'])); 

$sql = "INSERT INTO login (fullname, email, pass) VALUES ('$fullname','$email', '$pass')"; 

if ($conn->query($sql) === TRUE) { 
    echo "New record created successfully"; 
} else { 
    echo "Error: " . $sql . "<br>" . $conn->error; 
} 

$conn->close(); 
?> 

在提交這份表單,它是建立在MySQL空記錄。

任何幫助表示讚賞。

+1

你還沒有設置輸入字段的ID,那麼如何獲得輸入字段的值與ID。 –

回答

0

這是發生,因爲

<input type="text" placeholder="Fullname" name="fullname"> 

有像編號ID =「全名」可用,你指的是它。

+0

謝謝。有用。只要注意,如果標籤具有相同的ID,仍然會發送空的數據。更改標籤ID或將其刪除。然後它工作正常。 – SamS