简体   繁体   English

用javascript和php推送MySQL数据

[英]Pushing in MySQL data with javascript and php

I'm working on a program but I'm new to js/jQuery/Ajax. 我正在开发一个程序,但是对js / jQuery / Ajax不熟悉。 I am trying to get user input from a form (html) and send it over to a php file that will insert the data into a MySQL database and then ultimately spit out the information into a div. 我试图从表单(html)获取用户输入并将其发送到php文件,该文件会将数据插入MySQL数据库,然后最终将信息吐出到div中。 I press submit but my user submitted data does not get inserted into the database. 我按提交,但我的用户提交的数据未插入数据库。 I initially had the submission redirect to my php file through the tag (action="post.php") which had worked in terms of inserting the data into the mysql database but had also redirected it to that post.php file upon submission. 我最初通过标签(action =“ post.php”)将提交重定向到我的php文件,该标签的作用是将数据插入mysql数据库,但提交后也将其重定向到该post.php文件。

my js file datawire.js: 我的js文件datawire.js:

$( 'button#submit').on('click', function() {
  var uName = $('input#uName').val();
  var uMessage = $('input#uMessage').val();

  if ($.trim(uName) != '' && $.trim(uMessage) != '') {
    $.post('post.php', {username: uName, message: uMessage}, function(data) {
      $('div#viewer').text(data);
    });
  }
});

My php file post.php 我的php文件post.php

 <?php include("config.php");
$username = isset($_POST['username']);
$message = isset($_POST['message']);
if (($username && $message) && (empty($_POST['username'] === false) && empty($_POST['message']) === false)) {
    $username = $_POST['username']; 
    $message = $_POST['message'];

    // insert into database
    $nowTime = getDateTime();
    $userIp = getIp();
    $sql = "INSERT INTO commentdb (id,username, message,date,ip) VALUES ('','$username','$message', '$nowTime', '$userIp') ";
    $result = mysql_query($sql);    
} 
?>

and my HTML file: 和我的HTML文件:

<html>
    <head>        
        <!-- latest jQuery direct from google -->
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <!-- for getting data -->
        <script  src="datawire.js"></script>

        <!-- for posting data -->
        <script type="text/javascript">
            $(document).ready(function() {
              $('#viewer').load("getdata.php");
            });
        </script>
    </head>
    <body>
            <div id="viewer">   </div>
            <br>
            <!-- User form -->
            <form method='post'>
            <input type="text" id="uName" name="username" placeholder="Name" value="" maxlength="15" />
            <br />
            <input type="text" id="uMessage" name="message" placeholder="Message" value="" maxlength="100" />
            <br />
            <button id="submit" type="submit">Submit!</button> <button type="reset">Clear!</button>
            </form>           
    </body>
</html>

Prevent your form submit with preventDefault() function 使用preventDefault()函数阻止表单提交

$( 'button#submit').click(function(e) {
    e.preventDefault();
    var uName = $('input#uName').val();
    var uMessage = $('input#uMessage').val();

    if ($.trim(uName) != '' && $.trim(uMessage) != '') {
        $.post('post.php', {username: uName, message: uMessage}, function(data) {
            $('div#viewer').text(data);
         });
     }
});

Return the text message from the server like this 像这样从服务器返回短信

<?php include("config.php");
$username = isset($_POST['username']);
$message = isset($_POST['message']);
if (($username && $message) && (empty($_POST['username'] === false) && empty($_POST['message']) === false)) {
    $username = $_POST['username']; 
    $message = $_POST['message'];

    // insert into database
    $nowTime = getDateTime();
    $userIp = getIp();
    $sql = "INSERT INTO commentdb (id,username, message,date,ip) VALUES ('','$username','$message', '$nowTime', '$userIp') ";
    $result = mysql_query($sql);    

   if($result)
   {
    echo " Data Inserted Successfully";
   }else{
    echo " Data insert failed - ".mysql_error();
   }
}else{

  echo " Required fields are missing";

} 
?>

Try wrapping the contents of datawire.js with $(document).ready like you did in html file. 尝试像在html文件中一样用$ {document).ready包装datawire.js的内容。 This insures $ is actually defined before it's used. 这可以确保$在使用之前实际已定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM