我目前在我网站上的代码如下,我将很快将该代码转移到我的profile.php :)
test.php的
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/
libs/jquery/1.3.0/jquery.min.js">
</script>
<script type="text/javascript" >
$(function() {
$("#ntb").click(function() {
var nt = $("#nt").val();
var data=$('#nt').serialize();
if(nt=='')
{
$('.success').fadeOut(200).hide();
$('.error').fadeOut(200).show();
}
else
{
$.ajax({
type: "POST",
url: "../ajax/post.php",
data: dataString,
success: function(){
$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();
}
});
}
return false;
});
});
</script>
<form method="post" name="form">
<input id="nt" name="nt" type="text" />
<input type="submit" style="display:block;" name='ntb' id='ntb' class="Buttonchat" value="Post" />
</form>
<div>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Registration Successfully</span>
</div></form>
上面这段代码是ajax和html,当我有ajax但是当我只有html和php它发布到数据库中时,它真的不起作用,idk为什么虽然:(
../ AJAX / post.php中
<?php
include '../core/init.php';
if(isset($_POST['nt']) && isset($_POST['ntb'])){
mysql_query("INSERT INTO `post` (`myid`, `text`) VALUES ('".$_SESSION['id']."', '".mysql_real_escape_string($_POST['nt'])."')");
}
?>
以上是我的php发布数据,这是100%的工作:) 谢谢你的帮助
答案 0 :(得分:1)
我是这样做的:
function submitMe(val) {
jQuery(function($) {
$.ajax( {
url : "some_php_page.php?action="+val,
type : "GET",
success : function(data) {
alert ("works!"); //or use data string to show something else
}
});
});
}
然后我只需解析action
some_php_page.php?
这样做:
//function that get value from the request object and parses it to a variable; or stopps the script if no value is found
function getanyValue($param) {
if (isset($_GET[$param])) {
return $_GET[$param];
} else {
die('');
}
}
//get the action value
$action = getanyValue('action');
//add action to the database
$inserId = addAction($action);
public $dbserver = '';
public $dbusername = '';
public $dbpassword = '';
public $dbname = '';
function openDb() {
try {
$db = new PDO('mysql:host=' . $this->dbserver . ';dbname=' . $this->dbname . ';charset=utf8', '' . $this->dbusername . '', '' . $this->dbpassword . '');
} catch (PDOException $e) {
die("error, please try again");
}
return $db;
}
function addAction($action) {
$query = "INSERT INTO action_table (action_column) VALUES(?)";
$db = $this->openDb();
$stmt = $db->prepare($query);
$stmt->bindValue(1, $action, PDO::PARAM_STR);
$stmt->execute();
//get the action_table id from the database from the new insert
return $db->lastInsertId('id');
}
答案 1 :(得分:1)
您不一定需要AJAX才能在不重新加载的情况下提交表单。一个方便的技巧是简单地提交到一个隐藏的iframe:
<iframe style="display:none" name="submissiontarget" id="submissiontarget"></iframe>
<form method="post" target="submissiontarget" action = "../ajax/post.php" name="form">
<input id="nt" name="nt" type="text" />
<input type="submit" style="display:block;" name='ntb' id='ntb' class="Buttonchat" value="Post" />
</form>
<div>
<span class="error" style="display:none"> Please Enter Valid Data</span>
<span class="success" style="display:none"> Registration Successfully</span>
</div>
</form>
答案 2 :(得分:1)
问题在于您将'dataString'传递给PHP脚本。我所看到的'dataString'没有在任何地方定义。
所以你要做的是:
var dataString = 'nt=' + data + '&ntb=whateverntbshouldbeequalto';
这应该可以做到!
答案 3 :(得分:-1)
首先我会尝试打印出您的$_POST
以查看发送的内容。看来你的数据没有正确格式化。你正在测试你的php中的两个seprate变量,但似乎你试图只在ajax帖子的data
部分发送一个。