Every Thing is working fine for my online quiz system but page of uploading questions is not working as it should be working.
The user is restricted to add 20 questions at a time if the limit exceeds, a message will prompted and he will be redirected to his account.
This is the form which will allow the user to input his question, four options and a correct option.
<html>
<body>
<form action="be_uploadquiz.php" method="post">
<table><tr><td>Enter Question Here</td>
<td>
<input name="question" type="text" maxlength="100" /></td></tr>
<tr><td>Enter First Option</td>
<td>
<input name="opt1" type="text" maxlength="100" /></td></tr>
<tr><td>Enter Second Option</td><td>
<input name="opt2" type="text" maxlength="100" /></td></tr>
<tr><td>Enter Third Option</td>
<td>
<input name="opt3" type="text" maxlength="30" /></td></tr>
<tr><td>Enter Fourth Option</td>
<td>
<input name="opt4" type="text" maxlength="30" /></td></tr>
<tr><td>Select The Correct Option</td>
<td>
<select name="woptcode">
<option>A</option>
<option>B</option>
<option>C</option>
<option>D</option>
</select>
</td></tr>
<tr><td>
<input name="submit" type="submit" value="Next" />
</td></tr></table></form>
</body>
</html>
Here is the uploadquiz.php file which inserts the questions
<?php
session_start();
$link = mysql_connect("localhost","root","");
mysql_select_db("quiz",$link);
$question = $_POST['question'];
$opt1 = $_POST['opt1'];
$opt2 = $_POST['opt2'];
$opt3 = $_POST['opt3'];
$opt4 = $_POST['opt4'];
$woptcode = $_POST['woptcode'];
if ( isset( $_POST['submit'] ) ) {
$sql = "INSERT INTO be_quiz (question,opt1,opt2,opt3,opt4,woptcode) VALUES ('$question', '$opt1','$opt2','$opt3','$opt4','$woptcode')";
$i++;
header('Location:be_uploadquiz.html');
if($i==20)
{
header('Location:message.html');
}
}
session_destroy();
if(!mysql_query($sql))
{
die('Error:'.mysql_error());
}
mysql_close();
?>
I want the user to redirect again to uploadquiz.html if the limit is not reached and to a file message.html if the maximum limit (i.e 20 questions have been reached) is reached and then to his account. this is not working need help.
Your variable $i is not maintained across navigation. You could use a session variable for that, like this:
start_session();
if (!isset($_SESSION["counter"])) {
$_SESSION["counter"] = 0;
}
Then use $_SESSION["counter"] instead of $i;
$_SESSION["counter"]++
Don't destroy the session, or you will not retain this value. So delete this line:
destroy_session();
If you want to make sure sessions are destroyed after a certain time of inactivity (also resetting the counter for that user), then read here how you can do that.
Now there is still an issue: your check on the 20-limit happens too late and would not stop the user from continuing to submit. You should put that test before the actual insert and increment happens.
Related
I am new to php and am not quite clear on what to do to carry my information from the first page to the next and then submit it to my email when they are done filling out contact information.
I need the script to work as follows:
Step1: User clicks the input check-boxes for the field they want that is stored in an array
ex:
< input type="checkbox" name="Sound[]" value="item1" > item1
and clicks a button i have written as
< input type="image" name="Submit" class="" alt="" src="images/contact1.png" border="0" >
Step2: The information from the check-boxes they have clicked needs to be carried over to the next page where they will fill out their contact info. Name email phone etc.
ex:
<tr>
<td valign="top">
<label for="telephone">Telephone Number *</label>
</td>
<td valign="top">
<input type="text" name="telephone" maxlength="30" size="30" style="margin-bottom: 10px;">
</td>
</tr>
Step3. All of this information should be sent to my email upon button press for me to contact them :D
<tr>
<td colspan="2" style="text-align:center;">
<input type="image" name="Contact" class="contactbutton" alt="" src="images/contact.jpg"/>
</td>
</tr>
I can pull the information from my inputs but do not know how to carry to the next page!
Can I do it all in the same php script? or does each page need a different php script?
Please help!
Thanks Paul
you can do it with a form and send it to the page2.php. value will be stored in
$_POST['S'] for the checkbox
<form action="page2.php" method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" >
</form>
------------------
page2.php
echo($_POST['S']); // will be item1
$_SESSION array is better. to use it you need to put session_start(); at start of
every page that will use your $_SESSION variable i.e
session_start();
if(isset($_POST['S'])){
$_SESSION['h'] = $_POST['S'];
echo($_SESSION['h']); } //output value in checkbox
?>
<html><body>
<form method="post">
<input type="checkbox" name="S" value="item1" > item1
<input type="SUBMIT" value="item1" >
Once this script is run you can accesS value in $_SESSION['h'] in other pages.
the data will be deleted when you close browser.
----------------------------------
page2.php
<?php
session_start();
if(isset($_SESSION['h'])){ //check if $_SESSION['h'] has been set a value
echo $_SESSION['h']; //output value stored in var
}
?>
You will ultimately still require the use of POST data to get the checkbox status from your page.
Page 1:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['Sound'])) {
$_SESSION['Sound'] = $_POST['Sound'];
header('Location: http://www.example.com/page2.php');
exit;
}
?>
<form method="post" action="page1.php">
Sound? <input type="checkbox" name="Sound" value="item1"><br>
<input type="submit">
</form>
Page 2:
<?php
session_start();
// If postdata is received then redirect to next page
if(isset($_POST['telephone']) && isset($_POST['email'])) {
$_SESSION['telephone'] = $_POST['telephone'];
$_SESSION['email'] = $_POST['email'];
header('Location: http://www.example.com/page3.php');
exit;
}
?>
<form method="post" action="page2.php">
<!-- If you want to output the previously saved data in a disabled item -->
Sound? <input type="checkbox" name="Sound" value="item1" disabled="disabled" <?php if($_SESSION['Sound'] == 'Yes') echo('checked="checked"'); ?>>
Telephone: <input type="text" name="telephone" value=""><br>
Email: <input type="email" name="email" value=""><br>
<input type="submit">
</form>
And so on and so forth for your next pages
This does not include the code for generating the e-mail via PHP but is intend to show you how you can take the form input/checkbox selections and store there values to a SESSION ARRAY. Note that in this example: The form is submitting to itself by leaving the action="" blank, but normal would submit to a external PHP file for parsing/handling.
Also, im choosing to create a random number to represent the visitor to the form if not specifically set by $_POST['user']
<?php session_start();
if (!isset($_SESSION['user'])) {$_SESSION['user']=rand(10,700);}
if (isset($_POST['user'])) {$id=$_POST['user'];} else {$id=$_SESSION['user'];}
?>
<form action="" method="post">
Sound 1:<input name="cb1" type="checkbox" value="sound1"><br>
Sound 2:<input name="cb2" type="checkbox" value="sound2"><br>
Sound 3:<input name="cb3" type="checkbox" value="sound3"><br>
<input type="submit" name="submit" value="submit"><br><br>
<?php
if (isset($_POST['submit']) && $_POST!=="") {
foreach($_POST as $key => $value) {
$_SESSION['visitor']['sounds'][$id]=array(
'selects'=>$_POST['cb1'].",".$_POST['cb2'].",".$_POST['cb3']
);
};
echo "For user ID:".$id." We echo the comma delimited stored SESSION array: ".$_SESSION['visitor']['sounds'][$id] ['selects'];
echo "<br><br>";
// Option 2 Explodes the comma delimited ['selects'] field to handle each choice seperately
$choice = explode(",",$_SESSION['visitor']['sounds'][$id] ['selects']);
echo "For an alternative, we EXPLODE the stored 'selects' field of the SESSION ARRAY and can then echo each out seperately"."<br><br>";
echo "User ".$id." Option 1 value was: ".$choice[0]."<br>";
echo "User ".$id." Option 2 value was: ".$choice[1]."<br>";
echo "User ".$id." Option 3 value was: ".$choice[2]."<br>";
echo "<br><br>";
echo "A last example we loop through the EXPLODED values and echo only those that were selected (ie: had a value)"."<br>";
foreach ($choice as $key => $value ) { if ($value!=="") {echo "Selection: ".$value."<br>";} }
}
?>
Please help me out of this.....
I am designing a table which is inside a form.
the table is generated based on while loop.
In each row there is a download button.
when i click download the POST value should get the same row information.
But my POST variable is giving me the last row information only.
I tried using input-type as hidden... But it did not work
Here is the code for your reference
enter code here
<form name="simpleform" method="post" action="insert.php">
<?php
$data = "environment";
$user_name = $_SESSION['username'];
$serch = mysql_query("SELECT * FROM data WHERE (data_category = '" . $data . "') ");
while ($record=mysql_fetch_assoc($serch))
{?>
<tr class="warning">
<td >
<input type="text" value=<?php echo $record['data_ID'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_ID'];?> name="dataid" />
</td>
<td >
<input type="text" value=<?php echo $record['data_name'];?> readonly="readonly" >
<input type="hidden" value=<?php echo $record['data_name'];?> name="dataname" />
</td>
<td >
<input type="text" value=<?php echo $record['data_downloads'];?> readonly="readonly">
<input type="hidden" value=<?php echo $record['data_downloads'];?> name="datadown" />
</td>
<td >
<input type="text" value="" >
<input type="hidden" value="" name="datause" />
</td>
<td>
<input type="submit" name="simplesubmit" value="Go to download" />
</td>
</tr>
<?php }
exit;
?>
</tbody>
</form>
The problem is that you are using the same name attribute for all your controls. Thus, when PHP receives the form, they get overwritten, and you only see the last value of the form.
The simplest way to avoid that is just appending [] to the end of your names -- eg name=dataid[]. This will make PHP take all arguments as an array, so you don't lose data.
The second problem, is that your submit button also has the same name - you should diversify it by using some row-specific data in its name, such as 'name="submit-'.$record['data_name'].'"'
For more info, more code from you is needed, such as what are the data you are printing like.
Every post button can have its name and value, so if you change your code to produce a traceable post button name you can just do whatever you want with that.
<table>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[1]" value="OK" />
</tr>
<tr>
<td>...</td>
<td>...</td>
<td><input type="submit" name="submit[2]" value="OK" />
</tr>
</table>
When the form is posted its very easy to capture which button is clicked;
if ($_POST["submit"]) {
$id = key($_POST["submit"]);
}
Thanks for info... and good response. As you said , i replaced the same and saw the post value is giving me all arguments as array. My purpose is to let the client download file that he clicks. so if the client click the first row button in the table, the post value should get only that Data_name. So that i can run a query to get the URL of that data_name and download
I have a loop in a form for dynamically adding/removing items, however, only the last item is removed every time. I can add items just fine though.
I can't figure out why no matter which items I try to remove from both add_friends and register_tasks, ONLY the last item is removed. When I echo $_POST['task_name'] it's always the last item on the list, never the one I selected(unless of course I select the last item).
Here is the form
<center><h2>Create Event</h2></center>
<form method="post">
<?
$form = new common_functions();
if($form->check_saved_forms($my_username, "event") == TRUE){
$this->existing_forms(); //allow the user to select existing forms that they have created
}
?>
<tr><td>Title:</td><td><input type="text" width="20%" name="event_title" value="<? echo $form_data[0]; ?>" /></td></tr>
<tr><td>Details:</td><td><input type="text" width="20%" name="event_details" value="<?echo $form_data[1]; ?>" /></td></tr>
<tr><td>Date:</td><td><input type="text" width="20%" name="event_date" id="event_date" value="<? echo $form_data[2]; ?>" /></td></tr>
<tr><td> <input type="submit" name = "submit_event" id = "lOBut" value="Create Event" /></td></tr>
</table>
<?
//this is the section giving me trouble
$form->add_friends($added_friends);
$form->register_tasks($tasks,$nums);
?>
</form>
Both add_friends and register_tasks dynamically add and remove friends/tasks by a textbox, and a list of the added items are shown. users can remove items, however, right now only the last item is removable and I can't figure out why.
Here is register (add_friend is the same, but labeled differently)
function register_tasks($tasks,$nums){
<table>
<td><input type="textbox" size="50" name="register_description"/> </td></tr>
<td><input type ="textBox" name="register_num" /> </td>
<td><input type="submit" name="add_register_tasks" value="Add"/></td></tr>
</table>
<?
if(count($tasks) > 0){
?>
<table>
<tr><td><b>Registration Item Description</b></td><td><b># Of Available Spots</b></td></tr><br>
<?
foreach (array_combine($tasks, $nums) as $task => $task_num){
?>
<tr><td><? echo $task; ?></td><td><? echo $task_num; ?></td>
<td><input type="submit" name="remove_task" value="Remove"/></td>
<td><input type="hidden" name="task_name" value="<? echo $task; ?>"/>
<td><input type="hidden" name="task_num" value="<? echo $task_num; ?>"/></td></tr>
<?
}
?></table><?
}
You need to place the form tag inside the loop. Right now what happens is all the records are displayed in a single form and no matter what item you choose, it takes only the last value. Another alternate solution would be to use GET method and pass the number/name as query string.
I am now developing a travel agency website, in this site when the user reserves a trip he/she have to enter trip related details. This information is collected in a forma. The user also enters the number of people who are travelling.
My question is, how do I gather the same information for everyone who is travelling? Basically I need the form to be generated many times depending upon the number of people of same family ,, so I can capture all their data. How do I do this?
well there are my Code;plllz help me am so confused and tried lots of things to solve it;
thanks in advance
<form action = "insertpassenger.php" method = "POST">
<center>Enter all the information below</center>
<?php for ($i=0;$i<$pplno;$i++) : ?>
people<?php echo $i+1 ; ?>
<input type="text" name="cpr" size="9" value="<?php echo $cpr;?>" maxlength="9">CPR
<input type="text" name="pplno" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr" size="9" maxlength="9">dad CPR
<input type="reset" value="clear" name="clear">
<input type="submit" value="join" name="join">
<?php endfor; ?>
</form>
Your code jumps in and out PHP rather a lot.
Just declare the item names as array entries, either with implicit or explicit numbering:
for ($i=0;$i<$pplno;$i++) : ?>
<input type="text" name="cpr[]" size="9" value="<?php echo $cpr[$i];?>" maxlength="9">CPR
<input type="text" name="pplno[]" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr[]" size="9" maxlength="9">dad CPR
<input type="reset" value="clear[]" name="clear">
<input type="submit" value="join[]" name="join">
<?php endfor;
or...
for ($i=0;$i<$pplno;$i++) {
print "<input type=\"text\" name=\"cpr[$i]\" size=\"9\" value=\"$cpr[$i]\" maxlength="9">CPR";
....
}
I would personally use a session variable and count down how many times the form needs to be completed. Unfortunately that would cause to page to reload after each form entry, but this allows you to have as many amount of forms as your user requests, without a screen scrolling down a few pages to create all the forms on one page.
At the start of your code before you displaying anything to the browser :
<?php
session_start ();
?>
And where you receive your count for looping:
<?php
if (!isset($_SESSION['yourAppName']))
}
$_SESSION['yourAppName'] = $pplno;
} else {
$_SESSION['yourAppName']--;
}
if ($_SESSION['yourAppName'] > 0) {
?>
<form action=''>
<input type="text" name="cpr" size="9" value="<?php echo $cpr;?>" maxlength="9">CPR
<input type="text" name="pplno" size="30" maxlength="25">Number Of People
<input type="text" name="gcpr" size="9" maxlength="9">dad CPR
<input type="reset" value="clear" name="clear">
<input type="submit" value="join" name="join">
<input type="submit" value="Proceed">
</form>
<?php
} else {
// code when all forms are filled in
}
?>
remember to have your form return to the same page. This code is only to guide you, don't expect it to work without some editing. :)
First let's explain what I want to do and then ask my question!
Well, I want to use a search filter for a query (the user should choose by which field will search the database, eg by name, code or fname) After the query runs, I want to show the data in some textfields, so the user can change them.
To do this, I put the first part (search filter-radio group- and filter value-text field-) in my first page(getStudentFilter.php). On submit, the query runs, I put values in SESSION and opens the second page(change_user.php) with my correct data!
If user change student's data, the update in db is ok, but in page change_user.php it shows the initial data again.
I tried to change SESSION values so I can keep my new values before run the update query, but it seems wrong.
Can someone give me a solution so I can fix the problem? Can this be done as it is or I have to change it and put both queries (select and update) in one form? Aaahh, I tried to put both in one form but I don't know how to control two "submit" in one form...
Thanks in advance..
my code after changes is
<form name="change_student" method="post" enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" >
<?php
if ( isset($_POST['upd_student']) && $_POST['upd_student'] = 'Change' ){
echo "UPDATE";
//RUN UPDATE QUERY
}
elseif( isset($_POST['get_filter']) && $_POST['get_filter'] == 'Show' ){
echo "SELECT";
$query = "select * from student where ".$_POST['filter']."='".$_POST['filter_val']."'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$id = $row['idstudent'];
$fn = $row['fname'];
$ln = $row['lname'];
$ph = $row['phone'];
$sc = $row['school_dept'];
echo "<META HTTP-EQUIV='Refresh' CONTENT='0' >";
}
?>
<table width="310">
<tr><td><label><b>FILTER</b></label></td> </tr>
<tr><td><label><input type="radio" name="filter" value="idstudent" id="filter_5">ID </label></td></tr>
<tr><td><label><input type="radio" name="filter" value="fname" id="filter_3">FIRST NAME</label></td></tr>
<tr><td><label><input type="radio" name="filter" value="lname" id="filter_4">LAST NAME</label></td></tr>
<tr><td><input type="text" name="filter_val"> </td></tr>
<tr><td><input type="submit" name="get_filter" id="get_filter" value="Show"></td></tr>
</table>
<table>
<th colspan="2">STUDENT'S DATA</th>
<tr><td>ID</td><td><input type="text" name="st_id" value="<?php echo $id?>"></td></tr>
<tr><td>FIRST NAME</td><td><input type="text" name="fname" value="<?php echo $fn?>"></td></tr>
<tr><td>LAST NAME</td><td><input type="text" name="lname" value="<?php echo $ln?>"></td></tr>
<tr><td>PHONE</td><td><input type="text" name="phone" value="<?php echo $ph?>"></td></tr>
<tr><td>DEPT</td><td><input type="text" name="dept" value="<?php echo $sc?>"></td></tr>
</table>
<input type="submit" name="upd_student" value="Change">
</form>
Do not put search params into session.
Do not use 2 pages.
Make it all on one page and pass search parameters using GET method, like every search facility does.
To control 2 submits in one form you would have to test the values in you're php script .
Let's take the following html form:
<form action="index.php" name="contestForm" id="contestForm" method="POST">
<input type="submit" value="Select" name="select" />
<input type="submit" value="Update" name="update" />
</form>
Now in you're php script you would do this :
if ( isset($_POST['update']) && $_POST['update'] = 'Update' )
{
//do the update part
echo "UPDATE";
} elseif ( isset($_POST['select']) && $_POST['select'] == 'Select' )
{
//do the select part
echo "SELECT";
}