Javascript表单数据不更新Mysql数据库

时间:2017-10-05 19:51:30

标签: javascript php jquery ajax mysqli

修改

我现在得到了以下内容 - 但它仍无效

<script>

$(document).ready(function() {
        $("button").click(function(){

var jsPostcode = document.login.getElementsByName("postcode").value;
var jsEmail = document.login.getElementsByName("email").value;
var formdata = {postcode:jsPostcode,email:jsEmail};


                $.ajax(
                {
                type: "POST",
                url: "database.php", //Should probably echo true or false depending if it could do it
                data : formdata,
                success: function(feed) {
                  if (feed!="true") {
                     // DO STUFF
                  } else {
                    console.log(feed);
                     // WARNING THAT IT WASN'T DONE
                  }
                }}}};

</script>

=============================================== ==================

原始问题跟进

我尝试使用表单接收数据并将其中的一部分发送到Mysql数据库。我不能在表单中使用action关键字,因为它用于完成身份验证,因此我尝试使用ajax和jquery。我已经到了一个点,我知道我接近破解它,但我不确定是什么问题。请参阅下面的我的文件。首先关闭主文件login.php如下:

表格跟随(login.php)

<?php
   $mac=$_POST['mac'];
   $ip=$_POST['ip'];
   $username=$_POST['username'];
   $linklogin=$_POST['link-login'];
   $linkorig=$_POST['link-orig'];
   $error=$_POST['error'];
   $chapid=$_POST['chap-id'];
   $chapchallenge=$_POST['chap-challenge'];
   $linkloginonly=$_POST['link-login-only'];
   $linkorigesc=$_POST['link-orig-esc'];
   $macesc=$_POST['mac-esc'];
if (isset($_POST['postcode'])) {
    $postcode = $_POST['postcode'];
}
if (isset($_POST['email'])) {
    $email = $_POST['email'];
}
?>

**SOME HTML HERE**


<script src="jquery-3.2.1.min.js"></script>

<script>

var js-postcode = document.login.getElementsByName("postcode").value;
var js-email = document.login.getElementsByName("email").value;
var formdata = {postcode:js-postcode,email:js-email};

        $("button").click(function(){
                $.ajax(
                {
                type: "POST",
                url: "database.php", //Should probably echo true or false depending if it could do it
                data : formdata,
                success: function(feed) {
                  if (feed!="true") {
                     // DO STUFF
                  } else {
                    console.log(feed);
                     // WARNING THAT IT WASN'T DONE
                  }
                }}}

</script>

</head>

<body>

<table width="100%" style="margin-top: 10%;">
        <tr>
        <td align="center" valign="middle">
        <table width="240" height="240" style="border: 1px solid #cccccc; padding: 0px;" cellpadding="0" cellspacing="0">
        <tr>
        <td align="center" valign="bottom" height="175" colspan="2">
<!-- removed $(if chap-id) $(endif)  around OnSubmit -->
                <form name="login" action="<?php echo $linkloginonly; ?>" method="post" onSubmit="return doLogin()" >
                        <input type="hidden" name="dst" value="<?php echo $linkorig; ?>" />
                        <input type="hidden" name="popup" value="true" />

                        <table width="100" style="background-color: #ffffff">
                                <tr><td align="right">login</td>
                                <td><input style="width: 80px" name="username" type="text" value="<?php echo $username; ?>"/></td>
                                </tr>
                                <tr><td align="right">password</td>
                                <td><input style="width: 80px" name="password" type="password"/></td>
                                </tr>
                                <tr><td align="right">Postcode</td>
                                <td><input style="width: 80px" name="postcode" type="text" /></td>
                                </tr>
                                <tr><td align="right">Email</td>
                                <td><input style="width: 80px" name="email" type="text" /></td>
                                </tr>
                                <td><button><input type="submit" value="OK" /></button></td>
                                </tr>
                        </table>
                </form>
        </td>
        </tr>
        </table>

        </td>
        </tr>
</table>

<script type="text/javascript">
<!--
  document.login.username.focus();
//-->
</script>
</body>
</html>

并调用文件database.php如下:

<?php 

if ((isset($_POST['postcode'])) && (isset($_POST['email']))) {

   $postcode = $_POST['postcode'];
   $email = $_POST['email'];

  $connect= new mysqli_connect('xx','xx','xx','xx');


  if ($conn->connect_errno) {
    echo "There was a problem connecting to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
  }

  if (!($sql = $conn->prepare("INSERT INTO visitors(postcode,email) VALUES(postcode,email)"))) {
    echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
  }

  //NOTE: the "ss" part means that $postcode and $email are strings (mysql is expecting datatypes of strings). For example, if $postcode is an integer, you would do "is" instead.

  if (!$sql->bind_param("ss", $postcode, $email)) {
    echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
  } 


  if (!$sql->execute()) {
    echo "Execute failed: (" . $sql->errno . ") " . $sql->error;

  } 


} else {

  echo 'Variables did not send through ajax.'; // any echoed values would be sent back to javascript and stored in the 'response' variable of your success or fail functions for testing.

}

?>

请帮助 - 非常感谢所有帮助

0 个答案:

没有答案