0

我有这个脚本,它必须将输入的数据发送到处理 php 页面

<button id="whatever" onclick="sendData()">Send</button>

        <script>

            function sendData() {
        var header = $('.header_input').val();
        var content = $('.content_input').val();

        $.ajax({
            type: 'POST',
            url: 'parts/post_functions.php',
            data: {
                header: header,
                content: content
                  }
               });
            }

        </script>

处理页面如下所示:

<?php

$con=mysqli_connect("localhost", "root", "shit", "data");
        // Check connection
        if (mysqli_connect_errno()) {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }

      $header = mysql_real_escape_string(htmlentities($_POST['header']));
      $content = mysql_real_escape_string(nl2br(htmlentities($_POST['content'])));

      $sql = mysqli_query($con,"INSERT INTO posts (Header, Content) VALUES 
        ('{$header}','{$content}')");
          mysqli_close($con);
?>

那么我如何正确接收数据,当然,将其发送到查询?

我对 AJAX 完全陌生,但我相信对于那些已经使用它一段时间的人来说,这是一个非常简单的问题。

4

3 回答 3

0

你可以做这样的事情

    <script>

    function sendData() {
    var header = $('.header_input').val();
    var content = $('.content_input').val();
    var dataString = 'header='+header'&content='+content;
    $.ajax({
        type: 'POST',
        url: 'parts/post_functions.php',
        data: dataString,
        success: function(data) {
          alert(data);
        } 
           });
        }

    </script>

现在在 PHP 页面中,您可以通过以下方式访问值

<?php
  $header = $_POST['header'];
  $content = $_POST['content'];
  // ANYTHING THAT YOU `echo` here will be alerted in jquery alert
?>

希望你能有所了解

于 2013-04-26T10:59:58.070 回答
0
$header = $mysqli->real_escape_string(htmlentities($_POST['header']));
$content = $mysqli->real_escape_string(nl2br(htmlentities($_POST['content'])));
于 2013-04-26T10:04:57.500 回答
0

用这个。

<button id="whatever" onclick="sendData()">Send</button>
<script>
    function sendData(){
        var header = jQuery('.header_input').val();
        var content = jQuery('.content_input').val();
        jQuery.ajax({
            type: 'POST',
            url: 'parts/post_functions.php',
            data: {
                header: header,
                content: content
            },
            success: function( data ) {
                alert(data);
            }
        });
    }
</script>

<?php

$con = mysqli_connect("localhost", "root", "shit", "data");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

  $header = mysqli_real_escape_string($con,htmlentities($_POST['header']));
  $content = mysqli_real_escape_string($con,nl2br(htmlentities($_POST['content'])));

  $sql = mysqli_query($con,"INSERT INTO posts (Header, Content) VALUES 
    ('$header','$content')");

  if($sql)
  {
      echo 'done';
  }
  else
  {
      echo 'error';
  }

mysqli_close($con);
?>
于 2013-04-26T10:17:45.220 回答