2011-06-17 66 views
1

我已经做了一个简单的表格,我将要求学生在线完成。Php表单 - 停止脚本运行直到提交被点击

它由许多字段组成,这些字段将被传递到mysql数据库中。表单工作正常,但唯一的问题是当我第一次加载页面时,它没有向数据库提交任何内容(因为所有的字段都是空的),并且出现了一个错误,表明你没有完成必填字段

如何让页面的脚本部分仅在单击“提交”按钮时才能运行?

这里是我的代码

<! Code to check that the user has logged into to view this page !> 
<?php 
Code to check for session here 
?> 
<!Connection details for connecting to mysql database!> 
<?php 
Info for connection to database 
} 

//Select which database you want to connect to 
Code to select database here 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Op Tech Database - Add Record</title> 
</head> 
<!Code to Create drop down menu's!> 
<?php 
//Code for collectiing values for Student Names drop down drop 
$result1=mysql_query("SELECT studentID, studentName FROM students"); 
$options1=""; 
while ($row=mysql_fetch_array($result1)) { 

    $id=$row["studentID"]; 
    $first=$row["studentName"]; 

    $options1.="<OPTION VALUE=\"$first\">".$first.'</option>'; 
    } 

//Code for getting tutors names in drop down list 
$result2=mysql_query("SELECT staffID, tutorName FROM staff"); 

$options2=""; 
while ($row=mysql_fetch_array($result2)) { 

    $id=$row["staffID"]; 
    $first=$row["tutorName"]; 

    $options2.="<OPTION VALUE=\"$first\">".$first.'</option>'; 
} 
    ?> 
<body> 
<link rel="stylesheet" type="text/css" href="ex1.css" > 
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" /> 
<!Create HTML elements!> 
<form name="myform" form method="post"> 
    <h1 align="center"><a href="both.php"><img src="colour_logo_400.jpg" alt="University Logo" width="400" height="185" /></a></h1> 
    <h1 align="center">Dental Hygiene Operative Technique Database</h1> 
    <h2 align="center">Welcome to the Dental Hygiene Operative Technique Database v1</h2> 
    <p align="left">Student Name</p> 
    <p align="left"> 

    <! Drop Down Menu to get student names from database !> 
    <SELECT NAME=studentName > 
<OPTION VALUE=0> 
<?php 

echo $options1 
?> 
</SELECT> 
    <p align="left">Tutor Name 
    <p align="left"> 

    <! Drop Down Menu to get tutor names from database !> 
    <select name=tutorName> 
    <option value=0> 

     <?php echo $options2 ?> </option> 
    </select> 
    <p align="left"> 

<p align="left"><br> 


    Procedure 
    <input type="text" name="procedure" value="<?php if(isset($_POST['procedure'])) echo $_POST['procedure'];?>" /> 
<p align="left"> 
<br> 
<SELECT NAME=grade id=grade> 
     <OPTION VALUE="">Grade 
     <OPTION VALUE="N" <?php if (isset($_POST['grade']) && $_POST['grade'] == "N") { 
     echo 'selected="selected"';} ?>>N</option> 
     <OPTION VALUE="B" <?php if (isset($_POST['grade']) && $_POST['grade'] == "B") { 
     echo 'selected="selected"';} ?>>B</option> 
     <OPTION VALUE="C" <?php if (isset($_POST['grade']) && $_POST['grade'] == "C") { 
     echo 'selected="selected"';} ?>>C</option> 
     </SELECT> 
     <br> 
<br> 
      Student Reflection:<br> 
     <textarea name="studentReflection" cols="100" rows="5"><?php if(isset($_POST['studentReflection'])) echo $_POST['studentReflection'];?></textarea> 
    <p align="left"> 
<p align="left"> 
<p align="left"> 
    <p align="left"> 
     <SELECT NAME=professionalism> 
      <OPTION VALUE="">Professionalism 
      <OPTION VALUE="U" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "U") { 
     echo 'selected="selected"';} ?>>U</option> 
     <OPTION VALUE="S" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "S") { 
     echo 'selected="selected"';} ?>>S</option> 
     <OPTION VALUE="E" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "E") { 
     echo 'selected="selected"';} ?>>U</option> 
     </SELECT> 
     </SELECT> 
     <SELECT NAME=communication> 
     <OPTION VALUE="">Communication 
     <OPTION VALUE="U" <?php if (isset($_POST['communication']) && $_POST['communication'] == "U") { 
     echo 'selected="selected"';} ?>>U</option> 
     <OPTION VALUE="S" <?php if (isset($_POST['communication']) && $_POST['communication'] == "S") { 
     echo 'selected="selected"';} ?>>S</option> 
     <OPTION VALUE="E" <?php if (isset($_POST['communication']) && $_POST['communication'] == "E") { 
     echo 'selected="selected"';} ?>>U</option> 
     </SELECT> 

     Alert: 
     <input type="checkbox" value="YES" name="alert" > 
     <br> 

<br> 
     Tutor Comments:<br> 
     <textarea name="tutorComments" cols="100" rows="5"><?php if(isset($_POST['tutorComments'])) echo $_POST['tutorComments'];?></textarea> 
    <p align="left"> 

     <input type="submit" value="Update Database" /> 

    <input type='button' value='Logout' onClick="window.location.href='http://138.253.2.72/php_sandbox/optech/dh/current/14june/logout.php'"> 
    <p align="left"> 

</FORM> 
<p> <a href="both.php">Enter another procedure </a> 
<p> 
    <?php 
//Error Message to display if all the correct fields are not completed. 
$errorMessage = "This is the standard error message"; 

$options1 = $_POST['studentName']; 
$options2 = $_POST['tutorName']; 
$procedure = $_POST['procedure']; 
$grade = $_POST['grade']; 
$studentReflection = $_POST['studentReflection']; 
$professionalism = $_POST['professionalism']; 
$communication = $_POST['communication']; 
$tutorComments = $_POST ['tutorComments']; 

if(empty($_POST['alert'])) 
{ 
$_POST['alert'] = "NO"; 
} 
$alert = $_POST['alert'] ; 

//Code to check that the Student Name field is completed 
if(empty($_POST['studentName'])) 
{ 
echo "You have not selected a student. Please go back and do so!"; 
exit(); 
} 
//Code to check that the Tutor Name field is completed 
if(empty($_POST['tutorName'])) 
{ 
echo "You did not select a tutor name. Please go back and select your name from the tutors list"; 
exit(); 
} 
//Code to check that the Procedure field is completed 
if(empty($_POST['procedure'])) 
{ 
echo "You did not select a procedure. Please go back and enter the name of the procedure which you undertook"; 
exit(); 
} 
//Code to check that the Grade field is completed 
if(empty($_POST['grade'])) 
{ 
echo "You did not select a grade. Please go back and select your grade from the drop down list"; 
exit(); 
} 
//Code to check that the Student Reflection field is completed 
if(empty($_POST['studentReflection'])) 
{ 
echo "The student did not enter any comments for this procedure. Student reflection is required for each procedure. Please go back and enter any comments"; 
exit(); 
} 
//Code to check if the tick box is checked that the tutor comment is entered 
if(!strlen($_POST['tutorComments']) && isset($_POST['alert'])){ 
    echo "You must enter a comment on the student if you tick the alert box"; 
    exit(); 
} 

//Code to connect to the database 
Database connect code here 


$query= "INSERT INTO entry (entryID, studentName , tutorName , procedureName , grade , studentReflection , tutorComments, professionalism , communication , alert ) VALUES ('NULL', '".$options1."' , '".$options2." ' , '".$procedure."' , '".$grade."' , '".$studentReflection."', '".$tutorComments."' , '".$professionalism."' , '".$communication."' , '".$alert."' )"; 


mysql_query($query) or die ('Error : Something fucked up' .mysql_error()); 


echo "The Database Has been updated. Thanks " ; 
?> 
</p> 
<p>&nbsp;</p> 
</body> 
</html> 
+2

也许你可以显示你的代码...? – deceze 2011-06-17 08:28:16

回答

1

你missinterpreting PHP。由于整个脚本运行并且结果被发送到用户浏览器,因此您无法停止脚本,直到表单被引用为止。您无法停止脚本。所以,你需要一些机制来运行脚本的某些部分

在窗体中,插入一个隐藏字段:

<input type="hidden" name="submited" value="true" /> 

在你的PHP代码,您尝试获得表单的值,并将其保存到数据库之前,请检查:

if(isset($_REQUEST['submited'])) { 

    // your save code goes here 

} 
+0

我试过这个,但不认为我把你建议的代码放在正确的区域。我添加了隐藏字段,然后在php获取POST值之前添加了if语句。它是否正确? – Mattrsa 2011-06-17 09:42:34

+0

是的,这应该是正确的。我试过在这个代码之前加上if语句 $ errorMessage =“这是标准的结束符(在'echo”数据库已经更新之后,谢谢“;') – 2011-06-17 10:06:50

+0

错误信息”; 但我越来越语法错误解析错误:语法错误,意外的'{' – Mattrsa 2011-06-17 10:22:45

2

如果表单发送数据时,你会使用类似

If($_POST){} 

而且在大括号内放置你的代码,这在处理之前检测是否有任何数据正在发送到页面。但是如果没有看到你的代码,我不能告诉你其他的东西。

0

你可以给你的提交按钮命名为&值。 说name= "submit" value ="submit"

,把你所有的PHP代码,您要执行按钮被点击

if($_REQUEST['submit'] =='submit' && isset($_REQUEST['submit'])){ 
    //php code 
} 

这将工作细到你后

+0

我试过这个建议,但是当我完成表单并单击提交没有任何反应。 – Mattrsa 2011-06-17 13:41:28

2

运行仅POST请求的脚本:

if ('POST' === $_SERVER['REQUEST_METHOD']) 
{ 
    // process the form data 
    // print result 
} 
else 
{ 
    // print form 
}