简体   繁体   English

jQuery AJAX POST允许在不进行页面重定向的情况下发送表单数据-然后如何将数据插入mysql

[英]jQuery AJAX POST to allow form data sending without page redirect- how to then insert data into mysql

I have followed three tutorials, one on submitting form data to a php file, one on writing that data to a mysql table, and then one on jQuery AJAX to do all this without page redirects. 我遵循了三篇教程,一本关于将表单数据提交到php文件,一本关于将数据写入mysql表,然后一本关于jQuery AJAX来完成所有这些操作而无需页面重定向。

The jQuery AJAX tutorial is here, by the way, as it seems a lot of people on this site have asked how to send forms without POST redirects: 顺便说一下,jQuery AJAX教程就在这里,因为似乎该站点上的很多人都在问如何在没有POST重定向的情况下发送表单:

POST a form without page redirect 张贴没有页面重定向的表单

I have got everything set up correctly in terms of getting the data to the php file via AJAX, but the php mysql statement won't write the data to the database. 关于通过AJAX将数据获取到php文件方面,我已经正确设置了所有内容,但是php mysql语句不会将数据写入数据库。 I have used firebug to check that the data is getting to the php, and it is what I think it is. 我已经使用firebug来检查数据是否到达了php,这就是我的想法。

I am certain the issue is not one of database connectivity, I think it is due to format of the data being passed to the php file. 我确定问题不是数据库连接性之一,我认为这是由于数据格式传递给php文件。 Mixing and matching the tutorials was difficult for me. 对我来说,混合和匹配这些教程很困难。 Do I need to unpack the string data that I created in the jQuery function? 我是否需要解压缩在jQuery函数中创建的字符串数据?

As an aside, in the jQuery AJAX tutorial he mentions 'There are more advanced things you can do here, other than sending an email and giving a success message. 顺便说一句,他在jQuery AJAX教程中提到“除了发送电子邮件和发送成功消息外,您还可以在这里做更多高级的事情。 For example you could send your values to a database, process them, then display the results back to the user'. 例如,您可以将值发送到数据库,进行处理,然后将结果显示回给用户。 It sounds like he is saying that you can use something other than the POST to the php file to do all this. 听起来他在说您可以使用除POST之外的其他方法来执行php文件。 I thought the whole point of the php file was to do the database storage? 我以为php文件的全部目的是进行数据库存储?

Thankyou for your time. 感谢您的时间。

The jQuery AJAX sending the data: jQuery AJAX发送数据:

  jQuery(function() {  
  jQuery(".button1").click(function() {  

    var txtRow1 = jQuery('#txtRow1').val();
    var tickerRow1 = jQuery('#tickerRow1').val();

    var dataString = 'txtRow1='+ txtRow1 + '&tickerRow1=' + tickerRow1;  
    //alert (dataString);return false;  
    jQuery.ajax({  
    type: "POST",  
    url: "form_action.php",  
    data: dataString
    });  
            return false;  
        });  
});  

And form_action.php: 和form_action.php:

<?php

$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
$dbname = 'dbname';

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);

$txtRow1 = $_POST['txtRow1'];
$tickerRow1 = $_POST['tickerRow1'];

$sqlinsert = "INSERT INTO stock_port (name, ticker) VALUES ('$txtRow1', '$tickerRow1')"; 

?> ?>

first of all, make sure you escape the data from post: 首先,确保从发布中转出数据:

$sqlinsert = "INSERT INTO stock_port (name, ticker) VALUES ('".mysql_real_escape_string($txtRow1)."', '".mysql_real_escape_string($tickerRow1)."')";

second thing, you are not executing the query (or is some code missing here?) 第二件事,您没有执行查询(或者这里缺少一些代码?)

mysql_query($sqlinsert, $conn);

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

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