通过PHP在MySQL中插入Jquery

时间:2022-09-26 09:52:07

I am new to jQuery and MySQL inserting. My goal is to insert Phonegap application data in MySQL but unfortunately I'm having a difficult time executing. So I started now in the basic jQuery-AJAX inserting through PHP but still it is not working. Can someone help me? Thank you.

我是jQuery和MySQL插件的新手。我的目标是在MySQL中插入Phonegap应用程序数据,但不幸的是我执行起来很困难。所以我现在开始通过PHP插入基本的jQuery-AJAX,但它仍然无法正常工作。有人能帮我吗?谢谢。

Here's my testjs.html file

这是我的testjs.html文件

<html>
<head>

   <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
   <script type="text/javascript">
          $(document).ready(function(){
                $("#button").click(function(){

                      var name=$("#name").val();
                      var message=$("#message").val();

                      $.ajax({
                          type:"post",
                          url:"process.php",
                          data:"name="+name+"&message="+message,
                          success:function(data){
                             $("#info").html(data);
                          }

                      });

                });
           });
   </script>
 </head>

 <body>
      <form id = "commentform"> 
           name : <input type="text" name="name" id="name"/>
           </br>
           message : <input type="text" name="message" id="message" />
           </br>
           <input type = "submit" id = "button" value="Send Comment">

           <div id="info" />   <span id="msg"></span>              
      </form>
 </body>
 </html>

and here's my process.php file

这是我的process.php文件

<?php
mysql_connect("localhost","root","");
mysql_select_db("comments");

$name=$_POST["name"];
$message=$_POST["message"];

$query=mysql_query("INSERT INTO comment2 (name,message) values('$name','$message') ");

if($query){
    echo "Your comment has been sent";
}
else{
    echo "Error in sending your comment";
}
?>

I dont know where I am wrong because when I run testjs.html in my browser I get no result. Even this message "Your comment has been sent" is not displaying and if I run my process.php file alone, it's working. Of course there is a problem on these parts because

我不知道我错在哪里,因为当我在浏览器中运行testjs.html时,我得不到任何结果。即使这条消息“你的评论已被发送”也没有显示,如果我单独运行我的process.php文件,它就可以了。当然这些部件存在问题,因为

$name=$_POST["name"];
$message=$_POST["message"];

I manually run it without getting the value from the HTML file. I downloaded my own jquery.js and make it as a source but still not working. All of my files are in one folder under my xampp/htdocs. Thank you.

我手动运行它而不从HTML文件中获取值。我下载了自己的jquery.js并将其作为源但仍然无法正常工作。我的所有文件都在xampp / htdocs下的一个文件夹中。谢谢。

5 个解决方案

#1


0  

$("#button").click(function() {

      var name=$("#name").val();
      var message=$("#message").val();

      $.ajax({
          type:"post",
          url:"process.php",
          data:"name="+name+"&message="+message,
          success:function(data){
             $("#info").html(data);
          }

      });
      return false;

});

Add return false after calling ajax

调用ajax后添加返回false

#2


0  

Try using:

$(document).ready(function(){
  $("#button").click(function(){
        var name=$("#name").val();
        var message=$("#message").val();

        $.ajax({
            type:"POST",
            url:"process.php",
            data:{name: name, message: message},
            success:function(data){
               $("#info").html(data);
            }

        });

        return false;
  });
});

It wasn't working because your form was submitting and refreshing the page.

它无法正常工作,因为您的表单正在提交并刷新页面。

#3


0  

Try this? It's untested and based on my memory so it may or may not work.

试试这个?它是未经测试的,并且基于我的记忆,所以它可能会也可能不会起作用。

<script language="javascript">
$(document).ready(function(){
    $("#button").click(function(evt){
        evt.preventDefault(); // prevent form submission so we can use ajax
        var name = $("#name").val();
        var message = $("#message").val();
        var sendData = { "name": name, "message": message };

        $.ajax({
            type: "POST",
            url: "process.php",
            data: sendData,
            success: function(data) {
                $("#info").html(data);
            }

        });
    });
});
</script>

#4


0  

Try to change

试着改变

<input type = "submit" id = "button" value="Send Comment">

To

<input type = "button" id = "button" value="Send Comment">

#5


0  

Does your db connect work? try adding something like

您的数据库连接是否有效?尝试添加类似的东西

$link = mysql_connect("localhost","root","");
mysql_select_db('comments', $link);

My suggestion is that you should use PDO or mysqli

我的建议是你应该使用PDO或mysqli

#1


0  

$("#button").click(function() {

      var name=$("#name").val();
      var message=$("#message").val();

      $.ajax({
          type:"post",
          url:"process.php",
          data:"name="+name+"&message="+message,
          success:function(data){
             $("#info").html(data);
          }

      });
      return false;

});

Add return false after calling ajax

调用ajax后添加返回false

#2


0  

Try using:

$(document).ready(function(){
  $("#button").click(function(){
        var name=$("#name").val();
        var message=$("#message").val();

        $.ajax({
            type:"POST",
            url:"process.php",
            data:{name: name, message: message},
            success:function(data){
               $("#info").html(data);
            }

        });

        return false;
  });
});

It wasn't working because your form was submitting and refreshing the page.

它无法正常工作,因为您的表单正在提交并刷新页面。

#3


0  

Try this? It's untested and based on my memory so it may or may not work.

试试这个?它是未经测试的,并且基于我的记忆,所以它可能会也可能不会起作用。

<script language="javascript">
$(document).ready(function(){
    $("#button").click(function(evt){
        evt.preventDefault(); // prevent form submission so we can use ajax
        var name = $("#name").val();
        var message = $("#message").val();
        var sendData = { "name": name, "message": message };

        $.ajax({
            type: "POST",
            url: "process.php",
            data: sendData,
            success: function(data) {
                $("#info").html(data);
            }

        });
    });
});
</script>

#4


0  

Try to change

试着改变

<input type = "submit" id = "button" value="Send Comment">

To

<input type = "button" id = "button" value="Send Comment">

#5


0  

Does your db connect work? try adding something like

您的数据库连接是否有效?尝试添加类似的东西

$link = mysql_connect("localhost","root","");
mysql_select_db('comments', $link);

My suggestion is that you should use PDO or mysqli

我的建议是你应该使用PDO或mysqli