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 !
Related
I am trying to build a basic quiz system and everything seems ok.
The following code shows how a users choose the correct answer and the getresult.php shows the result. In my database, there is a question, opt1, opt2, opt3 opt4 and answer column.
<form method="POST" action="getresult.php">
<label>Enter Your Name:</label><br>
<input type="text" name="name" required><br><br>
<?php
$db = new mysqli("localhost", "root", "","learndb");
$stmt=$db->prepare("SELECT * FROM quiz");
$stmt->execute();
$result=$stmt->get_result();
while($myrow = $result->fetch_assoc())
{
echo "<form method='POST' action='getresult.php'>";
echo $myrow['id'];
echo ".";
echo $myrow['question'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt1'].">";
echo $myrow['opt1'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt2'].">";
echo $myrow['opt2'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt3'].">";
echo $myrow['opt3'];
echo "<br>";
echo "<input type='checkbox' name='mycheck[]' value=".$myrow['opt4'].">";
echo $myrow['opt4'];
echo "<br><br>";
}
?>
<input type="submit" name="submit" value="Get Results" class="btn btn-primary">
getresult.php
<?php
extract($_POST);
$db = new mysqli("localhost", "root", "","learndb");
$stmt=$db->prepare("SELECT * FROM quiz");
$stmt->execute();
$result=$stmt->get_result();
$myrow = $result->fetch_assoc();
$totalCheckboxChecked = sizeof($_POST['mycheck']);
$submit=isset($_POST['submit']);
$count=0;
if($submit)
{
for($i=0;$i<$totalCheckboxChecked;$i++)
{
if($mycheck[$i]==$myrow['answer'])
{
$count=$count+1;
}
}
echo "Hello ";
echo $_POST['name'];
echo "<br>";
echo "You scored ";
echo $count;
}
Now the problem is with the checkbox, I can check all the values from all the questions. And when I use radio button I can check only one value from all questions. How can I check only one value from one question.
I don't know why you're not using radio buttons if you plan to allow only one selection, but you can set a limit to have the same thing with checkboxes with some JavaScript (or jQuery).
Here is an example: see fiddle demo
var limit = 1;
$('input').on('change', function(event) { // this could've been a 'click' event too.
if($(this).siblings(':checked').length >= limit) {
this.checked = false;
/* OR you can do like:
if($("input[name='mycheck']:checked").length > limit) { //... }
*/
}
});
if the question contain only one answer better use radio buttons to select the option
or
else
use this jquery to select single value from checkbox
$('input[type="checkbox"]').on('change', function() {
$('input[type="checkbox"]').not(this).prop('checked', false);
});
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.
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.
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]'
When I click the update button it will not update the table but when i Click it again it will be updated how can I correct it?
<html>
<head>
<title>pharmacy</title>
<form method="post">
<?php
session_start();
$connection=Mysql_connect('localhost','admin','123');
Mysql_select_db('db',$connection);
$pname=$_SESSION['s1'];
$query="select * from request where ph='$pname'";
if(array_key_exists('rqlist',$_POST))
{
if(!$connection)
{
echo 'connection is invalid';
}
else
{
$result=mysql_query($query);
if($result )
{
$num=Mysql_num_rows($result);
$num1=Mysql_num_fields($result);
echo $num;
if($num>0)
{
echo "<table border=2> Table Request".$_SESSION['s1'];
echo"<tr>
<td>Id</td><td>Drug</td><td>Quantity</td><td>Doctor</td><td> </td><td>pharmacy</td><td>name</td><td>lastname</td><td>Situation</td>
</tr>";
for($i=0;$i<$num;$i++)
{
$row=mysql_fetch_row($result);
echo "<tr>";
for($j=0;$j<$num1;$j++)
{
echo"<td>$row[$j]</td>";
}
echo"<td><input type='Checkbox' name='p[$i]' value='on' unchecked /> </td>";
echo"</tr>";
}
}
}
}//else
}//array
if(array_key_exists('update',$_POST)){
$result=mysql_query($query);
$qut="select qun from $pname";
$rt=mysql_query($qut);
if($result && $rt)
{
$num=Mysql_num_rows($result);
$num1=Mysql_num_fields($result);
//num2=Mysql_num_rows($rt);
if($num>0)
{
echo "<table border=2> Table Request".$_SESSION['s1'];
echo"<tr>
<td>Id</td><td>Drug</td><td>Quantity</td><td>Doctor</td><td></td><td>pharmacy</td><td></td><td>Situation</td>
</tr>";
for($i=0;$i<$num;$i++)
{
$row=mysql_fetch_row($result);
$row2=mysql_fetch_row($rt);
$dr[$i]=$row[1];
$qu[$i]=$row[2];
$st[$i]=$row[7];
$qun[$i]=$row2[0];
$query8="select * from $pname where name='$dr[$i]' and qun>$qu[$i] ";
$result8=mysql_query($query8);
$num8=Mysql_num_rows($result8);
if($num8!=0)
{
if($row[5]!='ready' && $row[5]!='transfer from $pname')
{
$query9="update request set situation='ready' where drug='$dr[$i]' ";
$qu="update $pname set qun=$qun[$i]-$qu[$i] where name='$dr[$i]' ";
$result9=mysql_query($query9);
$resultqu=mysql_query($qu);
echo $qu;
}
}
else{
if($row[5]!='ready'&&$row[5]!='transfer from $pname')
{
$query10="update request set situation='transfer from $pname' where drug='$dr[$i]' ";
$result10=mysql_query($query10);
}
}
echo "<tr>";
for($j=0;$j<$num1;$j++)
{
echo"<td>$row[$j]</td>";
}
echo"<td><input type='Checkbox' name='p[$i]' value='on' unchecked /></td>";
echo"</tr>";
}//for$i
echo"</table>";
}//if num
}
}
?>
<input type="submit" name="rqlist" value="Request list"/>
<input type="submit" name="update" value="Update the list"/>
</form>
</head>
</html>
Try taking out the request list button to see it if anything changes