javascript in php passing value to javascript value - php

I have use code as shown below:
<?
$sql = mysql_query("select * from tbl_item where item_category = '$item_cat'");
$i=1;
while($crows = mysql_fetch_array($sql))
{
;
?>
<tr>
<td width="57" align="center" ><? echo $i;?></td>
<td width="540" align="center" onClick=><div align="center"><? echo $crows["item_name"];?> </div></td>
<td width="385" align="center">
1<input name="<? echo $crows["item_name"];?>" type="radio" id="radioqty<? echo $i;?>" value="1" checked="checked" />
2<input type="radio" name="<? echo $crows["item_name"];?>" id="radioqty<? echo $i;?>" value="2" />
3<input type="radio" name="<? echo $crows["item_name"];?>" id="radioqty<? echo $i;?>" value="3" />
4<input type="radio" name="<? echo $crows["item_name"];?>" id="radioqty<? echo $i;?>" value="4" />
</td>
<td width="249" align="center" >
Full<input name="radioplate<? echo $i?>" type="radio" id="radioplate<? echo $i?>" value="full" checked="checked" />
Half<input type="radio" name="radioplate<? echo $i?>" id="radioplate<? echo $i?>" value="half" /></td>
<td width="281" align="center">
<input name="serve" type="button" value="Serve" onclick="get_code('a','get_item_save.php?item_id='+<? echo $crows["item_id"];?>+'&cat_id='+document.exB.item_category.value+'&plate_type='+document.exB.radioplate<? echo $i?>.value+'&qty='+exB.radioqty<? echo $i;?>.value+'&bill_no='+document.exB.bill_summary_bill_no.value+'&bill_date='+document.exB.bill_summary_date.value),get_code('b','get_item_sale_list.php?bill_no='+document.exB.bill_summary_bill_no.value)"/> </td>
</tr>
<? $i++; }?>
Here I'm doing something wrong when I call get_code function and I'm unable to figure out were I'm wrong. Could someone figure out what I'm doing wrong on this line of code:
<td width="281" align="center">
<input name="serve" type="button" value="Serve" onclick="get_code('a','get_item_save.php?item_id='+<? echo $crows["item_id"];?>+'&cat_id='+document.exB.item_category.value+'&plate_type='+document.exB.radioplate<? echo $i?>.value+'&qty='+exB.radioqty<? echo $i;?>.value+'&bill_no='+document.exB.bill_summary_bill_no.value+'&bill_date='+document.exB.bill_summary_date.value),get_code('b','get_item_sale_list.php?bill_no='+document.exB.bill_summary_bill_no.value)"/> </td>
I get an error at: document.exB.radioplate<? echo $i?>.value show undefined.

Maybe with mysql_fetch_array($sql, MYSQL_ASSOC) in the while statement.
With the MYSQL_ASSOC you can retrieve the fields by its name, like crows['item_name'].

Related

Update multiple records of type radio

I want to update multiple records of type radio (field "status"). Below is the codes. But as you can see the result here:
http://lamoncheri.com/admin/dapur.php?id=21
It only recognize the status of one record.
<?php
$i = 1;
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"> <input name="nama_menu[]" type="text" id="nama_menu" value="<?php echo $rows['nama_menu']; ?>" readonly style="width: 484px; font-size:medium"> </td>
<td align="center"> <input name="qty[]" type="int" id="qty" value="<?php echo $rows['qty']; ?>" readonly style="width: 484px; font-size:medium"> </td>
<td> <input type="radio" name="status[]" id='status' value="order" <?php if ($rows['status'] == "order"){echo "checked";} ?> class ="auto-style5" required>Order<br>
<input type="radio" name="status[]" id='status' value="process" <?php if ($rows['status'] == "process"){echo "checked";} ?> class ="auto-style5" required>Process<br>
<input type="radio" name="status[]" id='status' value="finish" <?php if ($rows['status'] == "finish"){echo "checked";} ?> class ="auto-style5" required> Finish<br>
</td>
<td style="align:center; color:white"><input name="id[]" type="text" id="id" value="<?php echo $rows['id']; ?>" readonly style="border:0; color:white; width: 10px"> </td>
</tr>
<tr>
<td colspan="4" align="center"> </td>
</tr>
<?php
$i++;
}
?>
name put some thing like this
<?php
$i = 1;
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"> <input name="nama_menu<?php echo $i; ?>[]" type="text" id="nama_menu" value="<?php echo $rows['nama_menu']; ?>" readonly style="width: 484px; font-size:medium"> </td>
<td align="center"> <input name="qty<?php echo $i; ?>[]" type="int" id="qty" value="<?php echo $rows['qty']; ?>" readonly style="width: 484px; font-size:medium"> </td>
<td> <input type="radio" name="status<?php echo $i; ?>[]" id='status' value="order" <?php if ($rows['status'] == "order"){echo "checked";} ?> class ="auto-style5" required>Order<br>
<input type="radio" name="status<?php echo $i; ?>[]" id='status' value="process" <?php if ($rows['status'] == "process"){echo "checked";} ?> class ="auto-style5" required>Process<br>
<input type="radio" name="status<?php echo $i; ?>[]" id='status' value="finish" <?php if ($rows['status'] == "finish"){echo "checked";} ?> class ="auto-style5" required> Finish<br>
</td>
<td style="align:center; color:white"><input name="id<?php echo $i; ?>[]" type="text" id="id" value="<?php echo $rows['id']; ?>" readonly style="border:0; color:white; width: 10px"> </td>
</tr>
<tr>
<td colspan="4" align="center"> </td>
</tr>
<?php
$i++;
}
?>

PHP online exam from database

Here i wish to make a online exam portal based on subject. Corresponding staffs assigned for each subject will add the questions and options in database. Each option have a radiobutton. I display the page in using a while loop. The radio button for the all options for each question have same name. so i can't select the answers one by one. If i checked a answer for a question, then the answer of other will be unchecked. I don't know what to do.
<form action="" method="post">
<table width="200" border="1">
<?php
while ($r = mysql_fetch_array($vtb)) {
?>
<tr>
<td><img src="images/qe.png" width="19" height="17"/><b><font size=""><?php echo $r[2] ?></font></b></td></tr>
<tr><td><input name="a1" type="radio" value="" /><?php echo $r[3] ?></td></tr>
<tr><td><input name="a1" type="radio" value="" /><?php echo $r[4] ?></td></tr>
<tr><td><input name="a1" type="radio" value="" /><?php echo $r[5] ?></td></tr>
<tr> <td><input name="a1" type="radio" value="" /><?php echo $r[6] ?></td>
</tr>
<?php
}
?>
</table>
</form>
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
$viewid=$_REQUEST['sid'];
$vtb=mysqli_query($link,"select *from tblexam where sid='$viewid'");
//$r=mysql_fetch_array($vtb);
?>
<form action="" method="post">
<table width="200" border="1">
<?php
while($r=mysqli_fetch_array($vtb, MYSQLI_NUM))
{
?>
<tr>
<td><img src="images/qe.png" width="19" height="17"/><b><font size=""><?php echo $r[2] ?></font></b></td></tr>
<tr><td><input name="a<?php echo $r[0] ?>" type="radio" value="1" /><?php echo $r[3] ?></td></tr>
<tr><td><input name="a<?php echo $r[0] ?>" type="radio" value="2" /><?php echo $r[4] ?></td></tr>
<tr><td><input name="a<?php echo $r[0] ?>" type="radio" value="3" /><?php echo $r[5] ?></td></tr>
<tr> <td><input name="a<?php echo $r[0] ?>" type="radio" value="4" /><?php echo $r[6] ?></td>
</tr>
<?php
}
?>
</table>
</form>
I assume $r[0] is your question_id.
change it to your question_id location if it isn't.
Please be aware mysql_ is now deprecated and you should either be using MySQLi or PDO instead.

PHP QUIZ not displaying ques and its options when user run test

This is the code which is responsible for displaying the ques and its options
if(isset($_SESSION['stdname']))
{
$result=executeQuery("select qnid, stdanswer,answered from studentquestion where stdid=".$_SESSION['stdid']." and testid=".$_SESSION['testid'].";");
$already_answered_qns = array();
while($r1=mysql_fetch_array($result))
{
array_push($already_answered_qns, $r1['qnid']);
}
// select a question excluding the already answered questions
$result=executeQuery("select * from question where testid=".$_SESSION['testid']." and qnid not in (".implode(', ', $already_answered_qns).") Order by rand() ");
$r=mysql_fetch_array($result);
?>
<div class="tc">
<table border="0" width="100%" class="ntab">
<tr>
<th style="width:40%;"><h3><span id="timer" class="timerclass"></span></h3></th>
<th style="width:40%;"><h4 style="color: #af0a36;">Question No: <?php echo $_SESSION['qn']; ?> </h4></th>
<th style="width:20%;"><h4 style="color: #af0a36;"><input type="checkbox" name="markreview" value="mark"> Mark for Review</input></h4></th>
</tr>
</table>
<textarea cols="100" rows="8" name="question" readonly style="width:96.8%;text-align:left;margin-left:2%;margin-top:2px;font-size:120%;font-weight:bold;margin-bottom:0;color:#0000ff;padding:2px 2px 2px 2px;"><?php echo htmlspecialchars_decode($r['question'],ENT_QUOTES); ?></textarea>
<table border="0" width="100%" class="ntab">
<tr><td> </td></tr>
<tr><td >1. <input type="radio" name="answer" value="optiona" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optiona")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optiona'],ENT_QUOTES); ?></input></td></tr>
<tr><td >2. <input type="radio" name="answer" value="optionb" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optionb")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optionb'],ENT_QUOTES); ?></input></td></tr>
<tr><td >3. <input type="radio" name="answer" value="optionc" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optionc")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optionc'],ENT_QUOTES); ?></input></td></tr>
<tr><td >4. <input type="radio" name="answer" value="optiond" <?php if((strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"review")==0 ||strcmp(htmlspecialchars_decode($r1['answered'],ENT_QUOTES),"answered")==0)&& strcmp(htmlspecialchars_decode($r1['stdanswer'],ENT_QUOTES),"optiond")==0 ){echo "checked";} ?>> <?php echo htmlspecialchars_decode($r['optiond'],ENT_QUOTES); ?></input></td></tr>
<tr><td> </td></tr>
<tr>
<th style="width:80%;"><h4><input type="submit" name="<?php if($final==true){ echo "viewsummary" ;}else{ echo "next";} ?>" value="<?php if($final==true){ echo "View Summary" ;}else{ echo "Next";} ?>" class="subbtn"/></h4></th>
<th style="width:12%;text-align:right;"><h4><input type="submit" name="previous" value="Previous" class="subbtn"/></h4></th>
<th style="width:8%;text-align:right;"><h4><input type="submit" name="summary" value="Summary" class="subbtn" /></h4></th>
</tr>

how to save radio button has been selected at the time the page is reloaded?

how to save radio button has been selected at the time the page is reloaded? suppose I have selected some of the radio button and then reload the browser page. but after the page is reloaded radio buttons have been still the same. I use Codeigniter.
here is my view code
<div class="container">
<?php
$no=1;
foreach($hasil->result() as $row):
?>
<form action="<?php echo base_url();?>mahasiswa/hasil" method="post" class="form" enctype="multipart/form-data" name="form" onsubmit="stopCounter();">
<input type="hidden" name="id_soal[<?php echo $row->id_soal;?>]" value="<?php echo $row->id_soal;?>" />
<table>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td><b><?php echo $no.'. ';?></b></td>
<td><?php echo $row->pertanyaan;?></td>
</tr>
<tr>
<td rowspan="5"> </td>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="A" />a. <?php echo $row->a;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="B" />b. <?php echo $row->b;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="C" />c. <?php echo $row->c;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="D" />d. <?php echo $row->d;?></label></span></td>
</tr>
<tr>
<td><span class="radio"><label><input type="radio" name="jawab[<?php echo $row->id_soal;?>]" value="E"/>e. <?php echo $row->e;?></label></span></td>
</tr>
<?php
$no++;
?>
<input type="hidden" name="id_sesi" value="<?php echo $row->id_sesi;?>" />
<input type="hidden" name="jumlah" value="<?php echo $jumlah;?>" />
<?php
endforeach;
?>
<tr>
<td colspan="2"> </td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Selesai" class="btn btn-default"/></td>
</tr>
</table>
</form>
</div>
here my controller code
public function mulai_tes(){
$id=$this->uri->segment(3);
$this->load->model('m_mahasiswa');
$result=$this->m_mahasiswa->data_mahasiswa();
foreach($result->result() as $row):
$data['user']=$row->username;
$data['nama']=$row->nama;
endforeach;
$cek=$this->m_mahasiswa->validasi_tes($id,$data['user']);
foreach($cek->result() as $c){
if($c->id_sesi==$id){ ?>
<script type="text/javascript" language="javascript">
alert("Anda telah mengikuti tes soal ini");
</script>
<?php
echo "<meta http-equiv='refresh' content='0; url=".base_url()."mahasiswa/tes'>";
}
}
$data['hasil']=$this->m_mahasiswa->mulaites($id);
$data['jumlah']=$data['hasil']->num_rows();
$data['judul']='Mulai Tes';
$this->load->view('elearning/template',$data);
}
How I set cookie and where I can put cookie code in controller?
Check which radio(s) was selected in controler (http://ellislab.com/codeigniter%20/user-guide/libraries/input.html).
Pass this data to view (http://ellislab.com/codeigniter/user-guide/general/views.html).
Mark radio(s) as selected within view depending on data passed by controller (Assign an initial value to radio button as checked).

value getting array in php

I am doing a online test I fetch question and option from database....
I need to select which option they choosed and question id....to the next page for the answered question or not.....I got only they selected option I need get all value....
my php code as follows
<tr>
<td height="30"><?= $id?></td>
<td height="30" colspan="2"><?= $question ?></td>
</tr>
<?php
if($option1!='') { ?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option1?>" /></td>
<td height="30" colspan="2"><?= $option1?></td>
</tr>
<?php }?>
<?php if($option2!='') {?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option2?>" /></td>
<td height="30" colspan="2"><?= $option2?></td>
</tr><?php }?>
<?php if($option3!='') {?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option3?>" /></td>
<td height="30" colspan="2"><?= $option3?></td>
</tr><?php }?>
<?php if($option4!='') {?>
<tr>
<td height="30"><input type="radio" name="answer[<? echo $id?>]" value="<?php echo $id?>-<?php echo $option4?>" /></td>
<td height="30" colspan="2" ><?= $option4?></td>
</tr>
<? }
$id will have the same value for all your options.
So you might want to put name="answer" instead:
<input type="radio" name="answer" value="<?php echo $id?>-<?php echo $option2?>" />
On your result page $_GET['answer'] should then have the correct value.
You could send all the answers as hidden elements:
<input type="hidden" name="allanswers[]" value="Answer 1" />
<input type="hidden" name="allanswers[]" value="Answer 2" />
<input type="hidden" name="allanswers[]" value="Answer 3" />
<input type="hidden" name="allanswers[]" value="Answer 4" />

Categories