Storing form data using html & php - php

I've got a form with 50 questions. After the user has filled in the form, I want to take them to another page to first cancel / confirm that they are finished with the form. My question is if they select cancel, how do I put back all the fields that has been answered in the form?
EDITED
I've edited my question to show my code because I'm strugling:
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' ORDER BY RAND()";
$result1=mysql_query($sql1);
echo "<form method='post' action='....'>";
while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
$option3=$row1['option3'];
echo "$question";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='radio' name='question[$q_nr]' value='C'>$option3<BR>";
} else {
echo ''; }
} else { // else if not <> mr
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='A'>$option1<BR>";
} else {
echo ''; }
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='B'>$option2<BR>";
} else {
echo ''; }
if($option3!="") {
echo "<input type='checkbox' name='question[$q_nr][]' value='C'>$option3<BR>";
} else {
echo ''; }
} //end else if q_type <> mr
echo "<BR>";
echo "<BR>";
echo "</p>";
} //end while row1
echo "<input type='submit' value='Finish'>";
echo "</form>";

Make the Cancel button a Submit and let it post back to the original questionnaire. In there, you can do like this:
<input type="text" name="q1" value="<?=isset($_POST['q1']) ? $_POST['q1'] : '';?>" />
Or:
<input type="radio" name="q2" value="A" <?=isset($_POST['q2']) && $_POST['q2'] == 'A' ? 'checked="checked"' : '';?>" />
<input type="radio" name="q2" value="B" <?=isset($_POST['q2']) && $_POST['q2'] == 'B' ? 'checked="checked"' : '';?>" />
Same goes for option elements inside a select (selected="selected").

Pass the $_POST or $_GET array to the confirmation page, and if they hit cancel, pass the array back to the page, and fill in the elements.

The easiest solution would be adding:
<INPUT type=button value="Cancel" onClick="history.back();">
When you go back, the form would still be there.

You can just use $_REQUEST['Answer'] in value tag of form.

Related

PHP get request resets page

So, I'm making a PHP page. I'm trying to get a url like this
https://nikkiedev.com/folder/indexfile/?schedule-btn=true&time-zone=CET
but instead, when I click on my GET form for the time zone it goes to
https://nikkiedev.com/folder/indexfile/?time-zone=CET
Here's my code:
<div class='mobile-center'>
<form method="GET">
<button name="schedule-btn" type="submit" value="true" class="btn">schedule</button>
<button name="roles-btn" type="submit" value="true" class="btn">roles</button>
</form>
<?php
$sched_btn = $_GET["schedule-btn"];
$roles_btn = $_GET["roles-btn"];
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}
?>
</div>
Add the value of $sched_btn as a hidden input in the new form.
if (isset($sched_btn)) {
echo "<div class='mobile-center'>";
echo "<h1>Change schedule</h1>";
echo "<hr>";
echo "<p>Choose time zone</p>";
echo "<form method='GET'>";
echo "<input type='hidden' name='schedule-btn' value='$sched_btn'>";
echo "<p>
<input type='radio' name='time-zone' value='CET'> CET
<input type='radio' name='time-zone' value='EST'> EST
<input type='submit'>
</p>";
echo "</form>";
$timezone = $_GET["time-zone"];
echo "</div>";
if ($timezone == "CET") {
echo "<h1>CET</h1>";
}
else if ($timezone == "EST") {
echo "<h1>EST</h1>";
}
}

On submit i need to pass anchor tag value in php

while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
}
Blockquote
//on submit here i need to disply corresponding $row['user_from']
value
Use <input type="hidden" name="whateveryouwant" />, if you don't want to display text field to user.
Try this:
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
echo "<form method='post'><input type='hidden' name='user_from' value='".$row['user_from']."' /><a href='".$row['user_from']."' name='yo' value'".$row['user_from']."'>".$row['user_from']."</a> &nbsp";
echo "<input type='submit' name='acc' value='Accept'> &nbsp <input type='submit' name='cancel' value='Reject Rrquest'></form> <br/><br/>";
}
if(isset($_POST['acc']))
{
echo $_POST['user_from'];//echo the value of user_form
}
<?php
while($row = mysql_fetch_assoc($req))
{
$user_from = $row['user_from'];
?>
<form method="post" action="">
<?php $row['user_from'] ?>
<input type="submit" name="acc" value="Accept">
<input type="submit" name="cancel" value="Reject Rrquest">
</form>
<php
}
?>
<?php
if(isset($_POST['acc']) && !empty($_POST['yo']))
{
$link = $_POST['yo'];
// do what you want to do with this `url`
}
?>
NOTE: Don't Complex Your Code With Using Html COde In Php echo. You Can Just Open the While Loop Brackets { and then close the php ?> then simple html you have to written, So Just Avoid to used html code inside the php echo

Check a checkbox when element exists in the database

I have a table that has different names of competences and another one in which we have the competence_ID and the user_ID that has that competence. I have a form in which I submit the competences that apply to a certain user. Now, I want to retrieve these competences and check boxes in case they are valid.
I use this code, but it does not seem to work.
<?php
include "db.php";
$employees=mysql_query("SELECT * FROM asiakas");
echo "<form method='post' action='' id='employeesselection'><select name='select_employee' id='select_employee'>";
while($row=mysql_fetch_array($employees)){
$selected = ($row['Id'] == $_POST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
}
echo "</select><input type='hidden' name='action' value='selectedemployee'>
<input type='submit' value ='submit' name='submit'><br/>";
if(isset($_POST['select_employee'])){
$specific=mysql_query("SELECT Id, Sukunimi,Etunimi from asiakas WHERE Id=".$_POST['select_employee']);
$sp=mysql_fetch_array($specific);
}
if(isset($sp['Etunimi']) && isset($sp['Sukunimi'])){
$comp=mysql_query("SELECT * FROM Competences");
echo "<br/>select competences for: ". $sp['Etunimi'];
$id= $sp['Id'];
$result=mysql_query("SELECT distinct c_ID from User_Competence WHERE e_ID=".$id);
while($test=mysql_fetch_array($result))
{
echo $test['c_ID'];
}
echo "<table><th>valid?</th><th>Competence description</th>";
$counter=0;
$traverse=0;
while($compi=mysql_fetch_array($comp)){
$checked='';
if($test[$traverse]==$counter){
$checked="checked";
$traverse++;
}
$counter ++;
echo "<tr><td><input type='checkbox'" .$checked." name='c[]' value='".$compi['Competence_ID']."'></td><td>".$compi['Competence_Description']."</td></tr>";
}
echo "</table>";
echo "<input type='hidden' name='action' value='selectchecked'>";
echo "<input type='submit' value='submit checks'>";
}
if(isset($_POST) && !empty($_POST)){
print_r($_POST);
}
if(isset($_POST['action']) && $_POST['action']='selectchecked'){
if (isset($_POST['c'])){
$s = $_POST['c'];
foreach ($s as $k => $v) {
if (is_array($v)) {
// array_push($s, implode('', array($v [$what])));
}
};
echo $abc= implode(',', $s);
for ( $a=0;$a<count($s);$a++){
$ar=explode(',',$abc);
echo $var= $ar[$a];
$q=mysql_query("INSERT INTO user_competence(c_ID, e_ID) VALUES ('".$var."','".$id."')");
}
}
}
echo "</form>";
?>
If you put checked as an attribute on the <input type="checkbox"> it will be checked, even if you leave the actual value of the attribute empty.
So change the following parts from...
$checked="checked";
...
echo "<tr><td><input type='checkbox' checked='".$checked."' name='c[]' ...
Into...
$checked="checked='checked'";
...
echo "<tr><td><input type='checkbox' ".$checked." name='c[]' ...
Which will mean if the $test[$traverse$] is true, checked='checked' will be placed into the HTML, otherwise it will be left out.

Form with radio buttons & checkboxes sending to php

Harmiih really helped me with my script (http://stackoverflow.com/questions/7510546/html-form-name-php-variable). Basically I have a table with questions. The form retrieves the questions from the table and then I need the selected answers to go to answer.php. Everything is working with the radio butttons but with the check boxes it's only sending the last selected checkbox. Can someone please help me?
form
$sql1="SELECT * FROM ex_question WHERE test_name = '$tid' AND q_type = 'mr' ORDER BY RAND() LIMIT 5";
$result1=mysql_query($sql1);
echo "<form method='post' action='answer.php'>";
while($row1 = mysql_fetch_array($result1))
{
$test_name=$row1['test_name'];
$q_nr=$row1['q_nr'];
$q_type=$row1['q_type'];
$question=$row1['question'];
$option1=$row1['option1'];
$option2=$row1['option2'];
echo "<P><strong>$q_nr $question</strong><BR>";
if ($q_type != 'mr') {
if($option1!="") {
echo "<input type='radio' name='question[$q_nr]' value='$option1'>$option1<BR>";
} else {
echo '';
}
if($option2!="") {
echo "<input type='radio' name='question[$q_nr]' value='$option2'>$option2<BR>";
} else {
echo '';
}
} else {
if($option1!="") {
echo "<input type='checkbox' name='question[$q_nr]' value='$option1'>$option1<BR>";
} else {
echo '';
}
if($option2!="") {
echo "<input type='checkbox' name='question[$q_nr]' value='$option2'>$option2<BR>";
} else {
echo '';
}
}
echo "<BR>";
echo "<BR>";
echo "</p>";
}
echo "<input type='submit' value='Send Form'>";
echo "</form>";
answer.php
<?php
//Key is $q_nr and $answer is selected $option
foreach($_POST['question'] as $key => $answer) {
echo $key;
echo $answer;
}
?>
You can use this as name:
name="question[$q_nr][]"
This will make sure you return an array containing all values of the selected checkboxes.
You need to have different name values in checkboxes.
For example name='question1[$q_nr]' and name='question2[$q_nr]'. The same name works only with grouping elements witch radio buttons are.
Or passing them as arrays:
name='question[$q_nr][1]' and name='question[$q_nr][2]'

problem in $_POST

I got my question :D it was about $_POST using my id that couldn't be accessed in insert submit button I inserted hidden in if statement of my id
if(array_key_exists('myid',$_POST))
{
$query1="select * from patient where id=".$_POST['myid'];
$result1=mysql_query($query1);
echo $query1;
//var_dump($_POST['myid']);
$num2=Mysql_num_rows($result1);
$num3=Mysql_num_fields($result1);
//clinical file ro neshun mide
if($num2>0)
{
echo "<table border=2>";
for($i=0;$i<$num2;$i++)
{
$row=mysql_fetch_row($result1);
echo"<td>id</td><td>name</td><td>Lastname</td><td>Info</td><td>Sympthoms</td><td>Diagnosis</td>";
echo "<tr>";
for($j=0;$j<$num3;$j++)
{
echo"<td>$row[$j]</td>";
}
echo"</tr>";
}//for
echo"</table>";
$y=$_POST['myid'];
echo"<input type='hidden' name='negin' value='$y'>";
}//if
//showing pharmacies($_POST['ph']):
$query2="select * from pharmacies";
$result2=mysql_query($query2);
$nump=Mysql_num_rows($result2);
echo "Please Select a Pharmacy:<select ID=2 name='ph'>";
echo"<option >select please";
for($i=0;$i<$nump;$i++)
{
$row=mysql_fetch_row($result2);
echo"<option value=$row[1]>$row[1]";
echo"</option>";
}
echo"</SELECT>";
//showing drugs($_POST['dg']):
$query2="select * from pharmacy";
$result2=mysql_query($query2);
$nump=Mysql_num_rows($result2);
echo "Please Select Drug:<select ID=1 name='dg'>";
echo"<option >select please";
for($i=0;$i<$nump;$i++)
{
$row=mysql_fetch_row($result2);
echo"<option >$row[0]";
echo"</option>";
}
echo"</SELECT>";
echo"<b>Quantity:<input type='text' name='txt1'/>";
echo"<input type='submit' name='insert' value='insert this drug'/>";
}//ifmyid
if(array_key_exists('insert',$_POST))
{
echo "HELLO";
$negin=$_POST['negin'];
$qname="select * from pnt where id='$negin'";
$resname=mysql_query($qname);
$rown=mysql_fetch_row($resname);
echo $qname;
$na=$rown[1];
mysql_real_escape_string ($_POST['dg']);
mysql_real_escape_string ($_POST['txt1']);
mysql_real_escape_string ($_POST['ph']);
mysql_real_escape_string ($na);
$ins="insert into request(drug,qty,ph,situation,Doctor,userp)values('".$_POST['dg']."',".$_POST['txt1'].",'".$_POST['ph']."','underprocess','$uname','$na')";
echo $ins;
$rlt=mysql_query($ins);
//showing prescribe(table request)
$in="select * from request";
$rslt=mysql_query($in);
$num2=Mysql_num_rows($rslt);
$num3=Mysql_num_fields($rslt);
if($num2>0)
{
echo "<table border=2>";
echo"<td>id</td><td>drug</td><td>quantity</td><td>Doctor</td><td>explanation</td><td>pharmacy</td>";
for($i=0;$i<$num2;$i++)
{
$row=mysql_fetch_row($rslt);
echo "<tr>";
for($j=0;$j<$num3;$j++)
{
echo"<td>$row[$j]</td>";
}
echo"</tr>";
}//for
echo"</table>";
}//if$num2
}
Using two submit buttons in same form
is that your question ? i am not sure . if it is then simple !
<form name="xx" method="post">
<input type="submit" name="submit1" value="1" />
<input type="submit" name="submit2" value="2" />
</form>
step 1 : Add submit buttons with same name in your form .
if($_POST['submit1']=="1")
{
echo "form submited using first submit button";
}
if($_POST['submit2']=="2")
{
echo "form submited using second submit button";
}
step 2 : get values in do different sections . Then you can do different work for each buttons
I think it helps !

Categories