I'm working on something that is likely simple, but boggling my mind, as I'm relatively new to PHP and am trying to fine tune my skills.
So basically what I'm looking to have done is to have a form where a user inputs information into text boxes, picks something from a drop down list, and also chooses yes or no in a radio button, and submits.
Once the submission happens, where I'm stuck is the following:
A) What do I type in my "create.php" code that puts the information from the text boxes, selection from the drop down list, and choice picked in the radio button into the database? I know how to get the text area characters into it, and my sample code below reflects that, but how do options selected from a drop down list and radio button fit in?
and B) I am hoping that each inputted set of values each have an ability to be deleted by ONLY myself or the user that inputted it. Basically, create a password for each entry.
I'm now pasting my code. First, here is the form that is in the HTML document:
<div id="post form"><form action="create.php" method="post">
Your Information<br />
Name <input type="text" name="name" ><br />
E-mail <input type="text" name="email" ><br />
Phone <input type="text" name="phone" ><br />
<br />Other Information<br />
Location <input type="text" name="location" ><br />
Age <input type="text" name="age" ><br />
Budget <input type="text" name="price" ><br />
<br />Number of Cars
<select name="formCars">
<option value="">Select...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
<br />Number of Bicycles
<select name="formBicycles">
<option value="">Select...</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
<option value="6">Six</option>
<option value="7">Seven</option>
<option value="8">Eight</option>
<option value="9">Nine</option>
<option value="10">Ten</option>
</select>
<br />What You Drive
<select name="formType">
<option value="">Select...</option>
<option value="C">Car</option>
<option value="T">Truck</option>
<option value="M">Motorcycle</option>
<option value="V">Van</option>
</select><br />
<br />Are You Happy With It?
<input type="radio" name="fix" value="Yes" />Yes
<input type="radio" name="fix" value="No" />No
<br />
<br /><input type="submit" name="Submit" value="Post" /><br />
</form>
</div>
Here is the PHP code I've got so far:
<?php
echo "<a href='index.html'><img src='header.png' border=0></a>";
$con = mysql_connect("localhost","123456","sampleTable");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("123456", $con);
$sql = "CREATE TABLE Information
(
name varchar(50),
email varchar(50),
phone varchar(10),
location varchar(50),
age varchar(50),
price varchar(50),
)";
mysql_query($sql,$con);
mysql_select_db("123456", $con);
$sql="INSERT INTO information (name, email, phone, location, age,
price)
VALUES
('$_POST[name]','$_POST[email]','$_POST[phone]','$_POST[location]',
'$_POST[age]','$_POST[price]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "<br />";
echo "Thanks for posting!";
echo "<br />";
echo "<p><a href='index.html'>Back</a>";
mysql_close($con);
?>
Thank you all so much for your help! Again, newbie-ish here, so detailed help would be greatly appreciated!
Here is some information. Do you know how can you check your post data ?
<?php
// After post you can use dump functions
var_dump($_POST);
// or you can use array export for print
print_r($_POST);
?>
You will see input type text, select option value or input radio values same data structure. But if they are not array definition. For example;
<form .... method="POST">
<input type="checkbox" name="cars[]" value="audi">
<input type="checkbox" name="cars[]" value="vw">
</form>
Cars will be array because name has []
Second question a little interesting. Because you don't need a password for every entity normally. When you use user session, you can use only user id as a field in your form table. If users has a session ( logged ) and his user id same with form data record id you can show delete button.
Here is a user management sample here https://daveismyname.blog/login-and-registration-system-with-php
By the way you don't need the create table for every request for your php file.
Another important advice is do not use mysql_* functions. You can use mysqli_* functions or you can use PDO.
Related
I would like the user not to be able to submit a form unless they have selected something from the dropdown datalist which appears as they type. If they just typed something random, I don't want the form to be submitted.
If this isn't possible, would a better option to be to check if the typed text appears in the datalist when the user submits?
<form method="post">
<input type="text" list="browsers">
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']
// function to check if something from the datalist was clicked and NOT just typed
}else{
echo'Select something from the datalist!';
}
While I can set the datalist as required, this can easily be by-passed.
Using required in the input tag that has the same id as the datalist you are targeting will check force the user to input something.
<form method="post">
<input type="text" list="browsers" required>
<datalist id="browsers">
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']
// function to check if something from the datalist was clicked and NOT just typed
}else{
echo'Select something from the datalist!';
}
However it will not block the user from giving an input that is not listed in the dropdown. That needs to be checked via Javascript just before submission.
<form method="post" onsubmit="return myCheckFunction(this)>
<!-- Your form items -->
<!--------------------->
</form>
<script>
myCheckFunction(form) {
// get the values that are currently under the datalist tag in option tags
// get the user input
// compare the options with the user input
// if one is equal with the user input submit the form with the method submit();
// else don't submit the form, the user will have to change his input
}
</script>
If you wish some help with the js i'll be happy to help.
If you want to go with server side validation,
<form method="post">
<input type="text" list="browsers" name="items" required>
<datalist id="browsers" >
<option value="Internet Explorer">
<option value="Firefox">
<option value="Chrome">
<option value="Opera">
<option value="Safari">
</datalist>
<input type="submit" name="submit">
</form>
<?php
if(isset($_POST['submit']
$check = array('Internet Explorer', 'Firefox', 'Chrome', 'Opera', 'Safari');
if(in_array($_POST['items'], $check)){
// Code to be execute.
} else{
echo'Select something from the datalist!';
}
}else{
echo'Select something from the datalist!';
}
I have a following form in my HTML code.
When I forget to fill in the surname,
the form will show me a "bubble message" You have to fill in the surname, then I
will fill in the surname, press the submit button and the values are submitted.
When I forget to change the default option from <select> the form will show
me a "bubble message" You have to choose something, then I will choose another option,
press the submit button, but the values are not submitted. I always have to refresh
the page. Do you know where is the problem and how to achieve it without refreshing?
<form action="add" method="post">
...
<select id="choice" name="choice"
required oninvalid="setCustomValidity('You have to choose something')"
oninput="setCustomValidity('')">
<option value="" selected="selected"></option>
<option value="first">first</option>
<option value="second">second</option>
</select>
...
<input type="text" name="surname" id="surname" value=""
required oninvalid="setCustomValidity('You have to fill in the surname')"
oninput="setCustomValidity('')">
...
</form>
There is no problem with the form data submitting as you will see from this jsfiddle example:
http://jsfiddle.net/vbnh1a4y/
<form action="/echo/html" method="post">
<select id="choice" name="choice">
<option value="" selected="selected"></option>
<option value="first">first</option>
<option value="second">second</option>
</select>
<input type="text" name="surname" id="surname" value="" />
<input type='submit' />
</form>
This is my html form and php file i have to insert my budget dropdown values by select into database
i have to select values from budget and selected value can insert in database
and my data is inserting in my database when i click twice on submit button
<form action="insert.php" method="post" id="register-form" onsubmit=" return add();">
<div class="label"> Name</div><input type="text" id="name" name="name" /><br />
<div class="label">Email</div><input type="text" id="email" name="email" /><br />
<div class="label">Phone Number</div><input type="text" id="phone" name="phone" /><br />
<div class="label">budget</div>
<select id="budget" name="budget">
<option value="">select</option>
<option value="1">0-100</option> <!-- first option contains value="" -->
<option value="2">100-200</option>
<option value="3">200-300</option>
</select>
<br /><br />
<input type="submit" onclick="add()" name="submit" />
<div id="message"></div>
</form>
insert.php
<?php
include("config.php");
if(isset($_POST['submit'])){
$name=$_POST["name"];
$email=$_POST["email"];
$phone=$_POST["phone"];
$budget=$_POST["budget"];
$insert_query="insert into form(name,email,phone,budget) values ('$name','$email','$phone','$budget')";
$con=mysql_query($insert_query);
}
?>
Do the following:
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
Ensure that your field in the database is string/varchar
Re-visit the add() as well. That might be the cause for inserting successfully after the second click.
dropdown pass option value in variable so change the option value which you want to insert in database
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
<select id="budget" name="budget">
<option value="">select</option>
<option value="0-100">0-100</option> <!-- first option contains value="" -->
<option value="100-200">100-200</option>
<option value="200-300">200-300</option>
</select>
if u want to select multiple option u use
<select id="budget" name="budget" multiple="multiple">
The $_POST["budget"]; is taking the value that is in the option, in this case the value for "0-100" is 1. If you want the value to be stored in the database use "0-100" as the value.
As far as the insert problem goes, try using this syntax:
$budget=$_POST['budget'];
$query = "INSERT INTO users ( database_budget )
VALUES ('$budget');";
the top value being the value in the database and the bottom value being the post value.
This is assuming that you're connecting to the database correctly. Also use single quotes for post values not double quotes.
I am creating a search form that uses multiple forms to get matching results from a database that contains meat packaging information. I want eventually for users to be able to use forms to search for the width, length, type, shape and description of a package. With only the matching results to be displayed.
Currently I have 3 dropdown boxes and all work and find matching results. Unfortunately they seem to completely override the other form terms (another problem), but I want to add in a "Any" function so users can search for anything without having to give a specific search for all the dropdown boxes everytime limiting the amount of results that will appear.
HTML code:
<body>
<form action="form3.php" method="post">
<label for ="toolcode">Toolcode: </label>
<input type="text" name="term" /><br />
<label for ="delyncode">Delyn code: </label>
<input type="text" name="term" /><br />
<label for ="description">Description: </label>
<input type="text" name="term" /><br />
<label for ="traysize">Traysize: </label>
<input type="text" name="term" /> <br />
<label for ="trayheight">Trayheight: </label>
<input type="text" name="term" /> <br />
<label for ="traywidth">Traywidth: </label>
<input type="text" name="term" /> <br />
<label for ="traydepth">Traydepth: </label>
<input type="text" name="term" /> <br />
<label for="trayrange">Trayrange: </label>
<select name="trayrange">
<option value="Other">--Any--</option>
<option value="BBQ">BBQ</option>
<option value="Dessert">Dessert</option>
<option value="Display">Display</option>
<option value="Meat">Meat</option>
<option value="Microwave">Microwave</option>
<option value="Party">Party</option>
<option value="Salad/Wet Pasta">Salad/Wet Pasta</option>
<option value="Snacks">Snacks</option>
<option value="Standard">Standard</option>
</select>
<label for ="traytype">Traytype: </label>
<select name="traytype">
<option value="Other">--Any--</option>
<option value="Open">Open</option>
<option value="Cavitised">Cavitised</option>
<option value="Lid">Lid</option>
<option value="Tray">Tray</option>
<option value="Coallition">Coallition</option>
<option value="Bowl">Bowl</option>
<option value="Hinge pack">Open</option>
<option value="Pot">Pot</option>
<option value="Base & Lid">Base and Lid</option>
<option value="Rectangular">Rectangular</option>
<option value="Specalist">Specialist</option>
</select><br />
<label for="trayshape">Trayshape: </label>
<select name="trayshape">
<option value="Other">--Any--</option>
<option value="Rectangular">Rectangular</option>
<option value="Oval">Oval</option>
<option value="Square">Square</option>
<option value="Insert">Insert</option>
<option value="Round">Round</option>
<option value="Open">Open</option>
</select><br />
<input type="submit" value="Submit" />
</form>
</body>
The current "Any" option i have above was just a test. It doesn't work, no results get displayed.
PHP code:
<body>
<?php
$con = mysql_connect ("localhost", "root", "");
mysql_select_db ("delyn_db", $con);
if (!$con)
{
die ("Could not connect: " . mysql_error());
}
$varRange = $_POST['trayrange'];
$varType = $_POST['traytype'];
$varShape = $_POST['trayshape'];
$term = mysql_real_escape_string($_POST['term']);
$sql = "SELECT * FROM delyn WHERE toolcode LIKE '%".$term."%' AND delyncode LIKE
'%".$term."%' AND description LIKE '%".$term."%' AND traysize LIKE
'%".$term."%' AND trayheight LIKE '%".$term."%' AND traywidth LIKE
'%".$term."%' AND traydepth LIKE '%".$term."%' AND trayrange LIKE
'%".$varRange."%' AND traytype LIKE '%".$varType."%' AND trayshape LIKE
'%".$varShape."%' ";
$r_query = mysql_query($sql);
while ($row = mysql_fetch_array($r_query))
{
echo '<br /> Delyn code: ' .$row['delyncode'];
echo '<br /> Tool Code: '.$row['toolcode'];
echo '<br /> Description: '.$row['description'];
echo '<br /> Tray range '.$row['trayrange'];
echo '<br /> Tray type: '.$row['traytype'];
echo '<br /> Tray size: '.$row['traysize'];
echo '<br /> Tray height: '.$row['trayheight'];
echo '<br /> Tray width: '.$row['traywidth'];
echo '<br /> Tray depth: '.$row['traydepth'];
echo '<br /> Tray shape: '.$row['trayshape'] . '<br />' . '<br />'; ;
}
?>
</body>
What would I need to add to allow the users to search allowing for any "tray range", "trayshape" and "traytype". This in theory should display results that match the forms that have info entered.
Thanks in advance for anyone who can help.
use different names for different text-fields otherwise they will be overwritten i.e the last text got preferred at server side you will get tray width only because its the last one with name term
I'm trying to pass two variables (a search query (String) and a column name (also a String)) via a HTML form. The first string is entered into a text box and the second is selected from a drop-down menu.
Obviously this method doesn't work:
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You need to give your <input> field a name in order for this to work. Unnamed inputs won't get passed through the form post.
You don't have a 'name' parameter on your sString input boxes. The name parameter is what gets sent back to the server, not the field's ID:
<input type="text" id="sString" name="sString">
and then in PHP
$search = $_GET['sString'];
You need to use attribute name instead of value.
<h3>Search for a Customer</h3>
<form action="search.php" method="get">
<input type="text" id="sString" name="sString"/>
<select name="sField">
<option name="Name">Name</option>
<option name="HostID">Host ID</option>
<option name="OrderNumber">Order Number</option>
<option name="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
Try:
<form action="search.php" method="get">
<input type="text" name="sString" id="sString" value="">
<select name="sField">
<option value="Name">Name</option>
<option value="HostID">Host ID</option>
<option value="OrderNumber">Order Number</option>
<option value="Theme">Theme</option>
</select>
<input type="submit" value="submit"/>
</form>
You should be able to access form entered variables by $_GET array in search.php. To see what is going on try var_dump($_GET);