2016-08-06 19 views
0

我有兩個文件與PHP和HTML。這是我的A.php文件中的基本信息阿賈克斯,只是發送價值不顯示頁

<div class="col-md-8"> 
    <input type="email" class="form-control" placeholder="Ingresar Número de Nota" id="idnote" name="idnote"> 
</div> 
<button type="button" class="btn btn-primary" id="notes">Enviar</button> 

當用戶點擊按鈕時,它調用js中的函數。

$(document).ready(function() { 
    var formData = { 
     'id'   : $('input[name=idnote]').val() 
    }; 
    $.ajax({ 
     type   : 'POST', 
     url   : '../consults/findnote.php', 
     data   : formData, 
     dataType : 'json', 
     encode : true 
    }) 
     .done(function(data) { 
      //Going to findnote.php and keeping formData 
      //This findnote.php contains a completely different design but I need the first value to make some changes for the user. 
     }) 

    }); 
}); 

的問題是,阿賈克斯後,我的網頁是不會findnote.php剛剛發送的值,我需要證明findnote.php 我知道如果我使用

event.preventDefault(); 

Ajax將阻止重新加載,但我沒有使用它。

有辦法做到這一點嗎? 如何在創建window.location後保留值? (因爲如果我在調用成功的電話後調用文件,我將失去該值) 我應該只用PHP進行嘗試嗎? (它的一個選項)

回答

3

爲什麼不做純HTML?

<form action="../consults/findnote.php" method="POST"> 
    <div class="col-md-8"> 
     <input type="email" class="form-control" placeholder="Ingresar Número de Nota" id="idnote" name="id"> 
    </div> 
    <button type="submit" class="btn btn-primary" id="notes">Enviar</button> 
</form> 

剛剛添加了表單標籤並編輯了它的輸入名稱以符合您的函數結果。

「的問題是,阿賈克斯後,我的網頁是不會findnote.php只是發送值」 這基本上就是Ajax是:)

使用Ajax,您就可以,如果你想要的結果附加到當前頁面(你沒有在你的代碼,只是發送數據)。

另一種帶有表單標記的解決方案,如上所示,將結果作爲新頁面載入,就像經典鏈接一樣。

0

你需要創建一個表單,並通過功能上提交,將形成之前運行提交和重定向

<form action="../consults/findnote.php" method="POST" onsubmit="myfunction()"> 
    <input type="email" class="form-control" placeholder="Ingresar Número de Nota" id="idnote" name="idnote"> 
</div> 
<button type="submit" class="btn btn-primary" id="notes">Enviar</button> 
</form> 

然後在jQuery的你可以做

function myfunction() { 

var formData = { 
     'id'   : $('input[name=idnote]').val() 
    }; 
    $.ajax({ 
     type   : 'POST', 
     url   : '../consults/findnote.php', 
     data   : formData, 
     dataType : 'json', 
     encode : true 
    }) 
     .done(function(data) { 
      //Going to findnote.php and keeping formData 
      //This findnote.php contains a completely different design but I need the first value to make some changes for the user. 
     }) 

    }); 
}