setting default value to radio button php - php

i want to set a default value to radio buttons for my form. the default value should match the value from database. i know how to set default value that is my by using checked="checked". in my case, the problem lies in how to use database value as default in radiobuttons.
<tr>
<td>ASP.NET</td>
<td><input type="radio" name="asp" value="not at all competent" ></td>
<td><input type="radio" name="asp" value="little competent" ></td>
<td><input type="radio" name="asp" value="moderately competent"</td>
<td><input type="radio" name="asp" value="extremely competent"></td>
</tr>

You can do it like the following:
<?php
//Retrieve your value here
$query="select * from technical_skills where student_id='$student_id'";
$result=mysqli_query($conn,$query);
$row=mysqli_fetch_array($result);
// Assign the column to the variable
$dbValue = $row['columnname'];
?>
<tr>
<td>ASP.NET</td>
<td><input type="radio" name="asp" value="not at all competent"<?php echo ($dbValue=='not at all competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="little competent"<?php echo ($dbValue=='little competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="moderately competent"<?php echo ($dbValue=='moderately competent'?' checked=checked':''); ?> ></td>
<td><input type="radio" name="asp" value="extremely competent"<?php echo ($dbValue=='extremely competent'?' checked=checked':''); ?> ></td>
</tr>

You can define an empty variable for each option and set them to 'checked=checked' according to the DB returned value.
example:
$not_competent = ''
if($dbResult == 'not_competent') $not_competent = 'checked=checked';
Do the same with the other options and then print the table.
Write the table between "..." so PHP can pharse the variables and show
the proper info
print "<tr>
<td>ASP.NET</td>
<td><input type='radio' name='asp' value='not at all competent' $not_competent></td>
<td><input type='radio' name='asp' value='little competent' $little competent></td>
<td><input type='radio' name='asp' value='moderately competent' $moderately_competent></td>
<td><input type='radio' name='asp' value='extremely competent' $extremely_competent></td>
</tr>";
Cheers!

Related

Html table with data from mysql

I'm making a form like this one:
<?php
echo '<tr>
<td><strong>Kamp</strong></td>
<td width="80px"><strong>1X2</strong></td>
<td><strong>Resultat</strong></td></tr>';
$no = 1;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['kamp'].'</td>
<td><input type="radio" name="k1" id="k11" value="1" />1<input type="radio" name="k1" id="k1x" value="X" />X<input type="radio" name="k1" id="k12" value="2" />2</td>
<td><input name="k1r" type="text" id="k1r" placeholder="X-X" /></td>
</tr>
';
$no++;
}?>
And it echo a nice form with 3 rows from my database in the first <td>. It is like a betting-game, so I have the mathes in my database. But as it is right now, the user will check the radiobuttons at 3 different mathes, but submit the same, if you understand. How can I make it 3 different inputs?
I usually use counter in my loop
$no = 1;
while ($row = mysqli_fetch_array($query))
{
echo '<tr>
<td>'.$row['kamp'].'</td>
<td><input type="radio" name="k1_'.$no.'" id="k11_'.$no.'" value="1" />1<input type="radio" name="k1_'.$no.'" id="k1x_'.$no.'" value="X" />X<input type="radio" name="k1_'.$no.'" id="k12_'.$no.'" value="2" />2</td>
<td><input name="k1r_'.$no.'" type="text" id="k1r_'.$no.'" placeholder="X-X" /></td>
</tr>
';
$no++;
if you want multiple input fields with the same name you can create an array of input values with brakets
<input type="radio" name="k1[]" id="k11" value="1" />
See: http://www.php.net/manual/en/faq.html.php#faq.html.arrays for more infomation.

to get the form data from multiple checkbox

As i want to get the form data and want to process that data and store into database..I am getting all checked box values but i am enable to get the text value although i used $_POST['text-name'] in the code...Please help me to get the
error..My code is Below
if(isset($_POST['give-score'])&&!empty($_POST['checked'])){
$employeedetails = $_POST['checked'];
$score = $_POST['score'];
$username = $employeedetails[1];
$workname = $employeedetails[2];
changeworkstatus($username,$workname,$con);
$workname = $employeedetails[2];
addscorepoints($workname,$score,$con);
}else{
echo "";
}
and my form html code is below
<td><input type="checkbox" name="checked[]" id="employeework" value="" style="align: center"></td>
<td><input type="checkbox" name="checked[]" id="employeework"value="<?php echo $results['username']; ?>"><?php echo $results['username']; ?></td>
<td><input type="checkbox" name="checked[]" id="employeework"value="<?php echo $results['work_name'];?>"><?php echo $results['work_name'];?></td>
<td><input type="text" name="score" id="score" placeholder="Your Score Here"></td>
<td><input type="submit" name="give-score"></td>
php part used in the table are working fine..but the input[type=text] i am not getting that value..
PHP arrays index starts from 0 not 1. then you must change this lines:
$username = $employeedetails[0];
$workname = $employeedetails[1];
Hope this help you!
I think there is no problem in your code even then I am providing the following code which I've tried. I am getting all values from the form elements.
Code of the html file
<html>
<body>
<form action="test.php" method="POST"> Checked value:
<td><input type="checkbox" name="checked[]" id="t1" value="test1">test1</td>
<td><input type="checkbox" name="checked[]" id="t2" value="test2">test2</td>
<td><input type="checkbox" name="checked[]" id="t3" value="test3">test3</td>
<td><input type="text" name="score" id="score" placeholder="Your Score Here"> </td>
<td><input type="submit" name="give-score"></td>
</form>
</body>
</html>
Code of PHP file
<?php
if(isset($_POST['give-score'])&&!empty($_POST['checked'])){
$employeedetails = $_POST['checked'];
echo $score = $_POST['score']."<br>";
echo $username = $employeedetails[0]."<br>";
echo $workname = $employeedetails[1]."<br>";
echo $workname = $employeedetails[2]."<br>";
}else{
echo "No data found";
}
?>
I hope this will help you!

Keep text value after submit

I'm trying to find how to retain my text value after submit, so the text that i submit is still keep in the textbox, there's so many reference in internet but too hard for me to understand (newbie here), so i'd like to ask here, if anyone have some solution.
Here's my form code:
echo
"<form method='post' action='process.php'>
<tr>
<td>Nama Jurusan</td>
<td>:</td>
<td><input type='text' name='jurusan' size='50%'></td>
</tr>
<tr>
<td>Nama Laboratorium</td>
<td>:</td>
<td><input type='text' name='lab' size='50%></td>
</tr>
<input name='submit' type='submit' id='ajukan' value='Ajukan'>
</form>";
As you can see, i was placing the form inside echo.
Here's my process.php code:
<?php
if(isset($_REQUEST['submit'])) {
include "../conf/koneksi.php";
$jurusan = $_POST['jurusan'];
$lab = $_POST['lab'];
$urutkan= "ALTER TABLE tb_pengusul AUTO_INCREMENT = 1";
mysql_query($urutkan);
$input = mysql_query("INSERT INTO tb_pengusul (nama_jurusan,nama_laboratorium)
VALUES ('$jurusan','$lab')") or die (mysql_error());
echo "<script language=\"Javascript\">\n";
echo "window.alert('Input sukses !')";
echo "</script>";
echo "<META HTTP-EQUIV=\"REFRESH\" CONTENT=\"0;URL='../koordinator.php?url='\">";
}
?>
Use Post value as,
This will works only if you form page and submit code are in same page ( process.php )
<?php echo"<form method='post' action='process.php'>
<tr><td>Nama Jurusan</td>
<td>:</td>
<td><input type='text' name='jurusan' value='".$_POST['jurusan']."' size='50%'></td>
</tr>
<tr>
<td>Nama Laboratorium</td>
<td>:</td>
<td><input type='text' name='lab' value='".$_POST['lab']."' size='50%'></td>
</tr>
<input name='submit' type='submit' id='ajukan' value='Ajukan'>
</form>";?>
Also you have error in your code in line
<td><input type='text' name='lab' size='50%></td>
it should be <td><input type='text' name='lab' size='50%'></td>
If your form is still available in the process.php file, change it like this:
echo "<form method='post' action='process.php'>
<tr><td>Nama Jurusan</td>
<td>:</td>
<td><input type='text' name='jurusan' size='50%'";
if (isset($_POST['jurusan']))
{
echo " value=\'$_POST['jurusan']\'";
}
echo "></td>
</tr>
<tr>
<td>Nama Laboratorium</td>
<td>:</td>
<td><input type='text' name='lab' size='50%'";
if (isset($_POST['lab']))
{
echo " value=\'$_POST['lab']\'";
}
echo "></td>
</tr>
<input name='submit' type='submit' id='ajukan' value='Ajukan'>
</form>";
you need to use PHP sessions.
add the following string to your process.php page
session_start();
$_SESSION['prev_values'] = $_POST;
and the following to your form page
$lab = "";
$jur = "";
session_start();
if(isset($_SESSION['prev_values'])){
$jur = $_SESSION['prev_values']['jurusan'];
$lab = $_SESSION['prev_values']['lab'];
}
echo
"<form method='post' action='process.php'>
<tr>
<td>Nama Jurusan</td>
<td>:</td>
<td><input type='text' name='jurusan' size='50%' value='$jur'></td>
</tr>
<tr>
<td>Nama Laboratorium</td>
<td>:</td>
<td><input type='text' name='lab' size='50%' value='$lab'></td>
</tr>
<input name='submit' type='submit' id='ajukan' value='Ajukan'>
</form>";
SECURITY NOTICE:
Please note: this script is basic and it is vulnerable to XSS (just to name one). As a rule of thumb, you should NEVER display directly user inputs without some form of sanitation

Data some is errased when update button is pressed

Please, I am having a problem when updating data in the database through a form. When ever I press the Update button to submit any changes made to a record, all the data in the mysql fields corresponding to drop list controls is errased. I do not know what is causing this problem. Here is the code:
<?php
//include database connection
include 'db_connect.php';
// get value of object id that was sent from address bar
$c_id = $_GET['c_id'];
//check any user action
$action = isset( $_POST['action'] ) ? $_POST['action'] : "";
if($action == "update"){ //if the user hit the submit button
//write our update query
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "UPDATE collections
SET
ctitle = '".$mysqli->real_escape_string($_POST['ctitle'])."',
csubject = '".$mysqli->real_escape_string($_POST['csubject'])."',
creference = '".$mysqli->real_escape_string($_POST['creference'])."',
cyear = '".$mysqli->real_escape_string($_POST['cyear'])."',
cobjecttype = '".$mysqli->real_escape_string($_POST['cobjecttype'])."',
cmaterial = '".$mysqli->real_escape_string($_POST['cmaterial'])."',
ctechnic = '".$mysqli->real_escape_string($_POST['ctechnic'])."',
cwidth = '".$mysqli->real_escape_string($_POST['cwidth'])."',
cheight = '".$mysqli->real_escape_string($_POST['cheight'])."',
cperiod = '".$mysqli->real_escape_string($_POST['cperiod'])."',
cmarkings = '".$mysqli->real_escape_string($_POST['cmarkings'])."',
cdescription = '".$mysqli->real_escape_string($_POST['cdescription'])."',
csource = '".$mysqli->real_escape_string($_POST['csource'])."',
cartist = '".$mysqli->real_escape_string($_POST['cartist'])."'
where c_id='".$mysqli->real_escape_string($_REQUEST['c_id'])."'";
//execute the query
if( $mysqli->query($query) ) {
//if updating the record was successful
echo "The record was updated.";
}else{
//if unable to update new record
echo "Database Error: Unable to update record.";
}
}
//select the specific database record to update
$query = "SELECT c_id, ctitle, csubject, creference, cyear, cobjecttype, cmaterial, ctechnic, cwidth, cheight, cperiod, cmarkings, cdescription, csource, cartist, cfilename
FROM collections
WHERE c_id='".$mysqli->real_escape_string($_REQUEST['c_id'])."'
limit 0,1";
//execute the query
$result = $mysqli->query( $query );
//get the result
$row = $result->fetch_assoc();
//assign the result to certain variable so our html form will be filled up with values
$c_id = $row['c_id'];
$ctitle = $row['ctitle'];
$csubject = $row['csubject'];
$creference = $row['creference'];
$cyear = $row['cyear'];
$cobjecttype = $row['cobjecttype'];
$cmaterial = $row['cmaterial'];
$ctechnic = $row['ctechnic'];
$cwidth = $row['cwidth'];
$cheight = $row['cheight'];
$cperiod = $row['cperiod'];
$cmarkings = $row['cmarkings'];
$cdescription = $row['cdescription'];
$csource = $row['csource'];
$cartist = $row['cartist'];
$cfilename = $row['cfilename'];
?>
<!--we have our html form here where new object information will be entered-->
<table align=left>
<tr>
<td> <?php echo '<img src="./images/'.$cfilename.'" width="300" height="400" />'; ?> </td>
</tr>
<table>
<form action='#' method='post' border='0'>
<table>
<tr>
<td>TITLE</td>
<td><input type='text' name='ctitle' value='<?php echo $ctitle; ?>' /></td>
</tr>
<tr>
<td>SUBJECT</td>
<td><input type='text' name='csubject' value='<?php echo $csubject; ?>' /></td>
</tr>
<tr>
<td>REFERENCE No.</td>
<td><input type='text' name='creference' value='<?php echo $creference; ?>' /></td>
</tr>
<tr>
<td>YEAR</td>
<td><input type='text' name='cyear' value='<?php echo $cyear; ?>' /></td>
<tr><td>OBJECT TYPE</td>
<td>
<select name="cobjecttype" id="cobjecttype" tabindex="">
<option value="">---Select object type---</option>
<option value="ceramic">Ceramic</option>
<option value="clock">Clock</option>
<option value="gold">Gold and silverware</option>
<option value="mask">Mask</option>
<option value="painting">Painting</option>
<option value="sculpture">Sculpture</option>
<option value="tapestry">Tapestry</option>
</select>
</td></tr>
<tr><td>MATERIAL USED</td>
<td>
<select name="cmaterial" id="cmaterial" tabindex="" >
<option value="">---Select Material---</option>
<option value="brass">Brass</option>
<option value="oil">Oil</option>
<option value="wood">Wood</option>
<option value="carved">Canvas/Cotton/Fabric/Linen/Wool</option>
</select>
</td></tr>
<tr><td>TECHNIC</td>
<td>
<select name="ctechnic" id="ctechnic" tabindex="7" >
<option value="">---Select Technic---</option>
<option value="cast">Cast</option>
<option value="carved">Carved</option>
<option value="etched">Etched</option>
</select>
</td></tr>
<tr>
<td>WIDTH</td>
<td width="100"><input name="cwidth" type="text" id="cwidth" value="<?php echo $cwidth; ?>" size="10"></td>
</tr>
<tr>
<td>HEIGHT</td>
<td width="100"><input name="cheight" type="text" id="cheight" value="<?php echo $cheight; ?>" size="10"></td>
</tr>
<tr>
<td>PERIOD</td>
<td width="100"><input name="cperiod" type="text" id="cperiod" value="<?php echo $cperiod; ?>" size="30"></td>
</tr>
<tr>
<td>MARKINGS</td>
<td width="100"><input name="cmarkings" type="text" id="cmarkings" value="<?php echo $cmarkings; ?>" size="30"></td>
</tr>
<tr>
<td>DESCRIPTION</td>
<td width="400"><textarea name="cdescription" rows="2" cols="50" id="cdescription" value="<?php echo $cdescription; ?>"></textarea></td></tr>
<tr>
<td>SOURCE</td>
<td width="100"><input name="csource" type="text" id="csource" value="<?php echo $csource; ?>" size="30"></td>
</tr>
<tr>
<td>ARTIST</td>
<td width="100"><input name="cartist" type="text" id="cartist" value="<?php echo $cartist; ?>" size="30"></td>
</tr>
<td></td>
<td>
<!-- so that we could identify what record is to be updated -->
<input type='hidden' name='c_id' value='<?php echo $c_id ?>' />
<!-- we will set the action to update -->
<input type='hidden' name='action' value='update' />
<input type='submit' value='Save' />
<a href='gallery.php'>Back to display page</a>
</td>
</tr>
</table>
</form>
Can someone help to identify what the problem is?
Such problem occur when you dont validate your POST data correctly. In your code, you are updating your records directly, by using mysql_real_escape_string($variable). But although this might fix some security issues will not validated every data if it is present or not.
Validate your variables to be present and hold data before updating to the query.
you post a form with the method POST, but get the c_id with $_GET
change it to $_POST['c_id'] or $_REQUEST['c_id'] ...

Graying out HTML form elements

I want to let the user select one "row" to use to submit the with to request a report type. How can I put radio buttons in the first column of a table and whichever is selected is the active row that gets sent to the next page via the submit button?
I think Andreas is on the right track, but it's not as useful as it could be. This should be a bit better:
<?php
blah ...
echo <<<HTML
<form action="handler.php" action="post">
<table>
HTML;
foreach ($rows as $row)
{
$id = $row['id'];
$text = $row['text']; // escape this unless you know it's safe
echo <<<HTML
<tr>
<td><input type="radio" value="$id" name="theRadioButton" /></td>
<td><input type="text" name="textfield_$id" value="$text" /></td>
</tr>
HTML;
}
echo <<<HTML
</table>
</form>
HTML;
form handler:
<?php
$id = isset($_POST['theRadioButton']) ? $_POST['theRadioButton'] : null;
if ($id)
{
$textfield = $_POST["textfield_$id"];
}
?>
If you want to do it in pure PHP, i guess you could do this:
<form action="ascript.php" action="post">
<table>
<tr>
<td><input type="radio" value="row1" name="theRadioButton" /></td>
<td><input type="text" name="row1textfield" /></td>
</tr>
<tr>
<td><input type="radio" value="row2" name="theRadioButton" /></td>
<td><input type="text" name="row2textfield" /></td>
</tr>
<tr>
<td><input type="radio" value="row3" name="theRadioButton" /></td>
<td><input type="text" name="row3textfield" /></td>
</tr>
</table>
</form>
ascript.php
<?php
if ($_POST['theRadioButton'] == "row1") {
echo $_POST['row1textfield'];
// Handle row 1 ..
}
else if ($_POST['theRadioButton'] == "row2") {
echo $_POST['row2textfield'];
// Handle row 2 ..
}
else if ($_POST['theRadioButton'] == "row3") {
echo $_POST['row3textfield'];
// Handle row 3 ..
}
?>
However, if you're willing to use some jQuery, you could just name the textfields the same thing and disable the fields you're not going to use. Here's a fiddle: http://jsfiddle.net/rrvQu/1/

Categories