查看完整版本: PHP 經過JS函式發送表單 頁面如何跳轉
頁: [1]

新兵戰士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>
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div><div></div>

theloserbm 發表於 2016-12-29 12:04 PM

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

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

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

<button type="submit">submit</button>
</form>
test.php:
echo "success! name="+$_POST['acc'];
要用JS的話
page1:
<form name="form1" id="form1" method="post" action="test.php">
<label for="textfield">name:</label>
<input type="text" name="acc" id="textfield">

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

<script>
function fun_2(){
     document.getElementById("form1").submit();
}
</script>
test.php:
echo "success! name="+$_POST['acc'];
...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

新兵戰士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 static/image/common/back.gif
感謝大大回復

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


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

當然, server 類型的, 也可以用 js 處理, 當按下 submit 後, ajax 扔給另一個 php 去做驗証, 回傳無誤後就可以用 js redirect user 去 b.php...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

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<br><br><br><br><br><div></div>

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 static/image/common/back.gif
感謝大大回復

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

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

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


...<div class='locked'><em>瀏覽完整內容,請先 <a href='member.php?mod=register'>註冊</a> 或 <a href='javascript:;' onclick="lsSubmit()">登入會員</a></em></div>

play4140 發表於 2017-10-19 06:40 PM

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

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

表單 name='form1' 的話
<script>
document.form1.action="test.php";
document.form1.submit();
</script>
就送出表單了,不過你寫的也行,這樣少打幾個字。

albertlin001 發表於 2017-11-16 03:28 PM

你的 fun_2 function裏為什麼不直接用javascript轉址:

window.location.href='/test.php'

加這一行直接轉址就好了,不用去設form 的 action。
頁: [1]