0

我在我的网站上的当前代码如下,我将很快将该代码转移到我的 profile.php 中:)

测试.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&gt;

上面的代码是 ajax 和 html,当我有 ajax 时并不能真正工作,但是当我只有 html 和 php 时,它会发布到数据库中,我知道为什么 :(

../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% 有效 :) 谢谢你的帮助

4

4 回答 4

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');
}
于 2012-12-01T20:07:41.273 回答
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>
于 2012-12-01T20:08:56.297 回答
1

问题是您将“dataString”传递给 PHP 脚本。我所看到的“dataString”没有在任何地方定义。

所以你要做的是:

var dataString = 'nt=' + data + '&ntb=whateverntbshouldbeequalto';

这应该够了吧!

于 2012-12-01T20:10:34.160 回答
-1

首先,我会尝试打印出您的$_POST内容以查看正在发送的内容。看来您的数据格式不正确。您正在测试您的 php 中的两个单独的变量,但似乎您试图data在 ajax 帖子的您的部分中只发送一个。

于 2012-12-01T20:11:24.377 回答