如何以Ajax形式传输php数据 [英] How to transmit php data in ajax form

查看:57
本文介绍了如何以Ajax形式传输php数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ajax函数中传输php数据时遇到问题. 我想要的是发送一个HTML表单,其中我有一个来自mysqli_fetch_array的php值.该值是帖子的编号,这是我的主键. 对javascript来说还很陌生,我真的不知道该怎么做. 这是我的onclick功能代码:

I have a problem with transmitting php data in ajax function. What I want is to send a html form, in which i have php value from a mysqli_fetch_array. The value is the number of the post, which is my primary key. Being new to javascript, I don't really know how to do this. Here is my onclick function code :

<script>
 function foo () {
  $.ajax({
    url:"vote.php?no_post=<?php $post ['no_post'] ?>", //what I'd like to do
    type: "POST", //request type
    success:function(result)
    {
     alert(result);
   }
 });
 }
</script>

在php页面中,我有这个访存数组,在这里我们关注名为"up"的按钮:

In the php page, I have this fetch array, here we focus on button named "up" :

    if(!isset($_POST['more']))
    {
    while($post = mysqli_fetch_array($res))
    {
    echo
        '<p>'.
            '<center>'.
            '<img src="image/'.$post['image_post'].'">'.
            '</center>'.
            '<br/>'.
            '<label>'.$post['desc_post'].'</label>'.
                            '<p>'.
                '<a id="darkBlue" href="account.php?no_post='.$post['no_post'].'">'.'@'.$post['login'].'</a>'.
                            '<p>'.
                '<label>'.$post['time'].'</label>'.
                            '<p>'.
                '<label>'.$post['vote_post'].' points'.'</label>'.
                    '<footer>';

    if (isset($_SESSION['login']))
    {
        echo        '<button class="btn btn-success" name="up" id="up" onclick="foo()">+</button>'.
                '&nbsp'.
                        '<button class="btn btn-danger" name="down" id="down">-</button>'.
                    '&nbsp'.
                            '<a href="commentaire.php?no_post='.$post['no_post'].'" class="btn btn-warning">Comment</a>'.
                    '<hr>';
    }

编辑***

最后是执行sql请求的我的php页面:

And finally my php page where the sql request is executed :

<?php 
 if (isset($_POST['up'])) // 2 buttons on the first html FORM
 {
$req="UPDATE post SET vote_post=vote_post+1
    WHERE no_post=".$_GET['no_post'].""; //picking up the value from my url
$res=mysqli_query($con,$req);
 }

else if (isset($_POST['down']))
{
$req2="UPDATE post SET vote_post=vote_post-1
     WHERE no_post=".$_GET['no_post'].""; //picking up the value from my URL
 $res2=mysqli_query($con,$req2);
 }

 else
     echo 'Error';
?>

感谢您的帮助.

推荐答案

您错过了下面一行中的echo.

You have missed echo in below line.

url:"vote.php?no_post=<?php echo $post['no_post'] ?>", //what I'd like to do

编辑

使用过POST方法后,应在data中传递它,例如:

As you have used POST method, it should be passed in data like:

$.ajax({
    url:"vote.php",
    type: "POST", //request type,
    data: {
       postCount : <?php echo $post['no_post']; ?>
    }
    success:function(result)
    {
     alert(result);
    }
 });

这篇关于如何以Ajax形式传输php数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆