2013-11-22 78 views
0

我有一個表格頁面1我想解析它的變量,以調用AJAX調用page 2。 ajax調用由onload事件觸發。將php變量添加到ajax調用

場景:

第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好好嘗試採取變量「城市」和「房間」 .I想解析2變量file.php當第2頁加載第一次。

Hw解析文檔加載page2上的那些變量?

+0

它在js1中缺少'$ .ajax'。應該是'$ _GET ['Place']','$ _GET ['Type']' – Don

回答

0

不應對和類型在引號像

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

你們面前「方法」錯過blankspace以及在「類型」屬性:

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

在這裏,你會錯過的action屬性:

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

這是錯誤的²因爲你錯過了;你把一個常數我在指數的字符串的nstead:

<?php echo $_GET[Type] ?> 

正確:

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

在JS1你錯過$.ajax({ +上線4 ({是不正確的無論是。

瞭解valid HTML, proper PHP以及一些我建議的基礎知識,它不能以這種方式工作,尤其是不能跨平臺兼容。

您不能從100個教程中複製腳本,並希望它們能夠工作,您必須知道每個命令和代碼行以及它的功能。