繁体   English   中英

如何使用jQuery在同一窗口中加载新的php页面,同时将数据发布到该页面

[英]How to load a new php page in same window, using jQuery, and at the same time post data to it

我有以下代码:1.文件1- test1.php

---- PHP CODE ----
<html>
-----HTML CODE ----
<script>
var someComplexObject; //This is a very large and complex JSON object 
                     //with lots of string and array data in it
  $.ajax({
    url: "test2.php",
    data: ({
      'data1': 3,
      'data2': 1,
      'data3': 1
    }),
    dataType: "json",
    success: function (data) {

    }
  });
</script>
</html>

2.文件2: test2.php

$somevar = json_decode($_POST['someComplexObject']);
--------PHP CODE TO PROCESS $somevar ----------

我想test2.php页面在某些事件后打开test1.php和对象objQuestionBank应该得到POST -ed(我不希望使用GET方法),以便我可以处理它test2.php

是否有一些现有的jQuery函数来实现这一点。 我需要维护某种客户端会话变量吗? 我不想使用Cookies

请指教

您可以仅使用表单将数据粘贴到要处理的页面上,如果您不想显示此表单,可以将其设置为隐藏...触发器可以在获取订单时使用javascript ,只需使用form.submit()发布数据即可。

在index.php里面的一个表单

<a href='poupdate.php?id=$data'>click to send the data</a>

在poupdate.php中,您需要像

isset($_GET[id])

$_GET[id] to get the data

而是使用此代码而不使用GET方法

 //page1.php

     <form  name="form1" method="post">
     <?php while($row = mysql_fetch_array($result)){
     echo'
        <div id="id1">'.$row['id1'].'</div>
        <div id="title" >'.$row['title'].'</div>
     <div id="editShow'.$row['id1'].'" class="td width4"  >
     <img src="image/edit.png" id="edit_show" onclick="EditLinkshow('.$row['id1'].');">
     </div>
     }';?>
     </form>
     <div  class="page2_container" ></div>



//script.js //this is the code that will handle the event

function EditLinkshow(id1) {
    $('.page2_container').slideDown(300);
    //get the data of the form based by id
    title=document.getElementById('title').value;
    id=document.getElementById('id1').value;
    //send the data to page 2
    param = "id1=" + id +"&title="+ title;
    $.post("page2.php", param, 
             function (data) {  
                    //display page2.php in the same window
                $('.page2_container').html(data);
            }               
          );
    }       

//page2.php
<?php
 $id = $_REQUEST['id1']; 
 $title = $_REQUEST['title']; 

 echo "<div>".$id;
 echo "\n"$title."<\div>";
?>

暂无
暂无

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

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