Jump to content

[SOLVED] PHP Form help


plastik77

Recommended Posts

Hi, i'm trying to resolve a problem i have when processing a large form with 2 possible submit buttons. I am using PHP server self and running the script at the top of the page. Regardless of which button is pressed, the form data should be inserted into the database. The only difference is that one button will redirect to another page. When i test for which button has been pressed, however, I don't want to have to repeat all the insert statements for each IF statement. However, I can't use if (isset($_POST)) as the page prior to the current one is also a form, therefore the if (isset($_POST)) statement tries to execute the data on the first loading of the page, which shouldn't happen. If anyone knows how I can avoid having to repeat the insert statements, i'd be grateful for any pointers. Not actually sure if any of this makes any sense!

 

this is current incorrect php code....

 

if (isset($_POST))

 

{

$section= $_POST['sectDropdown'];

$weight = $_POST['weight'];

$qtext = $_POST['question'];

$qType = $_POST['quesType'];

 

if($_POST['categoryDrop']!="blank")

$category = $_POST['categoryDrop'];

else

$category = $_POST['newCategory'];

 

$query = "insert into Question (quesText,quesType,category) values ('$qtext','$qType','$category')";

$result = mysql_query($query);

if (!$result)

echo "Invalid Insert information";

 

$id = mysql_insert_id(); //assigning id of test just created

 

//answer option code

//Check submit

if (key_exists("answer",$_POST))

{

$options = array("A","B","C","D","E");

$answer=array();

//Indexing

foreach ($_POST["answer"] as $i => $value)

{

if($value!="")

{

$opt = $options[$i];

 

//test to see which is the correct answer

if ($_POST["option"]==$i)

{

$corr = "yes";

$query2 = "insert into Answer values ('$id','$opt','$value','$corr')";

$result2 = mysql_query($query2);

if (!$result2)

echo "Invalid Insert information";

}

//incorrect answer - insert into dbs

else

{

$corr = "no";

$query3 = "insert into Answer values ('$id','$opt','$value','$corr')";

$result3 = mysql_query($query3);

if (!$result3)

echo "Invalid Insert information";

}

}

}

}

 

//get section id and test number then update TestQuestion

$query4 = mysql_query("select sectionID from Section where sectionName = '$section'");

$numrows4 = mysql_num_rows($query4);

if($numrows4 ==0)

{

$sectID=""; //no sections in this test

}

else

{

$row = mysql_fetch_array($query4);

$sectID = $row['sectionID'];

}

 

 

$query5 = mysql_query("select questNumber from TestQuestion where testID = '$_SESSION[test]'");

$numrows5 = mysql_num_rows($query5);

if($numrows5 ==0)

{

$questNum=1; //no sections in this test

}

else

{

for($i=0;$i<$numrows5;$i++)

{

$row2 = mysql_fetch_array($query5);

$questNum = $row2['questNumber'];

$questNum++;

}

}

 

$query6 = "insert into testquestion values ('$_SESSION[test]','$id','$weight','$questNum','$sectID')";

$result6 = mysql_query($query6);

if (!$result6)

echo "Invalid Insert information";

}

 

//go to confirm test page

if (isset($_POST['SubmitProceed']))

{

header("location:preview.php");

exit;

}

Link to comment
Share on other sites


<?
if(isset($_POST['SubmitProceed'] == 'submit2') && empty($_POST['sectDropdown']) && empty($_POST['weight']) && empty($_POST['question']) && empty($_POST['quesType']))){
do this....
break;
} 
if(isset($_POST['submit']) && !empty($_POST['sectDropdown']) && !empty($_POST['weight']) && !empty($_POST['question']) && !empty($_POST['quesType'])){
do this....
break;
} 
?>
<input type="submit" name="submit" value="submit"/>
<input type="submit" name="SubmitProceed" value="submit2"/>

Link to comment
Share on other sites

Cheers for the reply DeadEvil. I may have misunderstood your coding, but I would still have to repeat all the mysql inserts for both of these IF statements. I have no problem distinguishing between the two submit buttons as below. I want to insert the data regardless of which button has been submitted - as long as the form has been submitted. Is this possible?

 

if(isset($_POST['SubmitNext']))

{

  //get the form data and do all the inserts

}

if(isset($_POST['SubmitProceed']))

{

  //get the form data and do all the inserts

}

Link to comment
Share on other sites

Rather than check for which button has been pressed just make sure the form fields exist than run your code. Then after check which button has been submitted and redirect to the correct page, eg:

if (isset($_POST['sectDropdown']) && isset($_POST['weight']) && isset($_POST['question']) && isset($_POST['quesType']))
{
    // run what ever code here for processing the form data


   // after processing the form data now check to see what button has been submitted
   if(isset($_POST['SubmitNext']))
   {
        // rediect here when submitNext button has been pressed
   }
   elseif(isset($_POST['SubmitProceed']))
   {
        // rediect here when submitProceed button has been pressed
   }
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.