繁体   English   中英

jQuery Ajax将html数据发布到php并获得回复

[英]JQuery Ajax post html data to php and get a reply

我想发布如下所示的跨度数据,但是单击时什么也没有发生。 我在以下范围内发布了“ 4”。 我不怎么发布这个或语法。 跨度。

<span style="color:#B00" id="PidClick">4</span>

使用这个。

$('#PidClick').click(function() {
console.log('PidClick event:')
   $.ajax({
    dataType:'json',
    data: {data:Pid},
    type:"POST",
    url:"test.php"
    }).done(function(reply){
    console.log('reply is' + reply) 
        alert (reply.name)

})
});

和PHP的。 ////也是我的“。$ Pid”。 可变对吗? 我不确定这是否正确。

<?php
require_once 'db_conx.php';
if(isset($_POST["Pid"])) {
$id = preg_replace ('#[^0-9 ]#i', '', $_POST['Pid']);
$Result = mysql_query("SELECT * FROM profiles WHERE Pid = '.$Pid.' ") or die (mysql_error());
while($row = mysql_fetch_array($Result)){
$result = array();
$result['pemail'] = $row['pemail'];
$result['name'] = $row['name'];
echo json_encode($result);
}
}
mysql_close($con);
?>

Console Errors:- 
PidClick event: 'Type error: Type error'

用户.html()获取内部html

因此,添加$('#PidClick').html();

$('#PidClick').click(function() {
  var Pid = $('#PidClick').html();
  Pid = parseInt(Pid);
   $.ajax({
data: 'Pid='+Pid,
type:"POST",
url:"test.php"
}).done(function(reply){
console.log('reply is' + reply) 
    alert (reply.name)

})
});

使用$('#PidClick').text()$('#PidClick').html()获取跨度的内部文本。

$('#PidClick').click(function() {
  var Pid = $('#PidClick').html();
  ..............
});

您可以分配Pid值吗?

var Pid = $(this).html();

首先,一旦我们为Pid分配了一个值,就需要像其他人一样为Pid分配一个值。 由于您要在PHP文件中尝试获取一个名为Pid的变量,因此我们希望这样传递它。

这是更新的JQuery:

jQuery(document).ready(function($) {
    $('#PidClick').click(function() {
        var Pid = $(this).text();
        $.ajax({
            type: 'POST',
            url: 'test.php',
            dataType: 'json',
            data: {
                Pid: Pid
            },
            success : function(data){
                console.log('reply is' + data.reply);
                alert(data.reply);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                alert('There was an error.');
            }
        });
    });
});

其次,我编写了一个小测试用例PHP文件。 只是为了确保一切正常。 再一次,您将需要确保在来回传递变量时保持相同。 在上面的jquery示例中,您会注意到我使用“ data.reply”检索信息,在下面的PHP文件中,我将数据分配给了一个也称为“ reply”的变量。 我还提供了一些保护措施,以确保只能通过ajax请求访问该文件。

<?php

// only work if requested with ajax
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
        strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {

    if(isset($_POST['Pid'])) {
        $return['reply'] = 'The input was '.$_POST['Pid'];
    }
    else {
        $return['reply'] = 'No input detected';
    }

    echo json_encode($return);

} else {
    die('direct access is forbidden');
}
?>

这是我使用的html文件:

<!DOCTYPE html>
<!--[if lt IE 7]>      <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>         <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>         <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title>pid example</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width">

        <script src="jquery.js"></script>
        <script src="ajax.js"></script>
    </head>
    <body>
        <span style="color:#B00" id="PidClick">4</span>
    </body>
</html>

另外,在尝试执行此示例时,请确保所有文件权限和路径均正确。 在这种情况下,所有文件应位于同一目录中。

$('#PidClick').click(function() {
   var Pid = $('#PidClick').text();
    $.ajax({
       dataType:'json',
       data: 'Pid='+Pid,
       type:"POST",
       url:"test.php"
  }).done(function(reply){
    console.log('reply is' + reply) 
    alert (reply.name)
  })
});

js提琴手的例子

暂无
暂无

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

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