I am working on a form to create an athlete forename and surname. This works as it should, populating the appropriate parts of the database.
I have now come to add a drop down box in which they will select the athlete's country. Unfortunately, I cannot get this to show up in the athletecountry field of the database. This is in the same table as forename and surname.
I would hugely appreciate any help.
<?php echo ($error != "") ? $error : ""; ?>
<form action="createathlete.php" method="post">
<br>
<br>
Athlete Forename: <input type="text" value="<?php echo $athleteforename; ?>" name="athleteforename" /><br/>
Athlete Surname: <input type="text" value="<?php echo $athletesurname; ?>" name="athletesurname" /><br/>
Representing: Country: <select name=$athletecountry tabindex="1">
<optgroup label="Continent">
<option value="Country 1">Country 1</option>
<option value="Country 2">Country 2</option>
<option value="Country 3">Country 3</option>
</optgroup>
</select>
<input type="submit" value="Register" name="submit-form" />
</form>
Earlier in the page I also have this code which I cobbled together from a couple of other tutorials.
//initialize php variables used in the form
$athleteforename = "";
$athletesurname = "";
$userID = "";
$athletecountry = "";
//check to see that the form has been submitted
if(isset($_POST['submit-form'])) {
//retrieve the $_POST variables
$athleteforename = $_POST['athleteforename'];
$athletesurname = $_POST['athletesurname'];
$athletecountry = $_POST['athletecountry'];
//initialize variables for form validation
$success = true;
$userTools = new UserTools();
//prep the data for saving in a new user object
$data['athleteforename'] = $athleteforename;
$data['athletesurname'] = $athletesurname;
$data['athletecountry'] = $athletecountry;
$data['userID'] = $user->id;
//create the new user object
$newAthlete = new Athlete($data);
//save the new user to the database
$newAthlete->save(true);
It seems you are not giving proper name to select i.e $athletecountry
<select name=$athletecountry tabindex="1">
change to
<select name="athletecountry" tabindex="1">
As per #Chandu and #taxicala answers, once the code is showing as "athletecountry" that should be fine.
Check that the Athlete() class is expecting all the elements in the array, perhaps the country string is being dropped because it's unexpected for some reason?
You have a typo in the name attr of the select:
Change:
<select name=$athletecountry tabindex="1">
To:
<select name="athletecountry" tabindex="1">
Related
I am making a online application used for storing user data and order information. I have successfully managed to create a system to which you first add customers and then add orders to specific customers.
I now want to create a function that would let the administrator of the page edit a certain line in the table.
I managed to figure out how to manually change it through a MySql code but I cannot figure out how to change it through a form? Right now I have this, but I do not think this is the correct way to do it... It works, but is very limited. For example, I am not sure what to do, if admin selects date?
PHP AND HTML
<?php
$id = $_GET['id'];
?>
<form action="update.php" method="POST" >
<input type="text" name="id" style="display:none;" value="<?php echo $id; ?> placeholder="Hidden id">
</select>
<select name="change"style="width:32.9%;" required>
<option selected hidden value="">Select a entry you want to edit</option>
<option value="narocnik">Customer</option>
<option value="naslov">Adress</option>
<option value="kraj">City</option>
<option value="enaslov">E-mail</option>
<option value="telefon">Number</option>
<option value="datumzacetka">Start date</option>
<option value="datumkonca">due date</option>
<option value="material">Material</option>
</select>
<input type="text" name="new" style="width:32.5%;" placeholder="New entry">
<input type="submit" value="PoĊĦlji">
</form>
AND THE MYSQL PART
<?php
include 'baza.php';
$change = $_POST['change'];
$new = $_POST['new'];
$id = $_GET['id'];
$id = $_POST['id'];
$sql = "UPDATE projekti SET $change='$new' where id='$id'";
if ($conn->query($sql) === TRUE) {
header("Location: projekti.php?id=$id");
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
As I said, the function works, but it is far from perfect, so any suggestions would be very welcome :) The most important thing would be the ability to have all the fields displayed but only update the newly inputted ones.
I am trying to update the table below timetable, I want to be able to input the day and subject to change via a selection box form and "add" another id (say 103) to ethics on tuesday at 2pm.
With my current code i am selecting the options in the selection form but it when submitted it returns a value of Ethics Ethics Ethics Ethics Ethics (5 times). Like so- 5x Returned Value
Also when i get into the page i get the following errors...
Notice: Undefined index: day in
/Applications/XAMPP/xamppfiles/htdocs/assignment/stuTimetable.php on
line 304
Notice: Undefined index: subject in
/Applications/XAMPP/xamppfiles/htdocs/assignment/stuTimetable.php on
line 305
Any help is greatly appreciated.....
PHP
$day = $_POST['day'];
$subject = $_POST['subject'];
$sqlevent = "SELECT '$subject' FROM timetable WHERE Day = '$day'";
$resultevent = mysql_query($sqlevent);
$row2 = mysql_fetch_row($resultevent);
$resultevent2 = $row2[0];
echo $resultevent2;
if($resultevent2 = true){
$sqlevent2 = "UPDATE timetable SET 9am = 'Hello' WHERE Day = '$day'";
}
<form action="<?php $_PHP_SELF ?>" id="showapp" method="POST">
<input name="submitapp" type="submit" value="Show Appointments">
</form>
<br>
<form action="<?php $_PHP_SELF ?>" id="timeForm" method="POST">
<h1>Book a slot:</h1>
<br>
Day:
<br>
<select name="day" form="timeForm">
<option value="">--Select--</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
</select>
<br>
Subject:
<br>
<select name="subject" form="timeForm">
<option value="">--Select--</option>
<option value="Meta-Physics">Meta-Physics</option>
<option value="Moral Theory">Moral Theory</option>
<option value="Ethics">Ethics</option>
<option value="Baking">Baking</option>
<option value="Appointment">Appointment</option>
</select>
<br>
Password:
<br>
<input type="password" name="password" placeholder="Type Password to Confirm Booking">
<br>
<input name="submit" type="submit" value="Submit">
</form>
You're getting this error because:
$day = $_POST['day'];
$subject = $_POST['subject'];
are executed once you load the page and at the first time these array elements are not defined.
It's only when you submit the form where $_POST is going hold the right values. So you have to check if you're coming from a form submit
if(isset($_POST['sumbit'])) {
// the update code here
}
Note that I've use submit. You usually have to check any required field because if the field is empty any element of the indexed array would use this field's name as and array key.
Check first if you are handling a post request using something like
if(isset($_POST['day'])) {
// the update code here
}
I have a form that a user can insert his/her level of education either by picking a value from a drop down menu or just typing some text. My code is simple:
<label for="education">Education: <input type="text" name="education"/> </label>
<select name="education">
<option value=""></option>
<option value = "Primary education" name="Primary education">Primary education</option>
<option value = "Secondary education" name="Secondary education">Secondary education</option>
<option value = "Bachelor" name="Bachelor">Bachelor</option>
<option value = "Master" name="Master">Master </option>
<option value = "Doctoral" name="Doctoral">Doctoral </option>
</select>
So i want to insert that value in a column called education in database. Using code above the value is inserted only when someone is picking a value from menu. Input text is not stored in database.
In your textbox part;
Change
<label for="education">Hobby: <input type="text" name="education"/> </label>
to
<label for="education">Hobby: <input type="text" name="education_text"/> </label>
Duplicate name causes your problem(select and textbox name are same). I have given education_text as an example, update it according to your needs. Do not forget to handle it backend with updated name.
On your backend;
$education = "";
if(!empty($_REQUEST["education_text"])) {
$education = $_REQUEST["education_text"];
} else if(!empty($_REQUEST["education"])) {
$education = $_REQUEST["education"];
} else {
die("Invalid education");
}
And use it in your query. You can change $_REQUEST to $_POST or $_GET according to your needs
I am not sure how to go about doing this, but I have an assignment tomorrow and i'm new in php i have to put in the html part a text box and check bob and radio button and drop down list and then take data from user and then extract data in php and display it in table also using php . Any one can helps me please
file.html
<form action="file.php" method="post">
Name:<input type="text" name="name"><br>From<br>
<input type="radio" name="from" value="us">US<br>
<input type="radio" name="from" value="else">Else<br>
<select name="car">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat" selected>Fiat</option>
<option value="audi">Audi</option>
</select>
<input type="submit">
</form>
file.php
<?php
$name = $_POST['name'];
//this takes the data from input
$from = $_POST['from'];
$car = $_POST['car'];
echo "<table><tr><td>Name</td><td>From</td><td>Car</td></tr>
<tr><td>".$name."</td><td>".$from."</td><td>".$car."</td></tr></table>";`
?>
This will return a table with the inserted values.
I don't think there's anything I left out, but the form doesn't seem to be transmitting any data upon hitting submit. My code is as follows, I realize it's kind of long, but I wanted to include the entire form. All of the attached functions just check if the input was valid:
<form name="formname" id="formname" action="database.php" method = post onsubmit="return checker()">
Username: <input type='text' id='un' onchange="checkname(id)" onkeyup="checkempty(id)" ><div id="un1"></div><br>
Reporter Title:<br>
<select id="s2" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="Assistant Director">Assistant Director</option>
<option value="Director">Director</option>
<option value="DB Admin">DB Admin</option>
<option value="Systems Admin">Systems Admin</option>
</select><div id="s21"></div>
<br>
Password: <input type='password' id='pw' onchange="checkpass(id)" onkeyup="checkempty(id)"><div id="pw1"></div><br>
Email: <input type='text' id='eml'onchange="checkemail(id)" onkeyup="checkempty(id)"><div id="eml1"></div><br>
Description:<br> <textarea rows="6" cols="20" id="desc" onchange="checkdesc(id)" onkeyup="checkempty(id)" ></textarea><div id="desc1"></div><br>
Type of Incident:<br>
<select id="s1" onblur="checktype(id)" >
<option value="choose">Choose one</option>
<option value="afs">afs</option>
<option value="password">password</option>
<option value="hardware">hardware</option>
<option value="other">other</option>
</select><div id="s11"></div>
<br>
<?php
include("connect.php");
$ret = mysql_query("SELECT * FROM countries");
echo "Choose your location:<br>";
echo "<select id='countries' onblur='checktype(id)'>";
echo "<option value='choose'>Choose one</option>";
while($array = mysql_fetch_array($ret)){
echo "<option value='{$array['abbrevs']}'>{$array['full']}</option>";
}
echo "</select><br>";
?>
<div id="countries1"></div><br>
<p>
Would you like an email copy?
<select id="s3">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
<input type="submit" id = "sub" value= "Submit">
</p>
</form>
and the php I tried to receive it with
<?php
include("connect.php");
$username = $_GET['un'];
$password = $_GET['s2'];
$reporter = $_GET['pw'];
$email = $_GET['eml'];
$description = $_GET['desc'];
$type = $_GET['s1'];
$country = $_GET['countries'];
$emailopt = $_GET['s3'];
?>
the checker function:
function checker(){
if(isgood && isgood1 && isgood2 && isgood3 && isgood4 && isgood5 && isgood6)
{
return true;
}
else{
document.getElementById('subb').style.visibility="visible";
document.getElementById('subb').innerHTML = "You must fully complete the form.";
return false;
}
where the "isgoods" where just quick flags I made for validations, which was all working properly.
Your form's method is POST so you should use $_POST instead of $_GET in your PHP script.
Also you should fix up your HTML so it's valid, you need to quote the method attribute properly:
<form name="formname" id="formname" action="database.php" method="post" onsubmit="return checker()">
Well, I feel stupid. The problem was that I didn't give my HTML elements names, but rather just IDs. I was so used to using ajax and submitting by just getting the value with javascript via the ID that I forgot about names O.o
You are submitting your form with method="POST", so in your PHP you need to read the POST values:
$username = $_POST['un'];
...
Had you submitted your form with method="GET", your PHP code would work. Read about the difference here.