So, I have made a code to edit radio buttons, drop down lists and text boxes.
The Division->(dropdown list and Other: textbox) and External->(textbox) are under the same "client_details".
This is the example of the edit form.
This is the code I made.
<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
?>
<html>
<head>
<title> Edit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
ID: <input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php
$sql = "SELECT * FROM contracts WHERE `id` = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
$client_type = $row['client_type'];
?>
<label for = "client1">
<input type="radio" name="client_type" id = "client1" value="Division" <?php echo ($client_type == 'Division')? "checked" : "" ?> onclick="toggleDivision()"/> Division
</label>
                                     
<label for ="client2">
<input type="radio" name="client_type" id = "client2" value="External" <?php echo ($client_type == 'External')? "checked" : "" ?> onclick="toggleExternal()"/> External
</label>
 
<input type="text" id="extText" name="client_details2" value="<?php echo $row['client_details']; ?>" disabled />
<br><br>
<div id="division">
Division:
<select id="mySelect" name="client_details" onclick="enableTextbox()" disabled>
<option value="Choose" <?php echo $row['client_details'] == 'Choose' ? "selected" : ""; ?> />Choose Division...</option>
<option value="Distribution" <?php echo $row['client_details'] == 'Distribution' ? "selected" : ""; ?> />Distribution</option>
<option value="Transmission" <?php echo $row['client_details'] == 'Transmission' ? "selected" : ""; ?> />Transmission</option>
<option value="Generation" <?php echo $row['client_details'] == 'Generation' ? "selected" : ""; ?> />Generation</option>
<option value="Procument" <?php echo $row['client_details'] == 'Procument' ? "selected" : ""; ?> />Procument</option>
<option value="Other" <?php echo $row['client_details'] == 'Other' ? "selected" : ""; ?> />Others</option>
</select>
<br><br>
Others:<input type="text" id="otherTxt" name="client_details1" value="<?php echo $row['client_details']; ?>" disabled />
<br>
</div>
<br>
<input type="submit" name="submit" value="Submit"/>
</form>
<script type="text/javascript">
function toggleExternal() {
document.getElementById("extText").disabled = false;
var divis_el = document.getElementById("division");
for (var i = 0; i < divis_el.children.length; i++) {
divis_el.children[i].disabled = true;
}
}
function toggleDivision() {
document.getElementById("extText").disabled = true;
var val = document.getElementById("mySelect").selectedIndex;
var divis_el = document.getElementById("division");
for (var i = 0; i < divis_el.children.length; i++) {
divis_el.children[i].disabled = false;
divis_el.children[5].disabled = true;
}
}
function enableTextbox() {
var val = document.getElementById("mySelect").selectedIndex;
if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4) { document.getElementById("otherTxt").disabled = true}
if (val == 5) { document.getElementById("otherTxt").disabled = false; }
}
</script>
</body>
<?php
if(isset($_POST['submit'])) {
$client_type = isset($_POST ['client_type']) ? $_POST['client_type'] :null;
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
if($client_type == 'Division'){
$client_details = isset($_POST ['client_details1']) ? $_POST['client_details1'] :null;
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null; // both variables under if statement are needed to successfully update the data avoiding blank values.
} else {
$client_details = isset($_POST ['client_details2']) ? $_POST['client_details2'] :null;
}
if($client_details == 'Other') {
$client_details = isset($_POST ['client_details1']) ? $_POST['client_details1'] :null;
}
$query = "UPDATE contracts set `client_type`=?, `client_details`=? WHERE `id`= ?";
$stmt = $con->prepare($query);
$stmt->bind_param("ssi", $client_type, $client_details, $id);
$stmt->execute();
if ($stmt->errno){
echo "FAILURE!!! " . $stmt->error;
} else {
echo "<br>Updated";
}
$stmt->close();
$con->close();
}
?>
EDIT: What I meant is this.
As you can see the example picture from the link, the "Generation" values is displayed in all the textboxes under both Division and External after I updated the data because The value was taken from the drop down list.
The "Other" textbox is under division but it will only enabled to be entered after the selecting the value "Other" from the drop down list under Division.
I only wanted the the Division values displayed in the textbox only under Division and not the under External when Division radio button is chosen. And if I choose External and input data in the textbox, I only wanted the value displayed in the textbox under External only after updated.
When I select the values and not "Other" value, the value should not displayed in the text box under Division. When I select "Other" value, then the value should be displayed in 'Other' textbox but I don't want the value to also displayed in the text box under External.
The question I'm trying ask is how to make the textbox values display values separately. I don't want the same values displaying in all the dropdown list and textboxes? How do I do that? Sorry for poor explaination as English is my second language
Well it's no wonder why both are displaying the samething, their values are both just <?php echo $row['client_details']; ?>
You already have a checking variable, just not sure why you didn't use it here.
E.g.
<?php $extText = ($client_type == 'External') ? $row['client_details'] : "" ?>
<?php $divText = ($client_type == 'Division') ? $row['client_details'] : "" ?>
<input type="text" id="extText" name="client_details2" value="<?php echo extText; ?>" disabled />
Others:<input type="text" id="otherTxt" name="client_details1" value="<?php echo $divText; ?>" disabled />
Related
Something very strange is happening here.
This code below (automatic input values after dropdown selection) does not get saved in the database with 1 click on submit but after 2 clicks. Is there a simple fix for this or do I need an different approach here?
<?php $id = (ISSET($_GET['client'])) ? intval($_GET['client']):0; ?>
<?php while ($row = $result->fetch_assoc()) {?>
<form class="edit" action="" method="post">
<?php
$a = $row['costs'];
$b = $row['deposit'];
$c = $a-$b;
$auto6 = $a*1.06;
$auto21 = $a*1.21;
?>
<select id="btw" name="btw" class="input-xlarge">
<option value="Verlegd"<?=$row['btw'] == 'Verlegd' ? ' selected="selected"' : '';?>>Verlegd</option>
<option value="6%"<?=$row['btw'] == '6%' ? ' selected="selected"' : '';?>>6%</option>
<option value="21%"<?=$row['btw'] == '21%' ? ' selected="selected"' : '';?>>21%</option>
</select>
<strong>Total excl BTW:</strong><input id="btw0" class="special <?=$row['btw'] != 'Verlegd' ? 'disabled' : 'enabled';?>" type="text" name="exclbtw" value="<?=$row['btw'] == 'Verlegd' ? $a : '';?>">
<strong>Total incl BTW 21%:</strong><input id="btw21" class="special <?=$row['btw'] != '21%' ? 'disabled' : 'enabled';?>" type="text" name="btw21" value="<?=$row['btw'] == '21%' ? $auto21 : '';?>">
<strong>Total incl BTW 6%:</strong><input id="btw6" class="special <?=$row['btw'] != '6%' ? 'disabled' : 'enabled';?>" type="text" name="btw6" value="<?=$row['btw'] == '6%' ? $auto6 : '';?>">
<INPUT TYPE="Submit" VALUE="Save" NAME="Submit">
</form>
<?php } ?>
<?php
if(isset($_POST['Submit'])){
$btw = $_POST['btw'];
$exclbtw = $_POST['exclbtw'];
$btw21 = $_POST['btw21'];
$btw6 = $_POST['btw6'];
$update = "UPDATE
invoice
SET
btw = '$btw',
exclbtw = '$exclbtw',
btw21 = '$btw21',
btw6 = '$btw6'
WHERE
id = $id";
$conn->query($update) or die("Cannot update");
}
?>
<?php
if($update){//if the update worked
echo("<script>window.location = 'http://example.com/invoice/edit.php?client=$id';</script>");
} else {
"Error";
}
?>
Even though I put the the php code in the the form to retrieve the value from the database, it didn't display the value in the dropdown list and the textbox. The code works fine and can update the value but it didn't display the value after I refreshed the page. It looks fine but can't seem to find the mistake I made.
<?php
require("config.php");
$id = $_GET['id'];
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
while ($row = $result->fetch_assoc())
{
$client_type = $row['client_type'];
?>
<html>
<head>
<title> Submit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
ID: <?php echo $id; ?><br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Division:
<select name="client_details" />
<option value="Choose" <?php $row['client_details'] == 'Choose' ? print "selected" : ""; ?> />Choose Division...</option>
<option value="Distribution" <?php $row['client_details'] == 'Distribution' ? print "selected" : ""; ?> />Distribution</option>
<option value="Transmission" <?php $row['client_details'] == 'Transmission' ? print "selected" : ""; ?> />Transmission</option>
<option value="Generation" <?php $row['client_details'] == 'Generation' ? print "selected" : ""; ?> />Generation</option>
<option value="Procument" <?php $row['client_details'] == 'Procument' ? print "selected" : ""; ?> />Procument</option>
<option value="Other" <?php $row['client_details'] == 'Other' ? print "selected" : ""; ?> />Others</option>
</select>
<br><br>
Others:
<input type="text" name="client_details" value="<?php $row['client_details']; ?>">
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php
}
if(isset($_POST['submit']))
{
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
if($client_details == 'Other'){
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
}
$query = "UPDATE contracts set `client_details` = '$client_details' WHERE `id` = '$id'";
if ($con->query($query) === TRUE)
{
echo "<br><br> Updated successfully <br>";
echo $query;
}
else {
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
}
?>
<option value="Choose" <?php echo $row['client_details'] == 'Choose' ? print "selected" : ""; ?> />Choose Division...</option>
your code is fine just add echo before will work
You directly close your select element;
<select ... />
Note the /. As in html specs it will consider the <option> tags as not being part of the <select>.
And as #ParthGoswami said; don't forget to echo the values
Here is fixed, clean code.
Few suggestions for future: Always try to follow best practices like don't use while/loop if you are getting only one record from database. Don't use inside while loop, always keep SQL injection in mind when interacting with DB and how to tackle it, follow Code Formatting Pattern etc etc.
<?php
require("config.php");
$id = filter_input(INPUT_GET, 'id');
?>
<html>
<head>
<title> Submit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
ID: <?php echo $id; ?><br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<?php
$sql = "SELECT * FROM contracts WHERE id = $id";
$result = $con->query($sql);
$row = $result->fetch_assoc();
$client_type = $row['client_type'];
?>
Division:
<select name="client_details">
<option value="Choose" <?php $row['client_details'] == 'Choose' ? echo "selected" : ""; ?> />Choose Division...</option>
<option value="Distribution" <?php $row['client_details'] == 'Distribution' ? echo "selected" : ""; ?> />Distribution</option>
<option value="Transmission" <?php $row['client_details'] == 'Transmission' ? echo "selected" : ""; ?> />Transmission</option>
<option value="Generation" <?php $row['client_details'] == 'Generation' ? echo "selected" : ""; ?> />Generation</option>
<option value="Procument" <?php $row['client_details'] == 'Procument' ? echo "selected" : ""; ?> />Procument</option>
<option value="Other" <?php $row['client_details'] == 'Other' ? echo "selected" : ""; ?> />Others</option>
</select>
<br><br>
Others:<input type="text" name="client_details" value="<?php $row['client_details']; ?>">
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php
if(isset($_POST['submit'])) {
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
if($client_details == 'Other') {
$client_details = isset($_POST ['client_details']) ? $_POST['client_details'] :null;
}
$query = "UPDATE contracts set `client_details` = '$client_details' WHERE `id` = '$id'";
if ($con->query($query) === TRUE) {
echo "<br><br> Updated successfully <br>";
echo $query;
} else {
echo "Error: " . $query . "<br>" . $con->error;
}
$con->close();
}
?>
And use echo because according to w3schools, echo is marginally faster than print.
I have made an edit form on a bunch of checkboxes and a textbox.
Here is my code
<?php
require("config.php");
$id = $_GET['id'];
$sql = "SELECT * FROM contracts WHERE id= '$id'";
$result = $con->query($sql);
while ($row = $result->fetch_assoc())
{
$subunit = explode(',',$row['subunit']);
?>
<form action="" method="GET">
ID: <?php echo $id; ?><br>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
Sub-Category Unit:
<input type="checkbox" name="subunit[]" value="OFL" <?php in_array('OFL', $subunit) ? print "checked" : ""; ?>>OFL
<input type="checkbox" name="subunit[]" value="HVTL" <?php in_array('HVTL', $subunit) ? print "checked" : ""; ?>>HVTL
<input type="checkbox" name="subunit[]" value="PINS" <?php in_array('PINS', $subunit) ? print "checked" : ""; ?>>PINS
<input type="checkbox" name="subunit[]" value="FEG" <?php in_array('FEG', $subunit) ? print "checked" : ""; ?>>FEG
<input type="checkbox" name="subunit[]" value="PC" <?php in_array('PC', $subunit) ? print "checked" : ""; ?>>PC
<input type="checkbox" name="subunit[]" value="PI" <?php in_array('PI', $subunit) ? print "checked" : ""; ?>>PI<br>                              
Others:
<input type="text" name="subunit[]" value="<?php echo $row['subunit']; ?>"/>
<br><br>
<input type="submit" name="edit" value="Update" />
</form>
<?php
}
if(isset($_GET['edit']) ){
$subunit = isset($_GET ['subunit']) ? implode(",", $_GET['subunit']) :null;
$upd= "UPDATE `contracts` SET `subunit` = '$subunit' WHERE `id` = '$id'";
if($do_upd = $con->query($upd)) {
echo "Update Success<br>";
echo "$upd <br>";
echo "$do_upd <br>";
echo "<a href='viewcheck.php?id=$id'>View</a>";
} else {
echo "Update Fail<br>";
echo "$upd <br>";
echo "$do_upd <br>";
echo "<a href='viewcheck.php?id=$id'>View</a>";
}
}
?>
The code is working fine. When I went back to the edit form again after I have updated the data, it looks something like this.
I don't want all the checkbox values to be displayed in the textbox. Yet I want to use the same attribute for both checkbox and textbox. How do I do this?
Inside the while loop below the line $subunit = explode(',',$row['subunit']); need to place this code:
$array_all=array('OFL','HVTL','PINS','FEG','PC','PI');
$a=array_values(array_diff($subunit,$array_all));
$a will give you the values for Others text field. You need to replace the value of Others as:
<input type="text" name="subunit[]" value="<?php echo $a[0]; ?>"/>
I'm writing a php form and I can't get the drop down boxes and check boxes to stick as in when I fill in my details but don't click something it will keep everything else filled but that one part that wasn't filled out. I can do it for the input text and radio buttons but I can't get it done for drop downs and checkboxes
<!DOCTYPE html>
<style>
#import url(stickyTextInput.css);
</style>
<?php
if(isset($_REQUEST["left"]))
{
process_form();
}
else
{
display_form_page('');
}
?>
<?php
function display_form_page($error)
{
$self =$_SERVER['PHP_SELF'];
$first_name = isset($_REQUEST['name']) ? $_REQUEST['name']:'';
$last_name = isset($_REQUEST['lastname']) ? $_REQUEST['lastname']:'';
$age = isset($_REQUEST['age']) ? $_REQUEST['age']:'';
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
$color = isset($_REQUEST['color']) ? $_REQUEST['color']: '';
$food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
?>
<html>
<head>
<title>
Forms Sticky input
</title>
<style>
#import url(stickyTextInput.css);
</style>
<style type="text/css">
.error
{
color:#ff0000
}
</style>
</head>
<body>
<?php
if($error)
{
echo "<p>$error</p>\n";
}
?>
<form action= "<?php echo $self?>" method = "post">
<h1>Forms-Sticky Input</h1>
<label>First Name:</label>
<input type="text" size="10" maxlength="40" name="name" value = "<?php echo $first_name?>">
<br>
<label>Last Name:</label>
<input type="text" size="10" maxlength="40" name="lastname" value = "<?php echo $last_name?>">
<br>
<label>Age:</label>
<input type="text" name="age" size="10" value="<?php echo $age?>">
<br>
<label>Gender</label>
<input type="radio" name="gender" value="male" <?php check($gender, "male")?>>Male
<input type="radio" name="gender" value="female" <?php check ($gender, "female")?>>Female
<br>
<label>Select favourite Colour</label>
<select name= "color">
<option <?php checkradio($color, "Red")?>>Red
<option <?php checkradio($color, "Blue")?>>Blue
<option <?php checkradio($color, "Green")?>>Green
<option <?php checkradio($color, "Pink")?>>Pink
<option selected="selected" disabled="disabled">
</select>
<br>
<label>Food</label>
<input type="checkbox" name="food[]" value="beans" <?php checkbox ($food, "beans")?>>Beans
<input type="checkbox" name="food[]" value="crisps" <?php checkbox ($food, "crisps")?>>Crisps
<input type="checkbox" name="food[]" value="lemons" <?php checkbox ($food, "lemons")?>>Lemons
<br>
<div id="buttons">
<input type="submit" name="left" id="left" value="Submit" >
<input type="reset" name="right" id="right" value="Reset" >
</div>
</form>
</body>
</html>
<?php
}
?>
<?php
// If $group has the value $val then select this list item
function check($group, $val)
{
if ($group === $val)
{
echo 'checked = "checked"';
}
}
?>
<?php
function checkradio($group, $val)
{
if ($group === $val)
{
echo 'selected = "selected"';
}
}
?>
<?php
// If $group has the value $val then select this list item
function checkbox($group, $val)
{
if ($group === $val)
{
echo 'checked = "checked"';
}
}
?>
<?php
function process_form()
{
$error = validate_form();
if($error)
{
display_form_page($error);
}
else
{
display_output_page();
}
}
?>
<?php
function validate_form()
{
$first_name = trim($_REQUEST['name']);
$last_name = trim($_REQUEST['lastname']);
$age = trim($_REQUEST['age']);
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
$color = isset($_REQUEST['color']) ? $_REQUEST['color']:'';
$food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
$error = '';
$reg_exp = '/^[a-zA-Z\-\']+$/';
$reg_exp1 = '[0-9]{3}';
if(!preg_match($reg_exp, $first_name))
{
$error .= "<span class=\"error\">First Name is invalid (letters, hyphens, ', only)</span><br>";
}
if (!preg_match($reg_exp, $last_name))
{
$error .= "<span class=\"error\">Last Name is invalid (letters, hyphens, ', only)</span><br>";
}
if (!is_numeric($age))
{
$error .= "<span class=\"error\">Age is invalid (numbers only)</span><br>";
}
if (strlen($gender) == 0)
{
$error .= "<span class=\"error\">Select Male/Female</span><br>";
}
if (strlen($color) == 0)
{
$error .= "<span class=\"error\">Select one color</span><br>";
}
if (! is_array($food))
{
$error .= "<span class=\"error\">You must select one food</span><br>";
}
return $error;
}
?>
<?php
function display_output_page()
{
$first_name = trim($_REQUEST['name']);
$last_name = trim($_REQUEST['lastname']);
$age = trim($_REQUEST['age']);
$gender = isset($_REQUEST['gender']) ? $_REQUEST['gender']:'';
$color = isset($_REQUEST['color']) ? $_REQUEST['color']:'';
$food = isset($_REQUEST['food']) ? $_REQUEST['food']:'';
?>
<html>
<head><title>Form Results</title></head>
<body>
<h1>Form Results</h1>
<?php
echo " First Name: $first_name<br/>\n";
echo " Last Name: $last_name<br/>\n";
echo " Age: $age<br/>\n";
echo " Gender: $gender<br/>\n";
echo " Favourite Color: $color<br/>\n";
echo "<ul>";
if (is_array($food))
{
echo "Favourite Food is:";
foreach($food as $selection)
{
echo "<li>$selection</li>";
}
}
echo "</ul>";
?>
</body>
</html>
<?php
}
?>
Possibly a browser issue, however most browsers only require 'checked' at the end of the html input element for checkboxes. However you appear to be outputting checked = "checked" This is possibly the problem. Have a look at the sample below:
<?php
$name = isset($_GET['name']) ? $_GET['name'] : '';
$agree = isset($_GET['agree']) ? 'checked' : '';
$title = isset($_GET['title']) ? $_GET['title'] : '';
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="">
<input type="text" name="name" id="name" value="<?=$name;?>">
<input type="checkbox" name="agree" id="agree" <?=$agree;?>>
<select name="title" id="title">
<option value="Mr" <?=$title == 'Mr' ? 'selected' : ''?>>Mr</option>
<option value="Mrs" <?=$title == 'Mrs' ? 'selected' : ''?>>Mrs</option>
<option value="Miss" <?=$title == 'Miss' ? 'selected' : ''?>>Miss</option>
</select>
<button type="submit">submit</button>
</form>
</body>
</html>
After processing the values the output html should be as follows:
<form action="">
<input type="text" name="name" id="name" value="test">
<input type="checkbox" name="agree" id="agree" checked>
<select name="title" id="title">
<option value="Mr">Mr</option>
<option value="Mrs">Mrs</option>
<option value="Miss" selected>Miss</option>
</select>
<button type="submit">submit</button>
</form>
I have a form with 'selected' values pulled from the database.
Now I want the user to edit the values.
When the data is send I want to show the new values.
When I submit my form I always get the 'green' value?
What am I doing wrong here?
<?php
// pulled from db
$color = "blue";
// update
if (isset($_POST['Submit'])) {
echo "write to db: " . $_POST['name'] . " + " . $_POST['color'];
}
?>
<html>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="name">Name:</label>
<input type="text" name="name" size="30" value="<?php echo (isset($_POST['name'])) ? $_POST['name'] : ""; ?>">
<br />
<label for="color">Color:</label>
<select name="color">
<option <?php echo (isset($_POST['color']) || $color == "red") ? 'selected="selected"' : ''; ?> value="red">red</option>
<option <?php echo (isset($_POST['color']) || $color == "blue") ? 'selected="selected"' : ''; ?> value="blue">blue</option>
<option <?php echo (isset($_POST['color']) || $color == "green") ? 'selected="selected"' : ''; ?> value="green">green</option>
</select>
<br />
<input type="submit" name="Submit" value="Update">
</form>
</html>
Your conditionals all use ||. They all evaluate to TRUE when the post is set. If you look at the HTML output, every option will say selected='selected'.
Just compare $_POST['color'] to your specified string.
<option <?php echo (isset($_POST['color']) || $color == "red") ? 'selected="selected"' : ''; ?> value="red">red</option>
|| is the "or" operator. If $_POST['color'] is set (i.e., the form was submitted), that will always evaluate to true. You should probably just do
$_POST['color'] == 'red'
Instead. Forget the isset check.