check a radio button based on value retrieved from database - php

I have radio buttons that want to check if value retrieved from database is 'Yes'.
on this same forum a same question is there but after trying that and still no success. also i can't comment there anything about my question because i don't have sufficient reputation.
link to the question same as mine is:
How to set the value for Radio Buttons When edit?
Below is the Form code which displays fetched data:
<?php
error_reporting(0);
$server="localhost";
$user="root";
$password="";
$database="camouflage_studio";
$con = mysqli_connect($server,$user,$password,$database);
if (mysqli_connect_errno())
{
echo "Connection Error: " . mysqli_connect_error();
}
//receiving CINC from search form and getting record on it's basis
$cnic = $_POST['search'];
$data = "SELECT * FROM personal_detail WHERE CNIC='$cnic'";
if($query = mysqli_query($con, $data)){
$data2 = mysqli_fetch_array($query); //i think it's used for to know the total number of rows fetched/retrieved
}else{ echo "Query Not Executed!";}
?>
<!DOCTYPE html>
<html>
<head>
<title>Camouflage Studio - Welcome</title>
<!-- My CSS coding starts here -->
</head>
<body>
<!-- form to display Retrieved/Fetched data-->
<center>
<form action="update.php" method="post">
<fieldset style="width:50%"><legend>Please do the required changes</legend><br>
<label for="Name">Name :<br></label><input name="name" type="text" size="20" maxlength="40" value="<?php echo $data2[Name]?>"><br>
<label for="CNIC">CNIC :<br></label><input name="cnic" type="text" size="20" maxlength="15" value="<?php echo $data2[CNIC]?>"><br>
<label for="Date">Booking Date :<br></label><input name="booking-date" type="date" size="20" value="<?php echo $data2[Date]?>"><br>
<!-- <label for="Ocassion">Ocassion :<br></label> -->
<label for="Ocassion">Ocassion :<br></label><input name="ocassion" type="text" size="20" maxlength="15" value="<?php echo $data2[Ocassion]?>"><br>
<label for="Address">Address :<br></label><input name="address" type="text" size="20" maxlength="11" value="<?php echo $data2[Address]?>"><br>
<label for="Phone Number">Phone Number :<br></label><input name="phone-no" type="text" size="20" maxlength="11" value="<?php echo $data2[Phone_No]?>"><br>
<label for="Bride Mobile">Bride Mobile :<br></label><input name="bride-mobile" type="number" size="20" maxlength="11" value="<?php echo $data2[Bride_Mobile]?>"><br>
<label for="Groom Mobile">Groom Mobile :<br></label><input name="groom-mobile" type="number" size="20" maxlength="11" value="<?php echo $data2[Groom_Mobile]?>"><br>
<label for="Family Mobile">Family Mobile :<br></label><input name="family-mobile" type="number" size="20" maxlength="11" value="<?php echo $data2[Family_Mobile]?>"><br>
<label for="Email">Email :<br></label><input name="email" type="text" size="20" maxlength="30" value="<?php echo $data2[EMail]?>"><br>
<label for="Who may I Thank for Refering You?">Who may I Thank for Refering You? :<br></label><input name="refering" type="text" size="20" maxlength="40" value="<?php echo $data2[Referring]?>"><br>
<label for="Do you provide consent to share images on our official web page">Do you provide consent to share images on our official web page? :<br><br></label><input type="radio" name="share" <?php echo ($data2[Share]=='Yes')?'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="share" <?php echo ($data2[Share]=='Yes')?'checked':'' ?> value="No">No<br><br>
<label for="If yes, with Identity">If yes, with Identity? :<br><br></label><input type="radio" name="permission" <?php echo ($data2[Permission]=='Yes')?'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="permission" <?php echo ($data2[Permission]=='Yes')?'checked':'' ?> value="No">No<br><br>
<!-- To center the button i'm embedding the buttons in a paragraph with an id as well. the id is used for CSS in head -->
<p id="btn">
<input type="submit" value="Update Record" name="submit_display_data_form" style="font-size:16px"></p>
</fieldset>
</form>
</center>
</body>
</html>

$Share = 'Yes';
$Permission = 'Yes';
<label for="Do you provide consent to share images on our official web page">
Do you provide consent to share images on our official web page? :
</label>
<input type="radio" name="share" <?php echo ($Share =='Yes')? 'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="share" <?php echo ($Share =='No')? 'checked':'' ?> value="No">No<br><br>
<label for="If yes, with Identity">If yes, with Identity? :<br><br></label>
<input type="radio" name="permission" <?php echo ($Permission=='Yes')?'checked':'' ?> value="Yes">Yes<br>
<input type="radio" name="permission" <?php echo ($Permission=='No')?'checked':'' ?> value="No">No<br><br>

You have to understand what value will be stored in $_POST['key1'].
If nothing is checked, $_POST['key1'] doesn't exist : !isset($_POST['key1']) ;
If first radio is checked, $_POST['key1'] = 'Yes' ;
If second radio is checked, $_POST['key1'] = 'No' ;
If you want to pre-check "No" when nothing is checked, you should test if the data does not exist or if the value is anything except 'Yes'.
In that way, you're sure that you will have 'Yes' or 'No' after submitting the form.
<input type="radio" name="key1" value="Yes" <?php if ( isset($data['key1']) && $data['key1'] == 'Yes' ) echo 'checked' ; ?> />
<input type="radio" name="key1" value="No" <?php if ( ! isset($data['key1']) || $data['key1'] !== 'Yes' ) echo 'checked' ; ?> />
<input type="radio" name="key2" value="Yes" <?php if ( isset($data['key1']) && $data['key2'] == 'Yes' ) echo 'checked' ; ?> />
<input type="radio" name="key2" value="No" <?php if ( ! isset($data['key1']) || $data['key2'] !== 'Yes' ) echo 'checked' ; ?> />

Related

trying to show the email and password Written in email and password inputs issue

I´m trying to show in a '<pre></pre>' the values that are passed in the form.
But Its not working and give my some errors:
-> Notice: Undefined index: email in
-> Notice: Undefined index: pass
-> Notice: Undefined index: remember
I already tried put isset like this:
$f['email'] = mysql_real_escape_string(isset($_POST['email']));
$f['pass'] = mysql_real_escape_string(isset($_POST['pass']));
$f['save'] = mysql_real_escape_string(isset($_POST['remember']));
And this way above I dont have erros but the data that I write in the form inputs dont show in my
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
Can you see something that Im doing wrong?
My code:
<?php
if(isset($_POST['sendLogin']))
{
$f['email'] = mysql_real_escape_string($_POST['email']);
$f['pass'] = mysql_real_escape_string($_POST['pass']);
$f['save'] = mysql_real_escape_string($_POST['remember']);
echo '<pre class="debug">';
print_r($f);
echo '</pre>';
}
?>
<?php
if(!isset($_GET['remember']))
{
?>
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" name="<?php if($f['email']) echo $f['email']; ?>" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" name="<?php if($f['pass']) echo $f['pass']; ?>" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
I think that you have made mistake with name attribute. You should have static name="email" and name="pass" in the inputs. While your current name attribute change to value:
<input type="text" class="radius" name="email" value="<?php if($f['email']) echo $f['email']; ?>" />
<input type="password" class="radius" name="pass" value="<?php if($f['pass']) echo $f['pass']; ?>" />
Your inputs must have the name attribute in order to be accessible in the PHP
<form name="login" action="" method="post">
<label>
<span>Email:</span>
<input type="text" class="radius" value="<?php if($f['email']) echo $f['email']; ?>" name="email" />
</label>
<label>
<span>Password:</span>
<input type="password" class="radius" value="<?php if($f['pass']) echo $f['pass']; ?>" name="password" />
</label>
<input type="submit" value="Login" name="sendLogin" class="btn" />
<div class="remember">
<input type="checkbox" name="remember" value="1"
<?php if(isset($f['save'])) echo 'checked="checked"' ?>
/>
In <input type="text"> as well as in <input type="password"> block, provide a meaningful value for the name attribute.
Here in your code provide name="email" and for the next one name="pass".
Also to display the email/pass values after the form submission, give it in value attribute.
Right now you wrongly given inside the name attribute.
value="<?php if($f['email']) echo $f['email']; ?>" and
value="<?php if($f['pass']) echo $f['pass']; ?>"

undefined index after submitting form

<?php
include 'includes/connectie.php';
$product_id=$_GET['id'];
$sql = "SELECT * FROM `producten` WHERE product_id='$product_id'";
$sql_result = $dbh->query($sql);
foreach($sql_result as $row)
{
$prijs=$row['prijs'];
$product_naam=$row['product_naam'];
$product_categorie=$row['product_categorie'];
$product_specificaties=$row['product_specificaties'];
$foto=$row['foto'];
$product_id=$row['product_id'];
$product_soort=$row['product_soort'];
echo "Product id nummer:", $product_id;
}
//$_SESSION['prijs'] = $prijs;
if ($_SERVER["REQUEST_METHOD"] == "POST"){
if (!empty($product_naam) && !empty($product_specifcaties) && !empty($product_categorie) &&
!empty($prijs)
&& !empty($product_soort)) {
print "Product aangepast!";
$sql = "UPDATE producten
SET prijs='$prijs', product_naam='$product_naam',
product_specificaties='$product_specificaties',
product_categorie='$product_categorie', product_soort='$product_soort'
WHERE product_id='$product_id'";
$query = $db->prepare( $sql );
$result = $query->execute();
exit();
}
}
?>
<html>
<form name="admin" action="producten_echt_aanpassen.php" method="POST" id="adminform" enctype="multipart/form-data">
<p>
<label for 'product_naam'>Naam: </label><br>
<input type="text" name="product_naam" value="<?php print $product_naam; ?>"/>
</p>
<p> <label for 'product_specificaties'>Specificaties: </label><br>
<textarea rows= "4" cols="50" name="product_specificaties"><?php print $product_specificaties; ?>
</textarea>
</p>
<p>
<label for 'prijs'>Prijs: </label><br>
<input type="text" name="prijs" value="<?php print $prijs; ?>"/>
</p>
<p>
<label for 'product_categorie'>Iphone: </label><br>
<input type="text" name="product_categorie" value="<?php print $product_categorie; ?>"/>
</p>
<p>
<label for 'product_soort'>Soort: </label><br>
<input type="text" name="product_soort" value="<?php print $product_soort; ?>"/>
</p>
<br/>
<label for 'uploadfile'>Kies foto <img src="<?php print $foto; ?>"></label><br>
<input type="file" name="file" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
</html>
the variable is loaded in a form in which product details can be changed. the form links to this page with the code above. but whenever I submit the form and try to change te detail i get an error of an undefined index. which is what the $_GET does in line 5. The foreach loop needs the index to be defined but whenever the form is submitted, the index in the URL is gone so the loop doesnt produce the variables that need to go to the database. I hope this makes sense. Can anybody help me out please?
Your html does not include a field named id.
You are sending the form as POST not GET, so after you add the correct field in the HTML you need to refer to it as $product_id = $_POST['id'];
No need for enctype="multipart/form-data", it will only be helpful when you are uploading files. Otherwise it can cause you problems.
<p>
<label for 'product_naam'>Naam: </label><br>
<input type="text" name="product_naam" value="<?php print $product_naam; ?>"/>
</p>
<p> <label for 'product_specificaties'>Specificaties: </label><br>
<textarea rows= "4" cols="50" name="product_specificaties"><?php print $product_specificaties; ?>
</textarea>
</p>
<p>
<label for 'prijs'>Prijs: </label><br>
<input type="text" name="prijs" value="<?php print $prijs; ?>"/>
</p>
<p>
<label for 'product_id'>Product ID: </label><br>
<input type="text" name="id" value="<?php print $product_id; ?>"/>
</p>
<p>
<label for 'product_categorie'>Iphone: </label><br>
<input type="text" name="product_categorie" value="<?php print $product_categorie; ?>"/>
</p>
<p>
<label for 'product_soort'>Soort: </label><br>
<input type="text" name="product_soort" value="<?php print $product_soort; ?>"/>
</p>
<br/>
<label for 'uploadfile'>Kies foto <img src="<?php print $foto; ?>"></label><br>
<input type="file" name="file" ><br><br>
<input type="submit" name="submit" value="Submit">
Hope this helps!

how to get input box values which is called by ajax?

<form action="chngschdl.php" method="post">
<fieldset>
<label class="home">Flight Name</label> <select name="select_catalog_query" ><?php echo $options1; ?></select>
<br/><br/>
<label class="home">Starting Route</label> <input class="text" type="text" name="stroute" onKeyUp="numericFilter(this);" /> <label class="home">Deperture Time</label> <input class="text" type="text" name="stdrt" /><br/>
<label class="home">Ending Route</label> <input class="text" type="text" name="enroute" onKeyUp="numericFilter(this);" /> <label class="home">Arrival Time</label> <input class="text" type="text" name="enart" /><br/>
<label class="home">Break Route Number</label> <input class="text" type="number" name="bpn" maxlength='1' onkeyup="Bpoint(this.value)" /><br/>
<label id='bp' > <?php //$i=$_SESSION['point'];$_SESSION['bp']=$broute[$i];?></label>
<button class="btn">Go</button>
</fieldset>
</form>
ajax getting data from:
<?php
session_start();
if( $_SESSION['type']!='admin')
{
header("Location: index.php");
return;
}
$point=$_GET['q'];
$_SESSION['point']=$point;
$bpoption="";
for($i=0;$i<$point;$i++)
{
echo "<label>Break Route[$i]<label> <input class=text type=text name=broute[$i] /> ";
echo "<label>Arrival Time[$i]<label> <input class=text type=text name=bart[$i] /> ";
echo "<label>Departure Time[$i]<label> <input class=text type=text name=bdrt[$i] /> ";
echo "<br/><br/>";
}
?>
i have to collect every data of Break Route, Arrival time and Departure time... how i suppose to get this data and use it in another page?? is there is other way around by not using ajax... bt it must be remembered that Break Route is not predefined...
since your form fields are named like "name[$i]" it doesn't matter how many ajax-populated fields there are.
All you have to do on the page processing the form submit is:
foreach ($_POST["name"] as $name) {
// do things
}

PHP comment form

I have a comment form that I have created. It gets the id from the database and prints out the data that goes with that id, but it also prints out the information into the form. How can I get a blank form, so that the user can add a comment to the record?
This the code for the form:
<form method="post" action="pv.php?id=<?php echo $row['ID']?>&action=<?php echo $form_action ?>">
<fieldset>
<legend></legend>
<p>
<label for="cname">Date Of Birth</label> *
<input id="cname" name="dateofbirth" class="required date" value="<?php echo $row['Date_Of_Birth']?>" /> (eg 1978.11.11)
</p>
<p>
<label for="cgender">Gender</label> *
<input type="radio"
name="gender"
value="Male"
<?php if($row['Gender']=='male'){echo 'checked';}?>/>
Male
<input type="radio"
name="gender"
value="Female"
<?php if($row['Gender']=='female'){echo 'checked';}?>/> Female </td>
</p>
<p>
<label for="curl">Title</label> *
<select name="title" id="title" class="required">
<option value="">Please Select</option>
<option value="Mr" <?php if($row['Title']=='Mr'){echo 'selected';}?>>Mr</option>
<option value="Ms" <?php if($row['Title']=='Ms'){echo 'selected';}?>>Ms</option>
<option value="Mrs" <?php if($row['Title']=='Mrs'){echo 'selected';}?>>Mrs</option>
<option value="Miss" <?php if($row['Title']=='Miss'){echo 'selected';}?>>Miss</option>
<option value="Other" <?php if($row['Title']=='Other'){echo 'selected';}?>>Other</option>
</select>
</p>
<p>
<label for="ccomment">First Name</label> *
<input type="text" name="firstname" value="<?php echo $row['First_Name']?>" maxlength="50" />
</p>
<p>
<label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname"
value="<?php echo $row['Last_Name']?>" maxlength="75" />
</p>
<p>
<label for="ccomment">Address 1</label>*
<input type="text" name="address1"
value="<?php echo $row['Address_Line_1']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">Address 2</label>
<input type="text" name="address2"
value="<?php echo $row['Address_Line_2']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">City</label>*
<input type="text" name="city"
value="<?php echo $row['City']?>" maxlength="50" />
</p>
<p>
<label for="ccomment">Postcode</label>*
<input type="text" name="postcode"
value="<?php echo $row['Postcode']?>" maxlength= "10" /> (eg LE5 5QE)
</p>
<p>
<label for="ccomment">Contact No</label>*
<input type="text" name="contactno"
value="<?php echo $row['Contact_No']?>" maxlength= "12" /> (eg 077448825723)
</p>
<p>
<label for="ccomment">Email</label>*
<input type="text" name="email"
value="<?php echo $row['Email']?>" maxlength= "40"/> (eg info#example.com)
</p>
<p>
<label for="ccomment">Comment</label>
<textarea rows="10" cols="30" name="note"
maxlength= "500"><?php echo $row['Additional_Comment']?></textarea>
</p>
<p>
<input class="submit" type="submit" value="Submit"/>
</p>
<p>
<a href='pv.php'>Main Page</a>
</p>
</fieldset>
</form>
This is the code for printing out the data on the page:
if($_GET['action'] == 'comment') {
$form_action = 'comment_ok';
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM project_data WHERE id='$id'");
$row = mysql_fetch_array($result);
echo'<b>';
echo $row['Date_Of_Birth'];
echo '&nbsp&nbsp';
echo $row['Gender'];
echo '&nbsp&nbsp';
echo $row['Title'];
echo '&nbsp&nbsp';
echo $row['First_Name'];
echo '&nbsp&nbsp';
echo $row['Last_Name'];
echo '&nbsp&nbsp';
echo $row['Address_Line_1'];
echo '&nbsp&nbsp';
echo $row['Address_Line_2'];
echo '&nbsp&nbsp';
echo $row['City'];
echo '&nbsp&nbsp';
echo $row['Postcode'];
echo '&nbsp&nbsp';
echo $row['Contact_No'];
echo '&nbsp&nbsp';
echo $row['Email'];
echo '&nbsp&nbsp';
echo $row['Additional_Comment'];
echo '</b>';
}
And a snippet of the code I am using to send the id to the form:
echo "<td><a href='pv.php?action=edit&id=" . $row['ID'] .
"'>Edit</a>&nbsp&nbsp<a href='pv.php?action=delete_ok&id=" . $row['ID'] .
"'>Delete</a>&nbsp&nbsp**<a href='pv.php?action=comment&id=" . $row['ID'] .
"'>Comment</a></td>"**;
echo "</tr>";
How can I do it?
If you want one page that allows the user to fill in a blank form, see a filled out form and update a form.
You can do the blank form or filled form by checking if an id exist, like this:
<?
if (isset(id)) // Here you can check if the id exists, which means you will be doing an update to the SQL query
echo'
<form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
<!-- Your form here with values -->
e.g. <label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname" value="' . $row['Last_Name'] . '" maxlength="75" />
</form>';
// Put your SQL update here to take the values from the above form
}
elseif (!isset(id)) // Here there is no id so you will want to insert
{
echo'
<form method="post" action="pv.php?id=' . $row['ID'] . '&action=' . $form_action . '">
<!-- Your form here with values -->
e.g. <label for="cemail">Last Name</label> *
<input id="cemail" type="text" name="lastname" maxlength="75" /> // No value here because will insert new record
</form>';
// Put your SQL insert here to create a new record
}
?>
If you may want the user to be able to add a new comment entirely rather than editing the one already associated with their id, I think you would have to do this by giving them the option to add another comment. To do this, I would create a new table and then insert the comments into that, so you have one table, example_users with the user information, and then another table, user_comments, with the user comments, for example:
Table example_users
id = 1
fname=Joe
lname=Bloggs
email=jbloggs#example.com
Table user_comments
id=1
user_id=1
comment=Comment will be saved here
This way, any user can have any number of comments. You could render this on the page by using a foreach statement to render a text box with all their existing comments and then have a blank one at the end for any new ones. Then they can edit any comment and add a new comment.
If you want the form to be blank, then just set the value attribute to empty. E.g value=''. Except for the id field.

Radio Buttons & Sessions, how to get value and use as a session variable

How would i get the value of a radio button and use that in my session so for example;
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage <?= isset($_SESSION['minor']) ? "style=\"display: block;\"" : "style=display: none;\"" ?>">
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1');?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2');?>" />
</div>
as you can see the code calls the name "minor" but even if NO is selected it will still show the div, regardless of the value, i need to pass the value into my isset($_Session['minor']) parameter, how would this be achieved ?
#Xavier: Try --
<input type="radio" name="minor" id="minor" group="underage" class="minor_yes" value="yes" />Yes<br />
<input type="radio" name="minor" id="minor" group="underage" class="minor_no" value="no" />No<br />
<div class="underage"<?php echo isset($_SESSION['minor']) && $_SESSION['minor'] != 'no' ? 'style="display: block;"' : 'style="display: none;"' ?>>
<label>Guardian 1</label>
<input type="text" name="guardian1" id="guardian1" value="<?php echo stickyText('guardian1'); ?>" />
<label>Guardian 2</label>
<input type="text" name="guardian2" id="guardian2" value="<?php echo stickyText('guardian2'); ?>" />
</div>

Categories