2015-09-21 63 views
0

當前,如果用戶離開鍛鍊標題輸入空白並在鍛鍊文本中有文本輸入,則會顯示一條提示,告訴用戶填寫完整表單,反之亦然。當警報出現並按下OK時,表單數據將丟失,用戶必須重新添加所有輸入。我將如何使警報顯示,並按下ok使用戶保持在同一頁面上?我不確定它是否因爲我點擊「創建新練習」或其他內容而隱藏並顯示div。確認警告仍保留在同一頁面上

我的代碼如下

CourseA.php

HTML:

<script> 
function showDiv(el1,el2){ 
    document.getElementById(el1).style.display = 'block'; 
    document.getElementById(el2).style.display = 'none'; 
} 
</script> 
<div class="right"> 
     <h2 id = 'course_admin_titleA'>Course A</h2> 
     <button id= 'create_btnA' onClick="showDiv('is', 'ie')">Create new session</button> 
     <button id= 'create_btnB' onClick="showDiv('ie', 'is')">Create new exercise</button> 
</div> 

<div id= "ie"> 
    <!-- input form for admin --> 
    <form action="courseA.php" method="post" enctype="multipart/form-data"> 
     <table class ="table_exercise"> 
       <td id = "exercise_titles"><strong> Exercise Title:</strong></td> 
       <td><input type="text" name="exercise_title" value="<?php if(isset($_SESSION['a'])){echo $_SESSION['a'];} ?> " size="60" /></td> 
      </tr> 
      <tr> 
       <!-- input text area for admin to type exercise content --> 
       <td id = "exercise_titles"><strong> Exercise Text:</strong></td> 
       <td><textarea name="exercise_text" rows="15" cols="50" ><?php if(isset($_SESSION['b'])){echo $_SESSION['b'];} ?></textarea></td> 
      </tr> 
     </table> 
     <!-- button with input type 'submit' which is referenced by the php code --> 
     <button id= 'create_btnB'input type="submit" name="submit1" > Publish Exercise Now</button> 
     <!-- Hide if not clicked --> 
     <input type="hidden" value="ie" name="type"> 
    </form> 
</div> 

PHP:

 <? 
     session_start(); 
     //if submit button is set i.e 'publish exercise now' pressed then: 
     if(isset($_POST['submit1'])){ 
      $_SESSION['a'] = $_POST['exercise_title']; //assign to session variable 
      $_SESSION['b'] = $_POST['exercise_text']; 
      //local variables taking the input names from the html forms above 
      $exercise_title = $_POST['exercise_title']; 
      $exercise_text = $_POST['exercise_text']; 
       //validation 
       if($exercise_title =='' OR $exercise_text==''){ 
        echo "<script>alert('Please make sure the exercise has a title and exercise info')</script>"; 
        //exit(); 

       } else { 
        //query to insert the form data from the admin into the exercises table 
        $insert_exercises = "insert into exercises (exercise_title,exercise_text) 
        values ('$exercise_title','$exercise_text')"; 
        //runs query 
        $run_exercises = mysqli_query($con,$insert_exercises); 
        //JS to tell user exercise has been published 
        echo "<script>alert('Exercise Has been Published!')</script>"; 
        //returns to courseA page on the admin panel 
        echo "<script>window.open('courseA.php','_self')</script>"; 
       } //end of else 
     } //end of IF 
     //end of if & end of PHP 
     } ?> 

感謝

+1

是的,它是代碼說的。建議您使用JavaScript驗證表單,而不是使用PHP。您在此使用PHP(表單在您提交數據時驗證)來驗證您的表單。 – divy3993

+0

你不能單單信任JavaScript來進行表單驗證。無論如何,你將不得不在PHP中進行驗證。 – harris

+0

你在php文件裏面使用這個html嗎? –

回答

1

我不知道這方面的其他風險,但您可以使用下面的會話。 在PHP中使用會話和分配數據輸入的變量

<?php 
session_start(); //start session for using session variable 
//if submit button is set i.e 'publish exercise now' pressed then: 
if(isset($_POST['submit1'])){ 
$_SESSION['a'] = $_POST['exercise_title']; //assign to session variable 
$_SESSION['b'] = $_POST['exercise_text']; //assign to session variable 
//local variables taking the input names from the html forms above 
$exercise_title = $_POST['exercise_title']; 
$exercise_text = $_POST['exercise_text']; 

,並在HTML會話,稱這些會話變量作爲文本輸入和地區的值。

<input type="text" name="exercise_title" value="<?php if(isset($_SESSION['a'])){echo $_SESSION['a'];} ?> " size="60" /> 
<textarea name="exercise_text" rows="15" cols="50" ><?php if(isset($_SESSION['b'])){echo $_SESSION['b'];} ?></textarea> 

您需要進行更多驗證。

+0

如果數據不需要超出那一頁,那麼使用會話似乎有點過分。 – harris

+0

@SubinThomas表單數據剩餘,但確定當按下警報框將用戶帶到courseA.php除了它是空白的(全白), – Davidaj

+0

它回來的網頁後,我點擊進入網址和按enter鍵(刷新),但按下確定的警報去一個空白頁,這應該是courseA.php – Davidaj

1

當頁面刷新,並驗證發ils,你需要把失敗的值放回現場。例如:

<input type="text" name="exercise_title" size="60" /> 

應該是:

<input type="text" name="exercise_title" values "$_POST['exercise_title']" size="60" /> 
+0

呼應的結果,因爲壓courseA警告後不工作被刷新,點擊「即」按鈕,當窗體顯示爲空。可能需要修改我的設計 – Davidaj

+0

如果您將表單提交到同一頁面,它將起作用,因爲這些值存在於'POST'數據中。這個答案需要一點點工作,但是它在正確的軌道上。 – harris

0

這可以按照你的方式,如果你正在使用jQuery做它做。無論如何,最好使用Javascript驗證表單,以便減少對服務器的訪問。

<? 
//if submit button is set i.e 'publish exercise now' pressed then: 
if(isset($_POST['submit1'])){ 
    //local variables taking the input names from the html forms above 
    $exercise_title = $_POST['exercise_title']; 
    $exercise_text = $_POST['exercise_text']; 
     //validation 
     if($exercise_title =='' OR $exercise_text==''){ 
      echo "<script>alert('Please make sure the exercise has a title and exercise info'); 
        $('input[name=\"exercise_title\"]').val($exercise_title); 
        $('input[name=\"exercise_text\"]').val($exercise_text);</script>"; 
      //exit(); 

     }else{.... 
+0

您可以直接將值反映到輸入字段中,因此不需要將添加項留給客戶端。還從問題中的代碼判斷,他沒有使用jQuery。 – harris

+0

警報courseA.php仍然被刷新,點擊之前隱藏'ie'div。點擊後,窗體出現空再次 – Davidaj

+0

沒有使用jQuery的,但開放的進一步建議 – Davidaj

1

Andrew的答案是正確的,但代碼有點偏離,並沒有考慮到問題中提到的所有問題。

數據丟失的原因是因爲提交表單將觸發POST請求到表單的action屬性中定義的目標頁面。

從你的問題,我明白,你提交表單到同一個頁面,這樣你就可以從POST數據檢索值,因爲你是你檢查的時候,如果他們這樣做空。

PHP部分應該是你的HTML之前:

<?php 
    if(isset($_POST['submit1'])){ 
     $exercise_title = $_POST['exercise_title']; 
     $exercise_text = $_POST['exercise_text']; 

     if($exercise_title =='' OR $exercise_text==''){ 
      // Do your stuff 
     } else { 
      // Do your other stuff 
     } 
    } else { 
     // Define the variables as empty strings 
     // so they can be used when the form hasn't been submitted yet 
     $exercise_title = ''; 
     $exercise_text = ''; 
    } 
?> 

注:你應該如<?避免使用短的標籤,不是每個環境支持他們。

HTML處理

然後你div知名度取決於如果表單已提交或不:

<div id= "ie" <?php if(!isset($_POST['submit1'])) { echo 'style="display:none"'; } ?>> 

最後echo輸入字段的值:

<input type="text" 
    name="exercise_title" 
    value="<?php echo htmlentities($exercise_title, ENT_QUOTES, 'UTF-8'); ?>" 
    size="60" /> 

而且

<textarea name="exercise_text" rows="15" cols="50"> 
    <?php echo htmlentities($exercise_text, ENT_QUOTES, 'UTF-8'); ?> 
</textarea> 

更新:忘了提及,你的代碼是開放給SQL注入的,所以你應該看看this post

+0

好吧,這肯定是沿着正確的線條用戶形式輸入保持警報出現,但在警報上按下確定將他們帶到一個空白頁面。該網址離開空白頁面建議它應該是courseA.php但這不顯示。感謝您花時間幫助我。 – Davidaj

相關問題