PHP中的Ajax调用与发布 [英] Ajax Call with Post in PHP

查看:59
本文介绍了PHP中的Ajax调用与发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!doctype html>
<html>
<head>
  <title>jQuery Tagit Demo Page (HTML)</title>
  <script src="demo/js/jquery.1.7.2.min.js"></script>
  <script src="demo/js/jquery-ui.1.8.20.min.js"></script>
  <script src="js/tagit.js"></script>


  <link rel="stylesheet" type="text/css" href="css/tagit-stylish-yellow.css">

  <script>


    $(document).ready(function () {


    var list = new Array();
    var availableTags = [];

     $('#demo2').tagit({tagSource:availableTags});


     $('#demo2GetTags').click(function () {
        showTags($('#demo2').tagit('tags'))
      });

     /*
    $('li[data-value]').each(function(){
        alert($(this).data("value"));
    });*/

    $('#demo2').click(function(){
        $.ajax({
            url: "demo3.php",
            type: "POST",
            data: { items:list.join("::") },
            success: alert("OK")
        });
    });

    function showTags(tags) {


        console.log(tags);
        var string = "";
        for (var i in tags){
          string += tags[i].value+" ";
         }
          var list = string.split(" ");
          //The last element of the array contains " "
          list.pop();   
        }
    });
  </script>
</head>
<body>

<div id="wrap">
    <?php 

        $lis = $_POST['items'];
        $liarray = explode("::", $lis);
        print_r($liarray);

    ?>
<div class="box">
  <div class="note">
    You can manually specify tags in your markup by adding <em>list items</em> to the unordered list!
  </div>

    <ul id="demo2" data-name="demo2">
        <li data-value="here">here</li>
        <li data-value="are">are</li>
        <li data-value="some...">some</li>
        <!-- notice that this tag is setting a different value :) -->
        <li data-value="initial">initial</li>
        <li data-value="tags">tags</li>
    </ul>

  <div class="buttons">
    <button id="demo2GetTags" value="Get Tags">Get Tags</button>
    <button id="demo2ResetTags" value="Reset Tags">Reset Tags</button>
    <button id="view-tags">View Tags on the console </button>
  </div>
</div>

</div>
<script>
</script>
</body>
</html>

这段代码将只传输dostuff.php中的项目列表,但是当我尝试在PHP上使用print_r时,什么也不会出现.为什么呢?

This code will just transfer the list of items in the dostuff.php but when I try to print_r it on PHP nothing won't come out. why is that?

我正在此行上执行ajax请求

I am doing an ajax request on this line

$('#demo2').click(function(){
            $.ajax({
                url: "demo3.php",
                type: "POST",
                data: { items:list.join("::") },
                success: alert("OK")
            });
        });

和PHP中的代码

<?php 

        $lis = $_POST['items'];
        $liarray = explode("::", $lis);
        print_r($liarray);

    ?>

推荐答案

鉴于有限的信息,这只是黑暗中的一枪,但看来您期望发生某事从服务器发回的数据..但实际上您什么也没做.成功后,您将显示警报...什么都没有.

This is just a shot in the dark, given the limited information, but it would appear that you're expecting something to happen with the data sent back from the server.. but you literally do nothing with it. On success, you have it display an alert... and nothing else.

尝试将您的成功条目更改为以下内容:

Try changing your success entry to the following:

success: function(data) {
    $("#wrap").html(data);
}

这将用POST请求中的数据填充div.它根本不显示的原因...,您没有在加载当前正在执行的页面时使用print_r实际上需要回显任何内容的数据.

This will fill the div with the data from the POST request. The reason it shows up as nothing as it is..., you aren't loading the currently executing page with the data needed for the print_r to actually echo anything.

编辑:如何将值插入数据库;

Edit: How to insert values into database;

如今,数据库交互是通过自定义包装程序或完成的php Data Object ,也称为PDO,与不推荐使用的mysql_*函数相反.

Database interaction now-a-days is done with either custom wrappers, or the php Data Object, also referred to as PDO, as opposed to the deprecated mysql_* functions.

首先,准备数据库对象,类似于在上述不推荐使用的函数中完成连接的方式:

First, you prepare your database object, similar to how a connection is done in the aforementioned deprecated functions:

$dbh = new PDO("mysql:host=hostname;dbname=database", $username, $password);

然后,您可以开始交互,准备查询语句.

Then, you can begin interaction, preparing a query statement..

$stmt = $dbh->prepare("UPDATE table_name SET column1 = :column1 WHERE id = :id");

在所述语句中绑定参数.

Binding a parameter in said statement..

$stmt->bindParam(':column1', $column1);
$stmt->bindParam(':id', $id);
$id = $_POST['id'];

最后执行查询:

try {
    $stmt->execute();
}
catch (Exception $e) {
    echo $e;
}

PDO自动转义先前语句中绑定的任何字符串,从而使其免受SQL注入攻击的影响,并加快了多次执行的过程.请看以下示例:

PDO auto-escapes any strings bound in the prior statements, making it save from SQL-injection attacks, and it speeds up the process of multiple executions. Take the following example:

foreach ($_POST as $id) {
    $stmt->execute();
}

由于id参数已经绑定到$id,因此您要做的就是更改$id并执行查询.

Since the id parameter is already bound to $id, all you have to do is change $id and execute the query.

这篇关于PHP中的Ajax调用与发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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