2014-12-06 83 views
1

我想提交兩個表單字段值到一個PHP函數,在同一頁上。 該功能完美地手動填充這兩個值。簡單的表單提交到PHP函數失敗

<?PHP // Works as expected 
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45); 
?> 

在窗體上,我必須缺少一些東西,或者我有一個語法錯誤。網頁無法顯示。我發現下面的例子在這裏:A 1 Year old Stackoverflow post

<?php 
if(isset($_GET['submit'])) { 
    $val1 = htmlentities($_GET['val1']); 
    $val2 = htmlentities($_GET['val2']); 

    $result = sav_newval_of($val1, $val2); 
} 
?> 

<?php if(isset($result)) echo $result; //print the result of the form ?> 

<form action="" method="get"> 
    Input position a-s: 
    <input type="text" name="val1" id="val1"></input> 
    <br></br> 
    Input quantity value: 
    <input type="text" name="val2" id="val2"></input> 
    <br></br> 

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

難道是我的窗體上放置代碼? 任何建議表示讚賞。

+0

而語法錯誤是? – Rizier123 2014-12-06 20:58:16

回答

1

您需要命名您的提交按鈕,或者檢查是否有別的東西在你的if(isset($_GET['submit']))部分:

<form action="" method="get"> 
    Input position a-s: 
    <input type="text" name="val1" id="val1" /> 
    <br></br> 
    Input quantity value: 
    <input type="text" name="val2" id="val2" /> 
    <br></br> 

    <input type="submit" name="submit" value="send" /> 
</form> 

還是保持同樣的形式,但改變PHP來:

<?php 
if(isset($_GET['val1']) || isset($_GET['val2'])) { 
    $val1 = htmlentities($_GET['val1']); 
    $val2 = htmlentities($_GET['val2']); 

    $result = sav_newval_of($val1, $val2); 
} 
?> 
+0

謝謝Rasclatt。這兩個解決方案解決了它。 – Ashton 2014-12-06 21:26:33

0

你可以隱藏你字段:

<input type='hidden' name='action' value='add' > 

,並通過使用形成有被isset功能檢查PHP已提交。