簡體   English   中英

將php變量添加到ajax調用

[英]Add php variable to ajax call

我在第1頁上有一個表單,我想將其變量解析為第2頁上的ajax調用。 Ajax調用由on onload事件觸發。

場景:

第1頁

<form id="form1"method="GET" action="page2">//send the variables to page 2
<input type="text" name="Place" value="city">
<input type="text" name="Type" value="room">
<input type="submit"></form>

第2頁

<form name="myform2" id="myform2" method="GET">
<input type="text" name="Place" value="<?php echo $_GET[Place] ?>">//
<input type="text" name="type" value="<?php echo $_GET[Type] ?>">
<button id="submit2"type="submit" value="submit2" name="submit2" onclick="return ss()">

js1

$(document).ready(function(){ // load file.php on document ready     
     function sssssss2(page){
          var form2 = document.myform2;
          var dataString1 = $(form2).serialize() + '&page=' + page;
         ({
         type: "GET",
         url: "file.php",//
         data: dataString1,
         success: function(ccc){
             $("#search_results").html(ccc);
        }});}
    sssssss2(1) ;
    $('#search_results .pagination li.active').live('click',function(){
        var page = $(this).attr('p');
        sssssss2(page);                 
    });  
});

js2

function sss() {//serialize the form each time submitted.
     var form2 = document.myform2;
     var dataString1 = $(form2).serialize();
     $.ajax({
         type:'GET',
         url: "file.php",
         cache: false,
         data: dataString1,
         success: function(data){
             $('#search_results').html(data);
         }
    });
    return false; 
}

問題是file.php不能使用變量“ city”和“ room” 。我想在第一次加載page2時將2變量解析為file.php。

為何在加載文件page2時解析這些變量?

不應將Place and Type用引號括起來

<input type="text" name="Place" value="<?php echo $_GET['Place'] ?>">//
<input type="text" name="type" value="<?php echo $_GET['Type'] ?>">

在這里,您會在'method'和'type'屬性之前錯過空格:

<form id="form1"method="GET" action="page2">
<button id="submit2"type="submit" value="submit2" name="submit2" onclick="return ss()">

在這里,您會錯過一個動作屬性:

<form name="myform2" id="myform2" method="GET">

這是錯誤的²,因為您錯過了; 然后在索引中放入常量而不是字符串:

<?php echo $_GET[Type] ?>

正確:

<?php echo $_GET['Type']; ?>

在js1中,您錯過了$.ajax({第4行上的({ (也不正確。

了解有效的HTML,適當的PHP和我建議的其他一些基礎知識,它不能以這種方式工作,尤其是不能跨平台兼容。

您不能將100個教程中的腳本一起復制,並希望它們能正常工作,您必須了解每個命令和代碼行及其作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM