简体   繁体   中英

Pushing in MySQL data with javascript and php

I'm working on a program but I'm new to 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. 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.

my js file 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 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>
    <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

$( '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. This insures $ is actually defined before it's used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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