2014-06-27 59 views
0

我想分配一個文本框值到一個PHP變量現在問題是我想要一個按鈕爲兩個不同的頁面工作,即如果我在文本框中輸入'a'如果我在文本框'b'中寫入,它應該重定向到'a.php'頁面,它應該重定向到'b.php'。一個文本框一個按鈕在HTML中的兩個不同的頁面

所以一個文本框,一個按鈕兩個不同的頁面。

代碼:

<html><head> 
<meta charset="UTF-8" /> 
<script> 
function submitForm(action) 
{ 
    document.getElementById('a').action = action; 
    document.getElementById('a').submit(); 
} 
function submitForm1(action) 
{ 
    document.getElementById('b').action = action; 
    document.getElementById('b').submit(); 
} 

</script> 
</head> 
<body > 



<h3><font face="verdana" size="3"><b>Enter Text:</b></h3> 

<input type="text" align="right" style="font-size:15pt;height:32px;"><br><br> 

    <form action="b.php" name="b" id="b" method="post"> 
<form action="a.php" name="a" id="a" method="post"> 

<input type="submit" onclick="submitForm('a.php')" value="TRY" name="a"> 
<input type="button" onclick="submitForm1('b.php')" value="TRY" name="b"> 



    </form> 
    </form> 


</body> 
</html> 

回答

1

你可以改變行動的onsubmit基於裏面輸入文本。

<html> 
<head> 
    <script type="text/javascript"> 
     function onsubmit_handler(){ 
      var myForm = document.getElementById('myForm'); 
      var data = document.getElementById('data').value; 
      if(data == "a") 
       myForm.setAttribute('action', 'a.php'); 
      else if(data == "b") 
       myForm.setAttribute('action', 'b.php'); 
      else 
       myForm.setAttribute('action', 'error.php'); 
     } 
    </script> 
</head> 
<body> 
    <h3>Enter Text:</h3> 
    <form id="myForm" method="post" onsubmit="onsubmit_handler()"> 
     <input type="text" id="data" name="data" value=""> 
     <input type="submit" value="Post"> 
    </form> 
</body> 
</html> 

測試代碼在這裏:http://jsfiddle.net/Eg9S4/

+0

非常感謝你的工作,但可以請你告訴我如何將這個值分配給php變量,因爲我嘗試了下面的代碼,但這不起作用。代碼:$ q = $ _ POST ['data']; –

+0

它的工作....謝謝 –

0

的問題看起來是您使用的getElementById但輸入的元素沒有一個ID(他們只有一個名字)。

我還建議附加一個onSubmit事件到窗體並從按鈕中刪除onClick事件。

編輯:仔細查看代碼後,我看到有一些其他問題可能會妨礙操作。最值得注意的是你不能嵌套窗體標籤。還有一些其他的CSS和驗證問題。

下面是一些工作代碼:

測試頁

function SubmitForm(el) { 

     // Get the form element so that we can set the action later 
     var form = document.getElementById('theForm'); 


     // Set the action for the form based on which button was clicked 
     if (el.id == "A") 
      form.action = "a.php"; 

     if (el.id == "B") 
      form.action = "b.php"; 

     // You dont need to submit the form. It's alreasdy happening, so there is no need for an explicit call. 
    } 

</script> 

<h3 style="font-family: Verdana; font-weight: bold;">Enter Text:</h3> 

<input type="text" style="font-size:15pt;height:32px;"><br><br> 

<form method="post" onsubmit="SubmitForm();" action="fake.html" id="theForm"> 
    <input type="submit" id="A" value="A" onclick="SubmitForm(this);"> 
    <input type="submit" id="B" value="B" onclick="SubmitForm(this);"> 
</form> 

+0

啊。我正在走下「兩個提交按鈕」的路線,並沒有意識到你想要一個文本框和一個按鈕。如果是這種情況,請使用上面的Thanasis'代碼! –

+0

非常感謝你的工作,但可以請你告訴我如何將這個值分配給php變量,因爲我已經嘗試過,但這不起作用。代碼:$ q ='$ _ POST ['data']'; –

+0

我以前的答案沒有輸入ID,但假設它是'firstName',那麼PHP代碼將是'$ q = $ _ POST [「firstName」];' –

0

使用此代碼好友

<html><head> 
<meta charset="UTF-8" /> 
<script> 
function submitForm() 
{ 
    var link=document.getElementById('a'); 
var str1 = "a.php"; 
var n = str1.localeCompare(link); 

if(n==1) 
{ 
    window.open("main.html"); 

} 

else if(n==-1) 
{ 
    window.open("index.html"); 
} 
} 



</script> 
</head> 
<body > 



<h3><font face="verdana" size="3"><b>Enter Text:</b></h3> 

<input type="text" align="right" style="font-size:15pt;height:32px;"><br><br> 


<input type="submit" onclick="submitForm()" value="TRY" name="a"> 





</form> 
</form> 


</body> 
</html> 
+0

不會提交表單數據。 –

相關問題