I have a form that takes the following inputs:
Name: IBM
Surface(in m^2): 9
Floor: (Checkbox1)
Phone: (Checkbox2)
Network: (Checkbox3)
Button to send to a next php page.
All those values above are represented in a table when i press the submit button.
The first two (name and surname) are properly displayed in the table.
The problem is with the checkboxes. If i select the first checkbox the value in the table should be presented with 1. If its not selected the value in the table should be empty.
echo "<td>$Name</td>"; // works properly
echo "<td>$Surface</td>"; // works properly
echo "<td>....no idea for the checkboxes</td>;
Some part of my php code with the variables:
<?php
if (!empty($_POST))
{
$name= $_POST["name"];
$surface= $_POST["surface"];
$floor= $_POST["floor"];
$phone= $_POST["telefoon"];
$network= $_POST["netwerk"];
if (is_numeric($surface))
{
$_SESSION["name"]=$name;
$_SESSION["surface"]=$surface;
header("Location:ExpoOverzicht.php");
}
else
{
echo "<h1>Wrong input, Pleasee fill in again</h1>";
}
if(!empty($floor) && ($phone) && ($network))
{
$_SESSION["floor"]=$floor;
$_SESSION["phone"]=$phone;
$_SESSION["network"]=$network;
header("Location:ExpoOverzicht.php");
}
}
?>
Second page with table:
<?php
$name= $_SESSION["name"];
$surface= $_SESSION["surface"];
$floor= $_SESSION["floor"];
$phone= $_SESSION["phone"];
$network= $_SESSION["network"];
echo "<table class=\"tableExpo\">";
echo "<th>name</th>";
echo "<th>surface</th>";
echo "<th>floor</th>";
echo "<th>phone</th>";
echo "<th>network</th>";
echo "<th>total price</th>";
for($i=0; $i <= $_SESSION["name"]; $i++)
{
echo "<tr>";
echo "<td>$name</td>"; // gives right output
echo "<td>$surface</td>"; // gives right output
echo "<td>...</td>"; //wrong output (ment for checkbox 1)
echo "<td>...</td>"; //wrong output (ment for checkbox 2)
echo "<td>...</td>"; //wrong output (ment for checkbox 3)
echo "<td>....</td>";
echo "</tr>;";
}
echo "</table>";
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" id="form1">
<h1>Vul de gegevens in</h1>
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="18"/></td>
</tr>
<tr>
<td>Surface(in m^2):</td>
<td><input type="text" name="surface" size="6"/></td>
</tr>
<tr>
<td>Floor:</td>
<td><input type="checkbox" name="floor" value="floor"/></td>
</tr>
<tr>
<td>Phone:</td>
<td><input type="checkbox" name="phone" value="phone"/></td>
</tr>
<tr>
<td>Network:</td>
<td><input type="checkbox" name="network" value="network"/></td>
</tr>
<tr>
<td><input type="submit" name="verzenden" value="Verzenden"/></td>
</tr>
</table>
There might be a few spelling mistakes since i had to translate it.
Best regards.
Instead of directly assigning your checkbox variables, see if they have been checked or not first.
$verdieping = isset($_POST["floor"]) ? $_POST["floor"] : 0;
$telefoon = isset($_POST["telefoon"]) ? $_POST["telefoon"] : 0;
$netwerk = isset($_POST["netwerk"]) ? $_POST["netwerk"] : 0;
This way, if the user hasn't ticked a checkbox, you have a value of '0' assigned to it instead of an undefined variable.
If you declare a checkbox with:
<input type="checkbox" name="mycheckbox" value="1">
you can check the value after submitting the form by:
if(!empty($_POST["mycheckbox"])) {
// checkbox was checked
}
else {
// checkbox was not checked
}
In this php page you can write like this, it may be solution of your question
if (!empty($_POST))
{
$standnaam = $_POST["name"];
$oppervlakte = $_POST["surface"];
$verdieping = $_POST["floor"];
$telefoon = $_POST["telefoon"];
$netwerk = $_POST["netwerk"];
if (is_numeric($oppervlakte))
{
$_SESSION["name"]=$standnaam;
$_SESSION["surface"]=$oppervlakte;
header("Location:ExpoOverzicht.php");
}
else
{
echo "<h1>Wrong input, Pleasee fill in again</h1>";
}
if(!empty($verdieping) && ($telefoon) && ($netwerk))
{
$_SESSION["floor"]=$verdieping;
$_SESSION["phone"]=$telefoon;
$_SESSION["network"]=$netwerk;
header("Location:ExpoOverzicht.php");
}
}
Related
Ok, so I almost have this script finished, but I'm having an issue where I am getting an Undefined Index error when I try to submit the page on the 'site' index, used in both the $_GET to get it from the URL, and the submit.
<?php
// including the database connection file
include_once("config.php");
$sitenumber = "";
$videolink = "";
$daynight = "";
$maxtents = "";
$maxpersons = "";
$geography = "";
$view = "";
$forestcover = "";
$waterfront = "";
$firepit = "";
$description = "";
$reslink = "";
if(isset($_POST['update']) && isset($_GET['site']))
{
$sitenumber = $_POST['sitenumber'];
$videolink = $_POST['videolink'];
$daynight = $_POST['daynight'];
$maxtents = $_POST['maxtents'];
$maxpersons = $_POST['maxpersons'];
$geography = $_POST['geography'];
$view = $_POST['view'];
$forestcover = $_POST['forestcover'];
$waterfront = $_POST['waterfront'];
$firepit = $_POST['firepit'];
$description = $_POST['description'];
$reslink = $_POST['reslink'];
// checking empty fields
if(empty($sitenumber) || empty($videolink) || empty($daynight) ||
empty($maxtents) || empty($maxpersons) || empty($geography) ||
empty($view) || empty($forestcover) || empty($waterfront) ||
empty($firepit) || empty($description) || empty($reslink)) {
if(empty($sitenumber)) {
echo "<font color='red'>Site Number field is empty.</font><br/>";
}
if(empty($videolink)) {
echo "<font color='red'>YouTube Link field is empty.</font><br/>";
}
if(empty($daynight)) {
echo "<font color='red'>Day or overnight field is empty.</font>
<br/>";
}
if(empty($maxtents)) {
echo "<font color='red'>Maximum Tents field is empty.</font><br/>";
}
if(empty($maxpersons)) {
echo "<font color='red'>Maximum Persons field is empty.</font>
<br/>";
}
if(empty($geography)) {
echo "<font color='red'>Geography field is empty.</font><br/>";
}
if(empty($view)) {
echo "<font color='red'>View field is empty.</font><br/>";
}
if(empty($forestcover)) {
echo "<font color='red'>Forest Cover field is empty.</font><br/>";
}
if(empty($waterfront)) {
echo "<font color='red'>Waterfront Access field is empty.</font>
<br/>";
}
if(empty($firepit)) {
echo "<font color='red'>Firepit field is empty.</font><br/>";
}
if(empty($description)) {
echo "<font color='red'>Description field is empty.</font><br/>";
}
if(empty($reslink)) {
echo "<font color='red'>Reservation Link Access field is empty.
</font><br/>";
}
} else {
//updating the table
$result = mysqli_query($mysqli, "UPDATE sites SET
sitenumber='".$sitenumber."',videolink='".$videolink."'
,daynight='".$daynight."',
maxtents='".$maxtents."'
,maxpersons='".$maxpersons."'
,geography='".$geography."',
view='".$view."',forestcover='".$forestcover."',
waterfront='".$waterfront."',
firepit='".$firepit."',description='".$description."',reslink='".$reslink."'
WHERE sitenumber='".$sitenumber."'");
//redirectig to the display page. In our case, it is index.php
header("Location: index.php");
}
}
echo mysqli_error($mysqli);
?>
<?php
//getting id from url
$sitenumber = $_GET['site']; //Undefined index here
//selecting data associated with this particular id
$result = mysqli_query($mysqli, "SELECT * FROM sites WHERE
sitenumber='".$sitenumber."'");
while($res = mysqli_fetch_array($result))
{
$sitenumber = $res['sitenumber'];
$videolink = $res['videolink'];
$daynight = $res['daynight'];
$maxtents = $res['maxtents'];
$maxpersons = $res['maxpersons'];
$geography = $res['geography'];
$view = $res['view'];
$forestcover = $res['forestcover'];
$waterfront = $res['waterfront'];
$firepit = $res['firepit'];
$description = $res['description'];
$reslink = $res['reslink'];
}
echo mysqli_error($mysqli);
?>
<html>
<head>
<title>Edit Data</title>
</head>
<body>
Home
<br/><br/>
<form name="form1" method="post" action="edit.php">
<table border="0">
<tr>
<td>Site Number</td>
<td><input type="number" name="sitenumber" value=<?php echo
$sitenumber;?>></td>
</tr>
<tr>
<td>YouTube Link</td>
<td><input type="url" name="videolink" value="<?php echo
$videolink;?>"></td>
</tr>
<tr>
<td>Day or Overnight</td>
<td><select name="daynight" value="<?php echo $daynight;?>">
<option value="Day">Day</option>
<option value="Overnight">Overnight</option></td>
</tr>
<tr>
<td>Maximum Tents</td>
<td><input type="number" name="maxtents" value="<?php echo
$maxtents;?>"></td>
</tr>
<tr>
<td>Maximum Persons</td>
<td><input type="number" name="maxpersons" value="<?php echo
$maxpersons;?>"></td>
</tr>
<tr>
<td>Geography</td>
<td><input type="text" name="geography" value="<?php echo
$geography;?>"></td>
</tr>
<tr>
<td>View</td>
<td><input type="text" name="view" value="<?php echo $view;?>">
</td>
</tr>
<tr>
<td>Forest Cover</td>
<td><input type="text" name="forestcover" value="<?php echo
$forestcover;?>"></td>
</tr
<tr>
<td>Waterfront Access</td>
<td><select name="waterfront" value="<?php echo $waterfront;?>">
<option value="Yes">Yes</option>
<option value="No">No</option></td>
</tr>
<tr>
<td>Firepit Availability</td>
<td><select name="firepit" value="<?php echo $firepit;?>">
<option value="Yes">Yes</option>
<option value="No">No</option></td>
</tr>
<tr>
<td>Site Description</td>
<td><input type="text" name="description" value="<?php echo
$description;?>"></td>
</tr>
<tr>
<td>Reservation Link</td>
<td><input type="url" name="reslink" value="<?php echo $reslink;?
>"></td>
</tr>
<td><input type="hidden" name="site" value="<?php echo
$_GET['site'];?>"></td> //Undefined here
<td><input type="submit" name="update" value="Update"></td>
</tr>
</table>
</form>
</body>
</html>
So far you guys have been amazing with my other errors, I'm hoping you can point me in the right direction. And I know, I should use prepared statements, but I'm just learning how to do them now that I've been told what they are. I will fix that in due time. Right now, I just need to get this update page working.
Thanks.
What is $sitenumber = $_GET['site']; //Undefined index here supposed to be when it's not setup yet? What si the default value?
$sitenumber = $_GET['site'] ?? 'DEFAULT VALUE';
same as
$sitenumber = isset($_GET['site']) ? $_GET['site'] : 'DEFAULT VALUE';
Looks like you might want:
$sitenumber = $_GET['site'] ?? $sitenumber;
Since you define that var at the top.
The problem here is that GET['site'] can be unset at those positions.
You have a nice check to see if $_GET['site'] is set at the first part of the code, but the part where you are absolutely sure that $_GET['site'] is actually set ends just before the PHP end tag. After that $_GET['site'] is never checked again for if it exists.
There are two ways to solve this particular problem:
Either you set a default value to $sitenumber and use sitenumber at all places in you code where you need the $_GET['site']
Or you also put the second part inside an if statement, which means that you'd need to copy the current HTML and paste it without the $_GET['site'] and everything it depends on
I hope it is clear that you should just set a default value. The easiest way would be to use a null coalesce operator, which will mean that you'd need to make two small changes.
The first and most obvious: change $sitenumber = $_GET['site']; //Undefined index here to $sitenumber = $_GET['site'] ?? "your default value" ;
The second change is <td><input type="hidden" name="site" value="<?php echo
$_GET['site'];?>"></td> to <td><input type="hidden" name="site" value="<?php echo $sitenumber?>"></td>
I need help on how to retain the entered values in the text fields where user entered the value after submit. I'm having difficulty trying to figure out how to retain the values, because when I click on the submit button, the page refreshes and then values gone, and I have to retype them again.
Below is my form:
<?php $count_name = count($x); ?>
<table>
<thead>
<tr>
<th colspan="<?php echo $count_name; ?>"><strong>MR</strong></th>
<th colspan="<?php echo $count_name; ?>"><strong>MS</strong></th>
</tr>
</thead>
<tbody>
<?php $_college = mysql_query("SELECT * FROM college");
if(mysql_num_rows($_college)) {
$i=0;
while($row_college=mysql_fetch_array($_college)) { ?>
<tr>
<?php for($j=0;$j<$count_name;$j++) { ?>
<td>
<input type="text" name="mr<?php echo $j; ?>[]" value=""/>
<td>
<?php } for($k=0;$k<$count_name;$k++) { ?>
<td>
<input type="text" name="ms<?php echo $k; ?>[]" value=""/>
<td>
<?php } ?>
</tr>
<?php } $i++;} ?>
</tbody>
<table>
<input type="hidden" value="<?php echo $count_name; ?>" name="totrows"/>
<input type="submit" value="Submit" name="submit"/>
Here's my code if button submit is click
<?php
if(isse($_POST['submit'])) {
$y = $_POST['totrows'];
$count_totcriteria = $y;
for($ab=0;$ab<$count_totcriteria;$ab++) {
$mr = 'mr_'.$ab;
$ms = 'ms_'.$ab;
$mr_score = $_POST[$mr];
$ms_score = $_POST[$mr];
foreach($mr_score as $key1 => $val1) {
if(is_numeric($val1) && !empty($val1)) {
$mr_val[] = $val1;
} else {
$msg = 'All fields are required and must be a valid score';
}
}
foreach($ms_score as $key2 => $val2) {
if(is_numeric($val2) && !empty($val2)) {
$ms_str[] = $val2;
} else {
$msg = 'All fields are required and must be a valid score';
}
}
}
}
I know I have to put some code in the 'value=""' in order to display back the entered values when form is submitted but I am not sure what code to use. Not sure how to catch each array values.
I think instead of
<input type="text" name="mr<?php echo $j; ?>[]" value=""/>
you are looking for something like this (assuming $i is your new outer loop)
<input type="text" name="mr_<?= $j ?>[<?= $i ?>]" value="<?= #$_POST['mr_'.$j][$i] ?>"/>
and the same change for the ms line.
Does that work?
I have a list of checkboxes created by a while loop but when the form is submitted the checkboxes are cleared. I have tried to use the unique id of the value in the record by putting it in a hidden field and then using it in an if query before marking the checkbox as checked but it does nothing
<?php
while($row = mysqli_fetch_array($result)){
$posted = $row['auditID'];
?>
<tr class="hover">
<td width="180"><? echo $row['auditName']; ?>
<input type="hidden" name="audit_id_confirm" value="<? echo $row['auditID'];?>">
</td>
<td width="33"><input type="checkbox" name="audit_selected[]"
value="<? echo $row['auditID'];?>"
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked"; }?>>
</td>
</tr>
<?php
}
?>
I sorted it by checking id=f the id number was in the arrays that had been posted.
<?php
$selected = $_POST['audit_selected'];
while($row = mysqli_fetch_array($result)){
$audit = $row['auditID'];
if(in_array($audit, $selected)) {
$check="checked='checked'";
}else{
$check = '';
} ?>
<tr class="hover">
<td width="180">
<?php echo $row['auditName'].' '.$num_audited; ?></td><td width="33"> <input type="checkbox" name="audit_selected[]" value="<?php echo $row['auditID'];?>" <?php echo $check; ?> >
</td>
</tr>
<?php }
?>
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked"; }?>>
Replace above line with this:
<?php if($_POST['audit_id_confirm'] == $posted){ echo "checked='checked'"; }?>>
You are not properly providing checked attribute to the Html entity.
One of the information you need to store in the table, is the state of the checkbox.
So, you can create a new column in your table, call it "checkboxstate".
Now inside your code use the following code to insert the checkbox:
<input type="checkbox" name="<?php echo($posted); ?>">
Thus far we have created several checkboxes whom names are the same as $posted, in other words each checkbox will have the name of its row's "$posted" value.
Now the only thing that is left is submitting the form and storing the checkbox info, which will return either TRUE or FALSE, and we will save that in our table, in the "checkboxstate" column.
So now in our table we will have a bunch of rows with several columns, one of the columns will be "checkboxstate" whom value will be either TRUE or FALSE.
At this point we have pretty much solved the problem, all that is left is to insert a simple if to either show a checked checkbox or an unchecked one.
<?php
while($row = mysqli_fetch_array($result)){
$posted = $row['auditID'];
$cbs = $row['checkboxstate'];
?>
<tr class="hover">
<td width="180"><? echo $row['auditName']; ?></td>
<td width="33">
<?php if($cbs == "true") echo "<input type='checkbox' name='$posted' checked='checked'>"; else echo "<input type='checkbox' name='$posted'>";
</td>
</tr>
<?php
}
?>
I am searching some records using two text boxes and then updating the selected records in database. i am able to see the value row id of the selected checkbox but when i want to get the value for updation in database it gives 0, i.e showing no record in array
Here is my code
if($_POST["search"])
{
$nitnumber = $_POST["nitnumber"];
$workno = $_POST["workno"];
$query = "select * from print where nit = $nitnumber and work = $work";
$result = mysql_query($query) or die ("<font color =red>NIT Number and/or Work Number is Missing</font>");
$count = mysql_num_rows($result);
if($count == 0)
echo "<font color=red>Record not found</font>";
else
{
while($record = mysql_fetch_assoc($result))
{
?>
<tr class="odd">
<td><div align="center"><?php echo $record['a']; ?></div></td>
<td><div align="center"><?php echo $record['b']; ?></div></td>
<td><div align="left"><?php echo $record['c']; ?></div></td>
<td> <div align="left">
<?php
enter code hereecho $record["d"];
?>
</td>
<td>
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>">
<?php echo $record["id"];?>
</td>
<?php } } }?>
<tr>
<td colspan="5" align="right"> <input type="submit" value="Save" name="save"> </td>
</tr>
<?php
if ($_POST['save'])
{
$num_chkboxes=count($_POST['checkbox']);
for($i=0; $i<$num_chkboxes; $i++){
$complete = intval($checkbox[$i]);
echo $complete;
var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
//$Result1 = mysql_query($updateSQL, $FamilyOrganizer) or die(mysql_error());
}
}
?>
<?php
if ($_POST['save'])
{
$checkbox1 = $_POST['chk1'];
$selected_checkbox = "";
foreach ($checkbox1 as $checkbox1)
{
$selected_checkbox .= $checkbox1 . ", ";
}
$selected_checkbox = substr($selected_checkbox, 0, -2);
$updateSQL = "" // your update query here
}
?>
<input type="checkbox" name="chk1[]" value=""> // take checkboxes like this
First, the problem started when you fill the values of the checkboxes:
<input name="checkbox[]" id="checkbox[]" type="checkbox" value="<?php echo $record[$id];?>" />
you must gave them the value like
value="<?php echo $record['id'] ?>"
Second, you don't get the values of the checkboxes as you should:
for($i = 0; $i < count($_POST['checkbox']); $i++){
$complete = intval($_POST['checkbox'][$i]);
//echo $complete;
//var_dump($complete);
echo $updateSQL = "UPDATE toDo SET complete=1, WHERE toDoId=$complete";
}
Another thing you should take care about is the assigning of the elements ids:
id="checkbox[]"
you must give unique ids for each element:
id="checkbox-1"
id="checkbox-2"
I've a doubt. I've 3 textboxes and each is having checkboxes next to it. I want to display
the values of only those textboxes whose respective checkboxes are clicked. Following is the attached HTML and PHP codes:
<html>
<head>
</head>
<body>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="a1" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Hostel"></td>
</tr>
<tr>
<td><input type="text" name="b1" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Transport"></td>
</tr>
<tr>
<td><input type="text" name="c1" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]" value="Food"></td>
</tr>
<tr>
<td colspan="3"><input type="submit" value="submit" /></td>
</tr>
</table>
</form>
</body>
</html>
and below is the PHP part.
<?php
$a=$_POST['a1'];
$b=$_POST['b1'];
$c=$_POST['c1'];
$facilityArray = $_POST['facility'];
$facility = "";
if(count($facilityArray) > 0)
{
foreach($facilityArray as $fac)
{
$facility .= " " . $fac;
}
}
echo $facility; echo "<br>";
echo $a; echo "<br>";
echo $b; echo "<br>";
echo $c;
?>
With the help of following codes I am able to display all the values of checked checkboxes. I am also able to display the values of all the textboxes. But I actually want to display the values of only those textboxes whose respective checkboxes are clicked. I know it may be a very basic question but please help me grow in PHP. Thanks in advance... :(
Your textboxes should also be in an array post to achieve this.
To achieve this change the input lines as:
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
From php you'll be getting the posted textboxes in an array as:
$textbox=$_POST['textboxes'];
You should then loop through the checkboxes array and if the corresponding checkbox is "on" (clicked), then display the textboxes value. To do this you would also need a counter to make sure you are on the same array index for both checkboxes and textboxes:
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
I've also added a name to your submit button so you only check the form when it is submitted.
Your page should now look something like this:
<?php
if(isset($_POST['submit']))
{
$textbox=$_POST['textboxes'];
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
?>
<form name="f" method="post" action="4.php">
<table>
<tr>
<th> Facility </th>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="a"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="b"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td><input type="text" name="textboxes[]" value="c"></td><td><input type="checkbox" id="facility[]" name="facility[]"></td>
</tr>
<tr>
<td colspan="3"><input name="submit" type="submit" value="submit" /></td>
</tr>
</table>
</form>
UPDATE:
To make sure that the $_POST variable exists before assigning it to a variable we use the isset(). In your case just update the php segment as:
<?php
if(isset($_POST['submit']))
{
if(isset($_POST['textboxes']))
{
$textbox=$_POST['textboxes'];
if(isset($_POST['facility']))
{
$facilityArray = $_POST['facility'];
if(count($facilityArray) > 0)
{
$i = 0;
foreach($facilityArray as $fac)
{
if($fac == "on")
{
echo $textbox[$i] . "</br>";
}
$i ++;
}
}
}
}
}
?>
Where the only changes are the addition of another two if statements that take a boolean flag from the isset() function according to whether the $_POST variable has been posted successfully
if(isset($_POST['textboxes']))
AND
if(isset($_POST['facility']))