2016-07-27 42 views
-4

在我的表單中,有一些字段,我想在提交表單前檢查。如果有錯誤,表單將不會被提交,並且用戶會收到錯誤提示。如何檢查PHP中的表單提交?

爲此,我試圖在php中提交表單提交的事件。我應該得到一個警告文本「已提交」。但是,我沒有。我在做什麼錯誤?

<?php 
if(isset($_POST['submit'])) 
    echo "<span>Submitted!</span>"; 
    sleep(2); 
?> 

<form action="next_page.php" method=post> 
*Other fields beside input* 
<input type="submit" name="submit" value="Submit form"> <br> 
</form> 
+0

HTML5'pattern'屬性或者您可以使用JS。哦,'提交'類型! – user5173426

+1

你是否看到一個名稱爲「submit」的表單元素?沒有?我也沒有。那麼你怎麼能期望在你的代碼中看到它呢? –

+0

它也看起來像你混合javaScript和PHP(除非你寫了你自己的alrt()函數,但基於這個問題的簡單性,我非常懷疑這一點)。 –

回答

3

alert()是不是由PHP提供的功能,所以它會報錯。

你似乎混淆了它與JavaScript。

要從PHP輸出文本,你需要echo它或把它放在<?php ... >?塊之外。要運行JavaScript,您需要一個<script>元素。

<?php 
if(isset($_POST['submit'])) { 
?> 
    <script> alert("Submitted!"); </script> 
<?php 
} 
?> 

<form action="next_page.php" method=post> 
    <!-- You also need to have a form control that matches the test you are doing --> 
    <button name="submit" value="X">Submit</button> 
</form> 
1

您的表單缺少submit類型。 您還需要echo提醒框,因爲alert()不是PHP函數。

<?php if(isset($_POST['submit'])) 
    echo"<script>alert('Submitted!');</script>"; ?> 

<form action="next_page.php" method=post> 
<input type = "submit" name = "submit" value = "Sub"/> 
</form> 
+0

沒有它沒有。檢查編輯 – RiggsFolly

+0

@RiggsFolly啊,我現在看到編輯過的! ':-)' – user5173426

0

有很多方法可以完成您想要完成的任務。你在使用任何一種框架嗎?它可能是爲此而構建的。通常依靠標準函數或庫進行驗證並不是一個壞主意。

如果沒有,你將不得不自己做,並決定什麼時候你想它是服務器端(PHP)或用戶端(JavaScript)。無論你應該驗證它在服務器端捕獲任何惡意提交。

如果我們以您的示例爲例,它可能看起來像這樣用於服務器端PHP驗證,它將結果反饋給用戶。當心,這是真的,真的很沒有安全感,因爲它的使用$ _GET可能被修改等

<?php 
$username = ''; 
$warning = ''; 

if(isset($_POST['submit'])){ 
    if(!empty($_POST['username']) && strlen(trim($_POST['username'])) != 0){ 
     $username = $_POST['username']; 
     if(strlen($username) > 8){ 
      $warning = 'Only 8 characters allowed!'; 
     }else{ 
      header("Location: next_page.php"); 
     } 
    } 
    else 
    { 
     $warning = 'Username is empty, please provide a valid name.'; 
    } 
} 
?> 
<form action="form.php" method="post"> 
    <input type="text" name="username" value="<?php echo($username); ?>" /> 
    <?php echo($warning); ?> 
    <input type="submit" name="submit" /> 
</form> 

這個片段將保持在任何情況下,輸入是錯誤的,只能重定向到下一個頁面,如果數據是有效的。使用jQuery的一個例子可能是這樣的:

<html lang="en"> 
<head> 
    <meta charset="utf-8"> 
    <title>event.preventDefault demo</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
</head> 
<body> 


    <form action="form.php" method="post"> 
     <input type="text" name="username" id="formUsername" /> 
     <span id="warning"> </span> 
     <input type="submit" name="submit" /> 
    </form> 

    <script> 
    $(document).ready(function() { 

     $("form").submit(function(event) { 
      var username = $("#formUsername").val().trim(); 

      if(username.length > 0 && username.length < 9){ 
       $("form").submit(); 
      } 
      else if(username.length > 8) 
      { 
       $("#warning").text("More than 8 characters."); 
       event.preventDefault(); 
      } 
      else{ 
       $("#warning").text("Empty username?"); 
       event.preventDefault(); 
      } 
     }); 

    }); 
    </script> 
</body> 
</html> 
+1

你在第一個例子中混合了'$ _GET'和'$ _POST',表單仍然通過POST提交,第二行會拋出'UNDEFINED Variable',除非用戶名是通過URL設置的。 (僅在第一個示例中) – Epodax

+0

謝謝,我確實改進了一下。起初,我正在處理一個不同的想法,因此與'$ _GET'混淆。這僅僅意味着要給某人一個關於可以做什麼的想法。它有很多缺陷。 – Seth

0

據我所知,你正在試圖輸出消息在同一頁上提交表單後,沒有報警。 HTML表單標籤上的action屬性將您重定向到next_page.php。因此,將action屬性保留爲空,並且在驗證了所有字段後,使用php header()函數重定向到next_page.php。注意不要在header()函數之前輸出任何消息,在這種情況下它將失敗。 不要smthing這樣的:

<?php 
if($_SERVER['REQUEST_METHOD'] == 'POST'){ 
    $err_message = ''; 
    //validate your fields 
    if($_POST['yourfield'] == ''){ 
     $err_message = 'Please check your fields'; 
    } 
    //other validations 
    ... 
    if($err_message == ''){ 
     //redirect to your next page 
     header('Location:next_page.php'); 
     //prevent further code execution 
     exit(); 
    } 
    //output your message 
    echo $err_message; 
} 
?> 

<form action="" method=post> 
*Other fields beside input* 
<input type="submit" name="submit" value="Submit form"> <br> 
</form> 
0

有很多方法可以做到這一點。

這裏又是,如果你想它的回聲在基於保持一種替代方案:

<?php 
if (isset($_POST['submit'])) 
{ 
    echo '<script>alert("Submitted!");</script>'; 
} 
?> 

<form action="" method=post> 
    *Other fields beside input* 
    <input type="submit" name="submit" value="Submit form"> <br> 
</form> 

編輯:一般來說,我試圖離開的PHP在同一個頁面的頂部,而不是其他地方點作爲沒有太多需要去做。

0

我解決這個問題的方式是使用Jquery,如下所示。

$('#form1').submit(
    function() { 
     //your validation rules here 
     var email = $('#YourEMail').val() 
     if(!validEmail(email)) 
     { 
      $('#invalidAlert').html('<i>Invalid e-mail address!</i>') 
      return false; 
     } 
     else 
      return true; 
    });