I have a form in HTML / PHP that posts the information to a MySQL Database. I have ran into a problem now since everything before was just a "input type="text" - and now I want to do a drop down option to select. I am trying but it does not work.
Here is the code on the form: If you need the other PHP file that displays the data and if it will help then please let me know and I will update it.
It is the Select Name = Track that I want to make a drop down. Only thing is the value of the text areas is one set thing in PHP. For the script to work right and post whatever is selected from the drop down, will I have to make each one of those in my Database also ? Please help me out. I cannot figure this one out and make it work. Thank you all in advance!
<form action="" method="post">
<input type="hidden" name="id" value="<?php echo $id; ?>"/>
<div>
<p><strong>ID:</strong> <?php echo $id; ?></p>
<strong>First Name: </strong> <input type="text" name="firstname" value="<?php echo $first; ?>" /><br/>
<strong>Last Name: </strong> <input type="text" name="lastname" value="<?php echo $last; ?>" /><br/>
<strong>Address: </strong> <input type="text" name="address" value="<?php echo $address; ?>" /><br/>
<strong>City: </strong> <input type="text" name="city" value="<?php echo $city; ?>" /><br/>
<strong>State: </strong> <input type="text" name="st" value="<?php echo $st; ?>" /><br/>
<strong>Zip: </strong> <input type="text" name="zip" value="<?php echo $zip; ?>" /><br/>
<strong>Phone Number: </strong> <input type="text" name="phone" value="<?php echo $phone; ?>" /><br/>
<strong>Drivers License: </strong> <input type="text" name="dl" value="<?php echo $dl; ?>" /><br/>
<strong>Email Address: </strong> <input type="text" name="email" value="<?php echo $email; ?>" /><br/>
<strong>Track Name: </strong>
<select name="Track">
<option value="hillclimb">HillClimb</option>
<option value="trioval">Tri-Oval</option>
<option value="pulpfiction">Pulp Fiction</option>
<option value="speeddrome">SpeedDrome</option>
<option value="roadptc">Road PTC</option>
<option value="tubby">Tubby</option>
</select>
<br />
<strong>Lane Color: </strong> <input type="text" name="lane" value="<?php echo $lane; ?>" /><br/>
<strong>Car Number: </strong> <input type="text" name="car" value="<?php echo $car; ?>" /><br/>
<strong>Controller Number: </strong> <input type="text" name="controller" value="<?php echo $controller; ?>" /><br/>
<input type="submit" name="submit" value="Submit">
</div>
</form>
You need to post the PHP code that processes this form result.
My guess is that in PHP, you're using all lowercase (like the rest of your form fields, "firstname", "lastname", "zip", etc..), yet the drop-down for track starts with a capital letter: "Track"
Related
How would I display student.Student instead of student_id
<?php
for ($i = 0; $i < $chkcount; $i++) {
$id = $chk[$i];
$res = $MySQLiconn->query("SELECT Student.ID, student.Student, student.School,student.PR, Jumps.dis_ft_1, Jumps.dis_in_1, Jumps.dis_ft_2, Jumps.dis_in_2, Jumps.dis_ft_3, Jumps.dis_in_3, Jumps.dis_ft_4, Jumps.dis_in_4, Jumps.dis_ft_5, Jumps.dis_in_5, Jumps.dis_ft_6, Jumps.dis_in_6
FROM Student LEFT JOIN Jumps ON Student.ID = Jumps.student_id WHERE ID=" . $id);
while ($row = $res->fetch_array()) {
?>
<tr>
<td>
<input type="hidden" name="id[]" value="<?php echo $row['ID']; ?>"/>
Student ID: <input type="text" name="st[]" value="<?php echo $row['student_id']; ?>"
class="form-control"/>
Attempt 1 <input type="text" name="df1[]" value="<?php echo $row['dis_ft_1']; ?>"
class="form-control"/>
<input type="text" name="di1[]" value="<?php echo $row['dis_in_1']; ?>"
class="form-control"/>
Attempt 2 <input type="text" name="df2[]" value="<?php echo $row['dis_ft_2']; ?>"
class="form-control"/>
<input type="text" name="di2[]" value="<?php echo $row['dis_in_2']; ?>"
class="form-control"/>
Attemp 3 <input type="text" name="df3[]" value="<?php echo $row['dis_ft_3']; ?>"
class="form-control"/>
<input type="text" name="di3[]" value="<?php echo $row['dis_in_3']; ?>"
class="form-control"/>
Attempt 4 <input type="text" name="df4[]" value="<?php echo $row['dis_ft_4']; ?>"
class="form-control"/>
<input type="text" name="di4[]" value="<?php echo $row['dis_in_4']; ?>"
class="form-control"/>
Attempt 5 <input type="text" name="df5[]" value="<?php echo $row['dis_ft_5']; ?>"
class="form-control"/>
<input type="text" name="di5[]" value="<?php echo $row['dis_in_5']; ?>"
class="form-control"/>
Attempt 6 <input type="text" name="df6[]" value="<?php echo $row['dis_ft_6']; ?>"
class="form-control"/>
<input type="text" name="di6[]" value="<?php echo $row['dis_in_6']; ?>"
class="form-control"/>
</td>
</tr>
<?php
}
}
?>
Not sure it can be done. But Student name would be better than student id.
Wouldn't post with more text so just typing so it will post.
I am learning PHP.
If there is only one field called Student i.e Student.Student, then you could just output the name <?php echo $row['Student']; ?>.
However, if there are ever name clashes, you can always give the field a new name for PHP to access.
SELECT Student.ID, Student.Student AS some_new_name FROM ....
<?php echo $row['some_new_name']; ?>
Trying to get an edit page to work and im getting this error Incorrect parameter count in the call to native function 'SHA1'. The error is only on the SHA1. everything else works fine other then this error. Any helped is greatly appreciated. I don't want the password showing or the encryption that is to stay blank as it is an admin editing a staff members details and they dont need to know the password.
<?php
if(empty($_POST['submit'])) {
$query = "SELECT * FROM user WHERE user_id = '$_GET[id]'";
// $supplier_id = $_GET['supplier_id'];
$result = mysqli_query($dbc, $query);
$row = mysqli_fetch_array($result);
}
else{
$update = "UPDATE user SET
title= '$_POST[inputtitle]',
first_name ='$_POST[inputfirst_name]',
middle_name = '$_POST[inputmiddle_name]',
last_name = '$_POST[inputlast_name]',
gender = '$_POST[inputgender]',
email = '$_POST[inputemail]',
pass = SHA1($_POST[inputSHA1pass]);
car_make = '$_POST[inputcar_make]'
car_model = '$_POST[inputcar_model]',
prefer_car = '$_POST[inputprefer_car]',
car_age = '$_POST[inputcar_age]',
st_no = '$_POST[inputst_no]',
st_name = '$_POST[inputst_name]',
suburb = '$_POST[inputsuburb]',
state = '$_POST[inputstate]',
country = '$_POST[inputcountry]',
postcode = '$_POST[inputpostcode]',
WHERE user_id = $_POST[user_id]";
mysqli_query($dbc, $update) or die(mysqli_error($dbc));
echo "User has been modified!";
header("Location: view_user.php");
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'].htmlspecialchars($_GET[‘id’]); ?>" method="POST">
<input type="hidden" value="<?php echo $supplier = $_GET['id']; ?>" name="supplier_id">
<p>Title:
<input type="text" name="inputtitle" size="20" maxlength="30"
value="<?php echo $row["title"]; ?>"/>
</p>
<p>First Name:
<input type="text" name="inputfirst_name" size="20" maxlength="30"
value="<?php echo $row["first_name"]; ?>"/></p>
<p>Middle Name:
<input type="text" name="inputmiddle_name" size="30" maxlength="100"
value="<?php echo $row["middle_name"]; ?>"/></p>
<p>Last Name:
<input type="text" name="inputlast_name" size="20" maxlength="30"
value="<?php echo $row["last_name"]; ?>"/></p>
<p>Gender:
<input type="text" name="inputgender" size="20" maxlength="30"
value="<?php echo $row["gender"]; ?>"/></p>
<p>Email:
<input type="text" name="inputemail" size="10" maxlength="5"
value="<?php echo $row["email"]; ?>"/>
</p><br>
<p>Password:
<input type="text" name="inputSHA1pass" size="30" maxlength="40"
value="<?php if (isset($_POST['pass'])) echo $_POST['pass']; ?>"/></p>
<p>Car Make:
<input type="text" name="inputcar_make" size="20" maxlength="30"
value="<?php echo $row["car_make"]; ?>"/></p>
<p>Car Model:
<input type="text" name="inputcar_model" size="20" maxlength="30"
value="<?php echo $row["car_model"]; ?>"/></p>
<p>Prefered Car:
<input type="text" name="inputprefer_car" size="20" maxlength="30"
value="<?php echo $row["prefer_car"]; ?>"/></p>
<p>Car Age:
<input type="text" name="inputcar_age" size="20" maxlength="30"
value="<?php echo $row["car_age"]; ?>"/></p>
<p>Street Number:
<input type="text" name="inputst_no" size="20" maxlength="30"
value="<?php echo $row["st_no"]; ?>"/></p>
<p>Street Name:
<input type="text" name="inputst_name" size="20" maxlength="30"
value="<?php echo $row["st_name"]; ?>"/></p>
<p>Suburb:
<input type="text" name="inputsuburb" size="20" maxlength="30"
value="<?php echo $row["suburb"]; ?>"/></p>
<p>State:
<input type="text" name="inputstate" size="20" maxlength="30"
value="<?php echo $row["state"]; ?>"/></p>
<p>Country:
<input type="text" name="inputcountry" size="20" maxlength="30"
value="<?php echo $row["country"]; ?>"/></p>
<p>Postcode:
<input type="text" name="inputpostcode" size="20" maxlength="30"
value="<?php echo $row["postcode"]; ?>"/></p>
<p>
<input type="submit" name="submit" value="Submit"/></p>
</form>
<?php
mysqli_close($dbc);
?>
<br>
<br>
</div>
<br>
<?php
include ('../includes/footer.php');
?>
A few things:
Check that $_POST['inputSHA1pass'] is not empty
Enclose $_POST[inputSHA1pass] in quote marks and curly braces: SHA1('{$_POST[inputSHA1pass]}'). SHA1() expects a string and will error without one
Look into prepared statements. As this code stands, it is wide open to SQL injection attacks as none of your passed data is checked for validity
Change the semi-colon to a comma in the SHA1 line
Finally, once the UPDATE query has been built, capture it and try running it directly on the database server. It should give you a more meaningful error message and make debugging easier
I wish every time I reload or refresh the page. The disabled textbox still disabled. Because my purpose is to modify the textbox once time and then it will be disabled forever. Is this possible?
Below is my code
<tr>
<td><?php echo $lang_txt['leader_id'][$lang]; ?></td>
<td>:</td>
<td><?php if($_POST['txtLeaderID'] == ""){?><input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php }
else
{
?>
<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="<?php echo $merchant['LeaderID']; ?>" maxlength="20" /><?php
}
?>
</td>
</tr>
Your variant will be worked with
if(!$_POST['txtLeaderID']){echo('<input type="text" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')} else {echo('<input type="text" disabled="disabled" name="txtLeaderID" id="txtLeaderID" style="width:400px;" value="'.$merchant['LeaderID'].'" maxlength="20" />')}
For example:
<?php
if (!$_POST['username']){
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
But it didn't work, if we open it in a new tab. You must save this propery anywhere (session, cookie, sql)and check the stored values. it will be work in a new tab.
For example with session:
<?php
session_start();
if (!isset($_SESSION['data'])) {
$_SESSION['data'] = true;
echo('
<form action="'.$_SERVER['REQUEST_URI'].'" method="post">
Name: <input type="text" name="username" /><br />
Email : <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Send POST!" />
</form>
');}
else{
echo('
<form>
Name: <input type="text" name="username" disabled="disabled"/><br />
Email : <input type="text" name="email" disabled="disabled"/><br />
</form>
');}
?>
Ok so Im trying to use radio buttons in my PHP code. I wrote a Madlib in PHP and I want to write in a radio button that when a user selects it, it will change the story etc. Happy ending or sad ending here the code I have so far. Any help would be appreciated.
<html>
<head>
<title>James Nygaard's Mablib</title>
</head>
<body bgcolor="<?php echo $bg; ?>" text="<?php echo $fg; ?>">
<h1 style="font-family:cursive;">Create your Madlib below:</h1>
<form action="" method="post">
Enter the name of a boy:
<input name="noun1" type="text" value="<?= $_POST['noun1'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj1" type="text" value="<?= $_POST['adj1'] ?>" /><br />
Enter the name of a man:
<input name="noun2" type="text" value="<?= $_POST['noun2'] ?>" /><br />
Enter an Adjective that describes a person:
<input name="adj2" type="text" value="<?= $_POST['adj2'] ?>" /><br />
Enter the name of a woman:
<input name="noun3" type="text" value="<?= $_POST['noun3'] ?>" /><br />
Enter your favorite animal:
<input name="noun4" type="text" value="<?= $_POST['noun4'] ?>" /><br />
Enter a name:
<input name="noun5" type="text" value="<?= $_POST['noun5'] ?>" /><br />
Enter the name of your favorite city:
<input name="noun6" type="text" value="<?= $_POST['noun6'] ?>" /><br />
Enter a feeling that ends in "ness":
<input name="adj3" type="text" value="<?= $_POST['adj3'] ?>" /><br />
Enter a Verb that ends in "ing":
<input name="verb2" type="text" value="<?= $_POST['verb2'] ?>" /><br />
Enter the name of a boy:
<input name="noun7" type="text" value="<?= $_POST['noun7'] ?>" /><br />
Enter the name of a girl:
<input name="noun8" type="text" value="<?= $_POST['noun8'] ?>" /><br />
<input type="submit" value="Click here or press enter to see your MadLib" /><br />
</form>
<div style="color: #2F4F4F; font-family: cursive;">
<?php
$bg = "8FBC8F";
$fg = "2F4F4F";
if(isset($_POST['noun1'])) {
echo "<h1> The adventures of {$_POST['noun1']}. </h1>";
echo "This is where i will put the story. I already have the story but the code is really long so I left it out of this question.";
echo "<br> The end.";
}
?>
</div>
</body>
</html>
It's not especially clear in this small space, but I tried to follow your code style.
Select an option:
<label>
<input name="some_option" type="radio" value="option_1" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_1') echo 'checked="checked" '; ?>/> Option 1
</label>
<label>
<input name="some_option" type="radio" value="option_2" <?php if(isset($_POST['some_option']) && $_POST['some_option'] == 'option_2') echo 'checked="checked" '; ?>/> Option 2
</label>
If you run that code you should see what's going on.
if(isset($_POST['submit'])){
$domain=$_POST['domain'];
$fname=$_POST['fname'];
$sname=$_POST['sname'];
$tel=$_POST['tel'];
if($domain==""){
$error="<h4>Enter Domain </h4>";
}elseif($fname == ""){
$error="<h4>Enter Firstname </h4>";
}elseif($sname == "")
{
$error="<h4 >Enter Surname</h4>";
}elseif($tel=="")
{
$error="<h4 >Enter telephono no</h4>";
}
else {
$sql11=mysql_query("INSERT INTO domain VALUES('','$domain','$fname','$sname','$tel','$mobile','$email','$company','$address','$city','$country','$pcode','$tele',
'$fax','$qus','$ans')");
echo $sql;
$db->query($sql);
}
}
<div><?php echo $error; ?></div>
<form action="" method="post" name="classic_form" id="classic_form">
<div><h4>Personal details:</h4></div><div style="margin-left: 109px;">
<div>Domain</div>
<input type="text" name="domain" id="domain" value="" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="" />
<div>Mobile:</div>
</form>
In my registration page, i used php validation. After the user submit the form if it shows validation errors it also resets all the fields. How can i resolve this problem? Without reset the fields i have to show the php validation errors. I also used in each input value. But it shows
"Notice: Undefined index: domain in D:\xampp\htdocs\deena\domainreg.php on line 82" . Please help me to resolve this problem
<input type="text" name="domain" id="domain" value="<?php echo isset($domain) ? $domain : ''; ?>" />
You have to pass all your values to php, and send back to html to feed your fields.
Its not 'resetting your fields' .. Your form is being submitted, hence the page is being reset and fields are therefore loading empty. Place the $_POST[] values in the field values upon page load:
<input type="text" name="domain" id="domain" value="<?php echo $domain ?>" />
<div>First name: </div>
<input type="text" name="fname" id="fname" value="<?php echo $fname?>" />
<div>Surname:</div>
<input type="text" name="sname" id="sname" value="<?php echo $sname?>" />
<div>Telephone:</div>
<input type="text" name="tel" id="tel" value="<?php echo $tel?>" />
Simple. Just add the variables to the input values:
<input type="text" name="domain" id="domain" value="<?php echo $domain; ?>" />
You should also protect the outputted value, against cross site scripting:
<input type="text" name="domain" id="domain" value="<?php echo htmlspecialchars($domain); ?>" />
In the value field:
<input type="text" name="domain" id="domain"
value="<?php if(isset($_POST['domain'])){echo $_POST['domain'];} ?>">
Didn't test it. But i think it should work.
In input tag add the php value as like value="" So that it will echo if the variable is posted or it will show the empty one