简体   繁体   English

发送jQuery值到PHP文件?

[英]Send Jquery value to php file?

I have a file called post-blog.php this is collecting the data and storing it in a variable called page. 我有一个名为post-blog.php的文件,它正在收集数据并将其存储在一个名为page的变量中。

var page = title + content+ image + datetime +categories;

I'm then sending this off to publish.php, if sent correctly send the user to that page. 然后,如果正确发送,则将其发送到publish.php,将用户引导至该页面。

  $.ajax({
        type:   'POST',
        cache:    false,
        url:      'publish.php',
        data:     page,
        success:  function( page ) {

          alert(page);
          window.location.href = "publish.php";

        }

      });

      return false;

This I my php script which is on the 2nd page (publish.php). 这是我的PHP脚本,位于第二页(publish.php)。 How can I store the jquery data into php varible. 如何将jquery数据存储到php变量中。 So that I can display them on publish page. 这样我就可以在发布页面上显示它们。

<?php
if ( $_POST['page'] == true )
{
   echo "Inserted";
}
?>

you can pass your variable like this 您可以像这样传递变量

var page = {
    title : title,
    content : content,
    image : image ,
    datetime : datetime ,
    categories : categories 
}

and then in php 然后在PHP

<?php 
if(isset($_POST["title"])){  // you can use content , image ,... same way
    $title = $_POST["title"];
    echo $title;
}
?>

and in ajax 并在ajax中

success:  function( response) { //response is the data which returned from php .. for sure (page) will work but just to be clear
          alert(response);
          //window.location.href = "publish.php";
        }

if you want to use page as an array you can use 如果要使用页面作为数组,可以使用

var page = [];
page.push(title);
page.push(content);
.......

then in php use 然后在php中使用

<?php
if (isset($_POST['page'])) {
  print_r($_POST['page']);
}
?>

You can send this by setting data as follow: 您可以通过如下设置数据来发送它:

 data: {'page': page},

jQuery has some examples on the $.ajax and $.post page: jQuery在$ .ajax和$ .post页面上有一些示例:

http://api.jquery.com/jquery.ajax/ http://api.jquery.com/jquery.ajax/

http://api.jquery.com/jquery.post/ http://api.jquery.com/jquery.post/

You can send data using query string variable in url like this 您可以使用url中的查询字符串变量发送数据,如下所示

$.ajax({
        type:   'POST',
        cache:    false,
        url:      'publish.php',
        data:     page,
        success:  function( page ) {
          alert(page);
          window.location.href = "publish.php" + "?page=" + page ;
        }
      });

  return false;

Then on publishing page you could get data using following code 然后在发布页面上,您可以使用以下代码获取数据

if (isset($_POST['page']) &&  $_POST['page'] != '') {
 var_dump($_POST['page']);
 echo 'inserted';}
?>

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

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