2017-03-21 29 views
1

我在使用php中的submit按鈕來進行一些簡單的操作。這是我要運行的代碼(它是一個網站,你打分的事情,如果這能幫助任何):在php中POST和提交按鈕的麻煩

<form method="POST"> 
<input type="radio" name="person" value="1" /> 
<input type="radio" name="person" value="2" /> 
<input type="submit" name="submit" /> 
</form> 

<?php; 
    $_variable1 = 1400 
    function ratePerson($person) 
    { 
     $_variable1+1 
     echo $_variable1 
    } 


    if (isset($_POST['submit'])); 
    {  
     $person = $_post['person']; 
     echo $person; 

     ratePerson($person) 
    } 


echo $_variable1  
?> 

所以,當我運行這個提交按鈕和兩個單選按鈕出現,我可以點擊一個他們並點擊提交,只是當我點擊一個按鈕並點擊提交時,沒有任何反應,沒有打印的值(回聲),我不知道+1是否工作,這是一團糟。我沒有在php中做過很多,所以請原諒我的無知。

我從朋友那裏得到了這段代碼,所以如果你想建議你自己的解決方案,那麼就去吧。

感謝您的幫助!

+0

我想你想''_variable1 + = 1',而不是'$ _variable1 + 1' – Jerfov2

+0

可能是因爲PHP代碼在合成上是不正確的。看看你rerror日誌或添加[錯誤報告](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025)到您的頂部 file(s)_while testing_正好在您打開PHP標記後,例如 '<?php error_reporting(E_ALL); ini_set('display_errors',1);' – RiggsFolly

+0

這很瞭解,但它仍然不會打印下一頁上的值。 – SamtheMan

回答

0

您的代碼缺少分號來結束命令。還有其他一些錯誤就像增加。

<?php 
$_variable1 = 1400; // note the added semicolon 

// pass $_variable1 by reference so incrementation is stored to the variable 
function ratePerson(&$_variable1) { 
    $_variable1 += 1; // increment correctly 
} 

if (isset($_POST['submit'])) { // no semicolon necessary 
    $person = $_POST['person']; // _POST instead of _post 
    echo $person; // missing semicolon 
    ratePerson($_variable1); // missing semicolon 
    echo $_variable1; // missing semicolon 
} 
echo $_variable1; // missing semicolon 
?> 
0

你有幾個錯誤的腳本:

<form method="POST"> 
    <input type="radio" name="person" value="1" /> 
    <input type="radio" name="person" value="2" /> 
    <input type="submit" name="submit" /> 
    </form> 

    <?php; 
     $_variable1 = 1400 
     function ratePerson($person) 
     { 
      global $_variable1; // global variable precision 

      $_variable1++; // increment or $_variable1 +=1; 
      echo $_variable1; 
     } 


     if (isset($_POST['submit'])) 
     {  
      $person = $_post['person']; 
      echo $person; 
     ratePerson($person) 
    } 
     echo $_variable1  
    ?> 

注:我不知道爲什麼你提供$person作爲參數傳遞給你的函數,因爲它不使用ratePerson($person) 希望它有幫助

0

我不確定你想要完成什麼,但是這個代碼至少會在表單提交時提供一些輸出:

<form method="POST"> 
1: <input type="radio" name="person" value="1" /><br /> 
2: <input type="radio" name="person" value="2" /><br /> 
<input name="submit" type="submit" value="Submit"> 
</form> 

<?php 
    function ratePerson($person) { 
    $_variable1 = 1400; 
    $_variable1+=1; 
    return($_variable1); 
    } 

    if (isset($_POST['submit'])) {  
     $person = $_POST['person']; 
     echo "Person: " . $person . "<br />"; 
     $_variable1 = ratePerson($person); 
     echo "Variable1: " . $_variable1 . "<br />"; 
    } 
?>