I'm currently creating a custom 'survey'-like form but since I'm new to php, I am not entirely sure how to submit the entries to a database (and make a fieldset required).
I currently have 3 fieldsets, lets call them who, when and what. Who is a simple select field so no problem. When however is a group of radios but two of those have text fields associated that need to be filled if the radio was selected. What is just a textarea.
I understand this much:
Form action is a php file (which contains database connection and
submission)
Form method is post
With the database, how do I submit this data? Especially the second fieldset.
Current Form:
<form action="heroes.php" method="post">
<fieldset>
<h2 class="req">Which Hero?</h2>
<span class="help">We need to know what hero says what line.</span>
<label><span class="title">Hero: </span>
<select>
<?php include 'files/hero-select.php'; ?>
</select>
</label>
</fieldset>
<fieldset id="when">
<h2 class="req">When?</h2>
<span class="help">When is it said? Who is it said to?</span>
<label>To Boss: <input name="when" type="radio" value="boss" /> <input type="text" placeholder="Boss Name" id="bname" size="10" /></label>
<label>To Hero: <input name="when" type="radio" value="hero" /> <input type="text" placeholder="Hero Name" id="hname" size="10" /></label>
<label>Low Health: <input name="when" type="radio" value="lowhp" /></label>
<label>Defeat: <input name="when" type="radio" value="defeat" /></label>
<label>Boss Defeat: <input name="when" type="radio" value="bossd" /></label>
<label>Mob Defeat: <input name="when" type="radio" value="mob" /></label>
<label>Emote: <input name="when" type="radio" value="emote" /></label>
<label>Item Pickup: <input name="when" type="radio" value="item" /></label>
</fieldset>
<fieldset>
<h2 class="req">Quote</h2>
<span class="help">What was said.</span>
<textarea id="quote"></textarea>
</fieldset>
<fieldset>
<input type="submit" value="Submit" /><input type="reset" value="Clear" />
</fieldset>
</form>
hero-select.php:
Black Panther
<option value="Black Widow">
Black Widow
</option>
<option value="Cable">
Cable
</option>
<option value="Captain America">
Captain America
</option>
<option value="Colossus">
Colossus
</option>
<option value="Cyclops">
Cyclops
</option>
<option value="Daredevil">
Daredevil
</option>
<option value="Deadpool">
Deadpool
</option>
<option value="Emma Frost">
Emma Frost
</option>
<option value="Gambit">
Gambit
</option>
<option value="Ghost Rider">
Ghost Rider
</option>
<option value="Hawkeye">
Hawkeye
</option>
<option value="Hulk">
Hulk
</option>
<option value="Human Torch">
Human Torch
</option>
<option value="Iron Man">
Iron Man
</option>
<option value="Jean Grey">
Jean Grey
</option>
<option value="Loki">
Loki
</option>
<option value="Luke Cage">
Luke Cage
</option>
<option value="Moon Knight">
Moon Knight
</option>
<option value="Ms Marvel">
Ms Marvel
</option>
<option value="Nightcrawler">
Nightcrawler
</option>
<option value="Punisher">
Punisher
</option>
<option value="Rocket Raccoon">
Rocket Raccoon
</option>
<option value="Scarlet Witch">
Scarlet Witch
</option>
<option value="Spider-Man">
Spider-Man
</option>
<option value="Squirrel Girl">
Squirrel Girl
</option>
<option value="Storm">
Storm
</option>
<option value="Thing">
Thing
</option>
<option value="Thor">
Thor
</option>
<option value="Wolverine">
Wolverine
</option>
I'm gonna do a short example, with different code (but it works the same).
<form method="POST" action="add.php">
Name: <input type="text" name="name">
Phone: <input type="text" name="phone">
</form>
method="POST" means that it Posts all the info to the action, the action is set to add.php so it posts all the info to add.php.
As you can see every input above has a different name="", what the name property does is:
It sends every value together with a name, this means that the phone input gets send as Phone = "123456" and the name value Name = "2324234".
The add.php code
<?php
if ($_SERVER['REQUEST_METHOD']) == 'POST') {
$name = $_POST['name'];
$phone = $_POST['phone'];
$sql = 'INSERT INTO user '.
'(name,phone) '.
'VALUES ( "$name", "$phone"NOW())';
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
}
First it check if the request is a post, if it is it turns the value's it get send into variables and then it inserts the values into the database.
You can use jQuery with the .post method:
https://api.jquery.com/jQuery.post/
$.post("submittodatabase.php", $( "#yourformid" ).serialize() );
Related
I have the following code, I am trying to make it so that the selected field displays on a php page when I return that hit the submit button, I'm doing this below.
<form action="welcome.php" method="post">
<div class="form-group">
<select class="form-control" id="inputState" style="width: 100%">
<option selected>
Choose...
</option>
<option name="account" value="1">
Minecraft Account
</option>
<option name="cape" value="2">
Minecraft Cape
</option>
</select>
</div>
<input type="submit">
</form>
this is my php echo code, but it does not work
<?php echo $_POST["account"]; ?>
<?php echo $_POST["cape"]; ?>
Set name to your select control
<select name="inputState" class="form-control"
Then you can get the "value" of selected item
<?php echo $_POST["inputState"]; ?>
I have got this code from a php lesson it worked on the lesson but not on my side, it basically transfer data from one page to another.
can you spot what issue please with the variable, size?
everything else work
Thanks
<form action= "form_script.php" method="POST">
<p> Name : <input type ="text" name = "name" /></p>
<p> Email : <input type ="text" name = "email" /></p
<p>size:
<select name="size">
<option value "small">small</option>
<option value "medium">medium</option>
<option value "large">large</option>
</select>
</p>
<p>Gender :
<input type = "radio" name ="gender" value ="Male"/> Male
<input type = "radio" name ="gender" value ="Female"/> Female
</p>
<input type ="submit" name = "submit" value="submit Button"/>
</form>
on form_script.php
<?php
$name =$_POST['name'];
$email =$_POST['email'];
$size =$_POST['size'];
$gender =$_POST['gender'];
print "<p>Name: $name </br> Email: $email </br>
Size: $size </br>Gender: $gender </p>";
?>
With every HTML-attribute (e.g. value) you have to use = sign to specify the value:
<p>size:
<select name="size">
<option value="small">small</option>
<option value="medium">medium</option>
<option value="large">large</option>
</select>
</p>
Change the option tags to this :
<option value="small"> small </option>
You forgot the = sign
try to format HTML correctly:
<form action="form_script.php" method="POST">
<p> Name : <input type="text" name="name"/></p>
<p> Email : <input type="text" name="email"/></p
<p>size:
<select name="size">
<option value="small">small</option>
<option value="medium">medium</option>
<option value="large">large</option>
</select>
</p>
<p>Gender :
<input type="radio" name="gender" value="Male"/> Male
<input type="radio" name="gender" value="Female"/> Female
</p>
<input type="submit" name="submit" value="submit Button"/>
</form>
I am trying to create a simple form that will send data to a PHP script upon submitting the form. I am able to access all of the textbox values, and have successfully implemented variables for each of them. However, when I try to access the value of a combo box on my form, I get this error in the PHP:
Notice: Undefined index: cmbProvince in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 15
HTML
<form id="orderForm" action="scripts/index.php" method="post" onsubmit="return validateForm()">
<fieldset id="customerInfo">
<legend>Customer Information</legend>
<label for="txtFirstName">Name:</label><br>
<input type="text" id="txtFirstName" name="txtFirstName"
placeholder="First Name" autofocus="true"
oninput="formatFirstName()">
<input type="text" id="txtLastName" name="txtLastName"
placeholder="Last Name" oninput="formatLastName()"><br><br>
<label for="txtAddress">Address:</label><br>
<input type="text" id="txtAddress" name="txtAddress" placeholder="Address"
oninput="formatAddress()"><br>
<input type="text" id="txtCity" name="txtCity" placeholder="City"
oninput="formatCity()">
<select id="cmbProvince" name="cmbProvince">
<option value="on">ON</option>
<option value="qc">QC</option>
<option value="mb">MB</option>
<option value="sk">SK</option>
<option value="ab">AB</option>
<option value="bc">BC</option>
<option value="nb">NB</option>
<option value="nl">NL</option>
<option value="ns">NS</option>
<option value="nu">NU</option>
<option value="pe">PE</option>
<option value="nt">NT</option>
<option value="yt">YT</option>
</select><br>
<input type="text" id="txtPostalCode" name="txtPostalCode"
placeholder="Postal Code" oninput="displayPostalCode()">
<br><br>
<!-- Button that will display the order form -->
<input type="button" id="btnOrder" value="Order"
onclick="showOrderInformation()">
</fieldset><br>
<fieldset id="orderInfo">
<legend>Order Information</legend>
<label for="cmbProduct">Product:</label><br>
<select id="cmbProduct">
<option> -- Select -- </option>
<option>Blackberries</option>
</select>
#
<input type="number" id="txtQuantity" name="txtQuantity" value="1"
step="1" min="1" max="50" oninput="displayTotalTax()"><br><br>
</fieldset>
<br><br>
<!-- Button to submit the form to PHP after validation -->
<input type="submit" id="btnSubmit" value="Submit">
</form>
PHP (lines 10 - 19)
/* Capture the user input from the HTML form */
$strFirstName = $_POST['txtFirstName'];
$strLastName = $_POST['txtLastName'];
$strAddress = $_POST['txtAddress'];
$strCity = $_POST['txtCity'];
$strProvince = $_POST['cmbProvince'];
$strPostalCode = $_POST['txtPostalCode'];
Am I typing something wrong? Or missing something? Everything except the combo box is working.
Let me know if I need to include anything else. Thanks!
EDIT
Here is what happens when I try var_dump($_POST);:
Notice: Undefined index: cmbProvince in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 15
Notice: Undefined index: cmbProduct in C:\inetpub\wwwroot\krAssignment4\scripts\index.php on line 18
array(6) { ["txtFirstName"]=> string(7) "Kendall"
["txtLastName"]=> string(4) "Roth"
["txtAddress"]=> string(19) "22 Resolution Drive"
["txtCity"]=> string(8) "Millbank"
["txtPostalCode"]=> string(7) "K2K 2K2"
["txtQuantity"]=> string(1) "2" }
Your code works for me with a little bit changes, try this:
<form id="orderForm" action="scripts/index.php" method="post" onsubmit="return validateForm()">
<fieldset id="customerInfo">
<legend>Customer Information</legend>
<label for="txtFirstName">Name:</label><br>
<input type="text" id="txtFirstName" name="txtFirstName"
placeholder="First Name" autofocus="true"
oninput="formatFirstName()">
<input type="text" id="txtLastName" name="txtLastName"
placeholder="Last Name" oninput="formatLastName()"><br><br>
<label for="txtAddress">Address:</label><br>
<input type="text" id="txtAddress" name="txtAddress" placeholder="Address"
oninput="formatAddress()"><br>
<input type="text" id="txtCity" name="txtCity" placeholder="City"
oninput="formatCity()">
<select id="cmbProvince" name="cmbProvince">
<option value="on">ON</option>
<option value="qc">QC</option>
<option value="mb">MB</option>
<option value="sk">SK</option>
<option value="ab">AB</option>
<option value="bc">BC</option>
<option value="nb">NB</option>
<option value="nl">NL</option>
<option value="ns">NS</option>
<option value="nu">NU</option>
<option value="pe">PE</option>
<option value="nt">NT</option>
<option value="yt">YT</option>
</select><br>
<input type="text" id="txtPostalCode" name="txtPostalCode"
placeholder="Postal Code" oninput="displayPostalCode()">
<br><br>
<!-- Button that will display the order form -->
<input type="button" id="btnOrder" value="Order"
onclick="showOrderInformation()">
</fieldset><br>
<fieldset id="orderInfo">
<legend>Order Information</legend>
<label for="cmbProduct">Product:</label><br>
<select id="cmbProduct" name="cmbProduct"> //changes is in this code
<option> -- Select -- </option>
<option value="Blackberries">Blackberries</option>
</select>
#
<input type="number" id="txtQuantity" name="txtQuantity" value="1"
step="1" min="1" max="50" oninput="displayTotalTax()"><br><br>
</fieldset>
<br><br>
<!-- Button to submit the form to PHP after validation -->
<input type="submit" id="btnSubmit" value="Submit">
</form>
In your index.php write:
<?php
if(isset($_POST) && !empty($_POST)){
echo "<pre/>";print_r($_POST);die;
}
?>
Output:-
Array
(
[txtFirstName] => a
[txtLastName] => b
[txtAddress] => c
[txtCity] => d
[cmbProvince] => qc
[txtPostalCode] => e
[cmbProduct] => Blackberries
[txtQuantity] => 1
)
Kendall, you were:
missing name for your <select id="cmbProduct">, and
missing value for your cmbProduct options as well
Putting in 'disabled selected' is a good practice as well as people won't be able to select the 'select' option :)
Your code
<select id="cmbProduct">
<option> -- Select -- </option>
<option>Blackberries</option>
</select>
Correct code
<select id="cmbProduct" name="cmbProduct>
<option value="select" disabled selected> -- Select -- </option>
<option value="blackberries">Blackberries</option>
</select>
I'm a beginner in PHP, I am working on a carpooling website and I want to display a form in the user account only if he didn't enter his car yet here is my code and unfortunately it didn't work and I haven't got any error on my screen. Please help me if you can !!
<div id="edit-settings" class="tab-pane">
<?php $user_id=$_SESSION['user']['id'];
$res = $db->query('SELECT COUNT(*) FROM car where $user_id');
$get_total = $res->fetchColumn();
if($get_total==0){
echo 'please enter a car by filling the form below'; ?>
<form action="car.php" method="post">
<br />
<label class="control-label" for="inputEmail">
Brand *
</label>
<input class="span4" type="text" placeholder="Brand *" name="brand" maxlength="64" required="">
<br />
<br />
<label class="control-label" for="inputEmail">
Model *
</label>
<input class="span4" type="text" placeholder="Model *" name="model" maxlength="64" required="">
<br /><br />
<label class="control-label" for="inputEmail">
Confort *
</label>
<select id="lst_confort" class="span4" name="confort">
<option value="Basique">Basic</option>
<option value="Normal">Normal</option>
<option value="Confortable">Confortable</option>
<option value="Luxe">Luxary</option>
</select>
<br /><br />
<label class="control-label" for="inputEmail">
Color *
</label>
<select id="lst_confort" class="span4" name="couleur">
<option value="0" selected> choose a color</option>
<option value="0"> white</option>
<option value="1">black</option>
<option value="2">blue</option>
<option value="3"> Red</option>
<option value="4"> Orange </option>
<option value="5"> Green</option>
<option value="6"> Yellow </option>
</select>
<br /><br />
<label class="control-label" for="inputEmail">
Nb places *
</label>
<select name="places" id="lst_NbPlaces" class="span2" required="">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<br />
<br />
<button type="submit" class="btn btn-success">
Submit</button>
<br />
<br />
<br />
<label class="text-info">
* required field
</label>
</form>
<?php } else {
echo "You have already entered car !";
}
?>
Your query is wrong:
SELECT COUNT(*) FROM car where $user_id
This is in single quotes, so it is interpreted as exactly what you see above; $user_id is seen as literal text, not a variable. That is not a valid query. I suspect you mean something like
$res = $db->query('SELECT COUNT(*) FROM car where user_id = ' . $user_id);
(Change user_id to match your column name as needed.)
Note that this is extremely vulnerable to SQL injection and is therefore entirely unsafe for real-world use.
Also, in the future, you can try checking the database errors and logs to determine what went wrong.
Your query has a syntax error:
$res = $db->query('SELECT COUNT(*) FROM car where $user_id');
^---
You have no comparison, so in effect you're doing
SELECT COUNT(*) ... WHERE true
and are counting ALL records in the table. As soon as you had one user enter a car, ALL users were treated as having cars. Perhaps you want something more like
$res = $db->query('SELECT COUNT(*) FROM car where user = $user_id');
^^^^^^^
My html code is like below
<select multiple="multiple" size="2" name="exServer[]">
<option value="1"> host.newcybage.alabanza.com </option>
<option value="2"> host2.newcybage.alabanza.com </option>
<option value="3"> host3.newcybage.alabanza.com </option>
<option value="5"> host3.devel.php5.qa.alabanza.com </option>
<option value="7"> centos5host2.alabanza.com </option>
<option value="8"> centos5host.template.alabanza.com </option>
</select>
<input type="submit" value="Apply" name="exSubmit" class="button">
And my php code is like below
$arr=$_POST['exServer'];
print_r($_POST['exServer']);
print_r($arr);
Both print statements are giving me no results.
I dont know where i am wrong in getting selected multiple dropdown values.
I verified using HttpFox that data is getting posted properly on server side.
When i am printing value of exSubmit, it is giving me proper values.
print "Exsubmit:".$_POST['exSubmit']; //Result Exsubmit:Apply
Please help.
you code is working. I tried this and got all the selected values in PHP
<?php
$arr=$_POST['exServer'];
print_r($_POST['exServer']);
print_r($arr);
?>
<form method="post">
<select multiple="multiple" size="2" name="exServer[]">
<option value="1"> host.newcybage.alabanza.com </option>
<option value="2"> host2.newcybage.alabanza.com </option>
<option value="3"> host3.newcybage.alabanza.com </option>
<option value="5"> host3.devel.php5.qa.alabanza.com </option>
<option value="7"> centos5host2.alabanza.com </option>
<option value="8"> centos5host.template.alabanza.com </option>
</select>
<input type="submit" value="Apply" name="exSubmit" class="button">
</form>
This should work
<select multiple="multiple" size="2" name="exServer[]">
<option value="1"> host.newcybage.alabanza.com </option>
<option value="2"> host2.newcybage.alabanza.com </option>
<option value="3"> host3.newcybage.alabanza.com </option>
<option value="5"> host3.devel.php5.qa.alabanza.com </option>
<option value="7"> centos5host2.alabanza.com </option>
<option value="8"> centos5host.template.alabanza.com </option>
</select>
My suggestion is to print the array $_POST and see the values passed in the backend.
<?php
print_r($_POST);
?>