伊莉討論區

標題: PHP 經過JS函式發送表單 頁面如何跳轉 [打印本頁]

作者: 新兵戰士35028    時間: 2016-12-28 11:03 PM     標題: PHP 經過JS函式發送表單 頁面如何跳轉

請問各位高手為什麼在文字框輸入1 按下提交後

頁面都不會跳到test.php

昨天測試了很久有成功 今天早上不知道為啥就又不行了

我想要值能傳到test.php且頁面會跳過去

感謝解答!!

<form name="form1" id="form1" method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
<label for="textfield">name:</label>
<input type="text" name="acc" id="textfield">

<input type="submit">
</form>


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {

$a=$_POST["acc"];

if($a=="1")
{
echo "$a";
echo "<script>fun_2();</script> ";
}
}
?>

<script type="text/javascript">
function fun_2(){
document.getElementById("form1").action="test.php";
document.getElementById("form1").submit();
}
</script>

作者: theloserbm    時間: 2016-12-29 12:04 PM

本帖最後由 theloserbm 於 2016-12-29 12:17 PM 編輯

一定要用JS嗎? 還是不用也可以?

不用的話就這樣
page1:
  1. <form name="form1" id="form1" method="post" action="test.php">
  2. <label for="textfield">name:</label>
  3. <input type="text" name="acc" id="textfield">

  4. <button type="submit">submit</button>
  5. </form>
複製代碼

test.php:
  1. echo "success! name="+$_POST['acc'];
複製代碼

要用JS的話
page1:
  1. <form name="form1" id="form1" method="post" action="test.php">
  2. <label for="textfield">name:</label>
  3. <input type="text" name="acc" id="textfield">

  4. <button type="button" onclick="fun_2()">submit</button>
  5. </form>

  6. <script>
  7. function fun_2(){
  8.      document.getElementById("form1").submit();
  9. }
  10. </script>
複製代碼

test.php:
  1. echo "success! name="+$_POST['acc'];
複製代碼


作者: 新兵戰士35028    時間: 2016-12-29 12:45 PM

感謝大大回復

我想要的功能是在a.php的表單按下submit後
表單會發送給a.php 驗證使用者輸入的內容
確認無誤之後在傳給b.php


而不是按下submit後直接傳送到b.php

作者: notice3    時間: 2017-1-7 06:06 PM

新兵戰士35028 發表於 2016-12-29 12:45 PM
感謝大大回復

我想要的功能是在a.php的表單按下submit後

首先, 你要驗証的數據類型是甚麼, 如果驗証可以在 client 那邊進行, 當然用 js 去處理.
如果需要在 server 那邊進行, 就要用 php 處理.

當然, server 類型的, 也可以用 js 處理, 當按下 submit 後, ajax 扔給另一個 php 去做驗証, 回傳無誤後就可以用 js redirect user 去 b.php
作者: jehovahcloud    時間: 2017-2-15 08:25 PM

可以試試看在 a.php 檢查完後,利用 header 的 location redirect 轉到 b.php。
記得要一併輸出 307 Temporary Redirect header 給瀏覽器,301 或 302 在某些環境下會把你的 method 改成用 get。

不過你這個方法在 a.php 裡輸出一些 b.php 裡面可能要做一些驗證,不然直接對 b.php 送資料就可以避開驗證了。至於驗證的東西,網路上有很多,我個人是使用 JWT
作者: alextang1030    時間: 2017-2-16 09:33 PM

window.location.href = "url"

這句就行了
作者: cswordli    時間: 2017-2-19 05:57 AM

本帖最後由 cswordli 於 2017-2-19 05:59 AM 編輯
新兵戰士35028 發表於 2016-12-29 12:45 PM
感謝大大回復

我想要的功能是在a.php的表單按下submit後

提供你參考:
◎方法1:使用php的header()。【請看method1.png】
◎方法2:如果要用js跳轉就參考alextang1030大大的用法。【請看method2.png】

  路過回一下,希望幫得上你理清php跟js之間的關係~
  * 改得程式碼看起來還ok,但是不讓我存檔...只好用圖片上傳惹!! *



作者: play4140    時間: 2017-10-19 06:40 PM

本帖最後由 play4140 於 2017-10-19 06:42 PM 編輯

你原本的問題應該是 php output buffer 的問題,然後總有機會你會用到 submit 跳轉頁面

表單 name='form1' 的話
  1. <script>
  2. document.form1.action="test.php";
  3. document.form1.submit();
  4. </script>
複製代碼
就送出表單了,不過你寫的也行,這樣少打幾個字。
作者: albertlin001    時間: 2017-11-16 03:28 PM

提示: 作者被禁止或刪除 內容自動屏蔽




歡迎光臨 伊莉討論區 (http://www879.eyny.com/) Powered by Discuz!