I am doing an assignment for Uni. It requires me to make an HTML form that needs a user to input what their name is, what table number they're sitting on, if they're a regular customer, and their actual coffee order.
I have done this so far, but now I'm asked in the assignment to create a PHP file to make use of this information. I'm not too savvy with PHP and my lecture notes are doing nothing but confusing the hell out of me.
I have a rough idea about GET methods and POST, but how would I do the button coding? The 'show order' button should return a page of the customers' order. I have tried to read plenty of tutorials but this has sadly become my last resort and really hope someone can help me out with proper explanation.
If I'm not clear with anything here please do let me know I will explain it a little more, and no, I'm not asking for anybody to do the code for me, I just need a head start so i know how to start coding it.
My HTML code:
<!doctype html>
<html>
<head>
<title>Task 1 </title>
<style>
</style>
</head>
<body>
<center> <h1>Legendary Ice Creams </h1> </center>
<h4>Customer Information</h4>
<form action"task1.php" method = GET>
Customer Name: <input type="text" name="Your Name" title="Please enter your name here" placeholder="Your name here"><br>
Table number: <select name="Table number">
<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>
Regular Customer: <input type="checkbox" name="regularcustomer"
value="regularcustomer"><br>
<h4> Orders </h4>
<label for = "mulList[]"> Select one or more items</label>
<select id = "mulList[]" name = "mList[]" size = "4" multiple
= "true">
<option value = "First">First</option>
<option value = "Second">Second</option>
<option value = "Third">Third</option>
<option value = "Fourth">Fourth</option>
</select><br>
Select Coffee: <input type="radio" name="coffee">Latte</input>
<input type="radio"
name="coffee">Cappacino</input>
<input type="radio" name="coffee">Flat Half
De-Caf</input><br>
<button type="button" name="showOrder" action="task1.php">Show Order</input>
<button type="button" name="cancelOrder" action="task1.php">Cancel Order</button>
</form>
First of all, your form tag is wrong, the = is missing and the method should be enclosed in quotes too, like this:
<form action="task1.php" method="GET" enctype="multipart/form-data">
Now your actual problem: To give something to another php file, you need to name the HTML element with the name attribute. Then you can call it in this php file with $_GET or $_POST, depending in what you specified in the form tag. So to access your information, in my case just the name, this would be necessary:
$name = $_GET['Your Name'];
Then you can use it however you want. You can also use an easy loop to get all necessary information, the keys will be preserved and the values filtered with filter_var:
foreach($_GET as $k => $v) {
$getvars[$k] = filter_var($v);
}
I hope this covers your actual question, you seemed pretty lost to me, so i tried to explain it as easy as possible.^^
A little sample of an easy form with html/php:
htmlform.html
<html>
<head><meta charset="utf-8"></head>
<body>
<form action="phpfile.php" enctype="multipart/form-data" method="GET">
<input type="text" name="fname">
<input type="text" name="lname">
<input type="submit">
</form>
</body>
</html>
phpfile.php
<?php
$fname = filter_var($_GET['fname']);
$lname = filter_var($_GET['lname']);
echo "Your name is ".$fname." ".$lname;
?>
First of all do not use space inside the name attribute of your HTML elements. Either replace it an underscore or club the two words together.
Inside your task1.php, you can capture the user input by using the $_POST super variable. So $_POST['Your_Name'] will contain whatever the user typed inside the Customer Name textbox. You can have information about all the keys that you need by using:
echo '<pre>';
print_r($_POST); // replace this with $_GET if using method="GET" in your form tag
echo '</pre>';
in your task1.php page. I suggest you start by doing this and take it from there.
Also, remove the action attribute from your <button> tags.
First of all don't use space between names like Your Name in customer name better use Your_Name or anything you want
second add = between action and "task1.php"
and in
<button type="button" name="showOrder" action="task1.php">Show Order</input>
type will be submit i.e
<button type="submit" name="showOrder" action="task1.php">Show Order</input>
task1.php
if(!empty($_GET)){
echo "Customer Name:".$_GET['name'];//Printing Name
...
}
There is a "=" missing.
<form action"task1.php" method = GET>
should be
<form action="task1.php" method = "GET">
right?
Then you should be able to print the selected content in your
task1.php
<?php
echo "<pre>";
print_r($_GET);
echo "</pre>";
?>
Correct?
Related
I am on page: index.php?page=2 and there is a search form for "crteria" and "term", but every time I submit, it takes me to index.php?criteria=x&term=x instead of index.php?page=2&criteria=x&term=x. So it ignores the page=2. Also I want to submit the criteria and term on the same page I am. Here's the code:
<div id="search">
<form name="search" action="index.php?page=2" method="get">
<p>Search by:</p>
<select name="criteria">
<option>Name</option>
<option>Last name</option>
<option>Project name</option>
</select>
<br><input type="text" name="tern" placeholder="term">
<br><button type="submit">Find`enter code here`</button>
<hr>
</form>
</div>
On the index.php I include the page that the form is on like this:
if(isset($_GET['page']) && $_GET['page'] == 2){
include 'modules/search.php';
}
So when I'm on index.php?page=2 the search.php is providing me with the form that you can see above.
I know, it's a mess, it's my first project. Thank you in advance.
You can simply add an input hidden parameter into your form and remove it from action attribute:
<form name="search" action="index.php" method="get">
<input type="hidden" name="page" value="2">
In case of more than one parameter or if the parameters could vary, you could add this code into the php that print the form:
foreach($_GET as $k => $v){
$k = addslashes($k); //or a DBMS specific escape function
$v = addslashes($v); //or a DBMS specific escape function
echo "<input type='hidden' name='$k' value='$v' />";
}
This is my second code but the problem is I have 3 queries. So it only returns the last product_id when i Click update it always return product_id=3, but i want update the product_id=2
<form action="update_qty.php" method="POST">
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
<?php } ?>
</form>
Your problem is that the PHP is server side and you need something client side to read the value of the text box. You would need a page refresh to pass the text field value to the server so it could write it to the url in the anchor tag. Which is what the form submit would do, but as it would have submitted the value already the anchor tag would be pointless
To do it without a page refresh use Javascript. It would be easy to do with jQuery. You could add an event that writes whatever is entered in the text box the the anchor tags href as it is typed.
I'll do something more like this.
One form per product.In your case when you submit the form the qty value will always be the las found.
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<form action="update_qty.php" method="POST">
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price">
<input type="hidden" value="<?=$getorder['product_id']?>" name="product">
<input type="text" value="<?=$getorder['qty']?>" name="qty" size="1" style="text-align:center">
<input type="submit" value="update" name="update">
</form>
<?php } ?>
You can add more information like this
update
You can not get all values as like that because input name overwrite in every loop iteration.
For multiple values you can try in two ways like:
<?php
while($getorder = mysqli_fetch_array($order)){
$newArr[] = $getorder['price']."~".$getorder['product_id']."~". $ getorder['qty'];
} //while end
?>
<input type="hidden" name="allinputs" value="<?=$newArr?>">
Input field outside the loop.
In php explode array value with ~ and get the all values.
Other solution is that
Your input field name must be change like:
<?php while($getorder = mysqli_fetch_array($order)){ ?>
<input type="hidden" value="<?=$getorder['price']?>" name="actual_price_<?=$getorder['product_id']?>">
<?php } ?>
Change field name in every iteration.
In current scenario either you need three different buttons or the best solution to use AJAX request .
update
On update_qty.php u can use like this
<?php echo $_GET['product_id'];?>
I am working on a project called online Test, where users can take the test. I have created a php code in the following format but its hard coded. Below is the code. If I click on C it will take me to c test and similarly c++
<center><h2><b>Select the test</b></h2>
<div class="test"><b>C</b></div><br>
<div class="test"><b>C++</b></div><br>
</center>
Now the question is I want to make this page has dynamic. To fetch all the course that are there in the database and if user selects a particular test, he should get the test for that particular subject.
<form action="promo.php" method="post">
<h2><center>
<td><select name="links" id="links" value=' ' ">
<?php
while($row=mysql_fetch_array($out))
{
echo "<option value=" .$row['course_id']." > ". $row['c_name']." </option>";
}
?>
</select>
<input type="Submit" value="Start" /></center>
</form>
This is what I have done, it displays all the courses in the drop down fetching from the database. When I click the start button, it does not start any test because promo.php requires another parameter.
I am not able to send the course name along with the promo.php. How to do this
Can any one help me with this code
As some points is not cleared but I assume you can do like this :
Change select box name to name="course"
<select name="course" id="course">
And if you want to send a select box value to a page like this : promo.php?course=c then you should do method = "get" :
<form action="promo.php" method="get">
<h2><center>
<select name="course" id="course">
<?php
while($row=mysql_fetch_array($out))
{
echo '<option value="'.$row['c_name'].'" > '.$row['c_name'].' </option>';
}
?>
</select>
<input type="Submit" value="Start" /></center>
</form>
Perhaps I misunderstood the question, but why not include the other parameter in your form? Just add another <input type="text">, <select> or <input type="hidden">, as appropriate.
I have url variables that was submitted from a form from the previous page. So my url looks something like site.com/submitted.php?first_name=hello&last_name=bye.
Now I am using a link to keep my submitted variables while I go to my second page
<a href="secondPage.php?first_name=hello&last_name=bye>pageLink</a>
On this second page, it's basically a drop-down using the select tag with a submit button that generates a table from mysql server which then links back into the same page. It looks like:
<FORM ACTION="secondPage.php?first_name=hello&last_name=bye" METHOD="GET">
<select name='selectedOption' >
<option value="op1">option1</option>
<option value="op2">option2</option>
<option value="op3">option3</option>
</select>
<INPUT TYPE="SUBMIT" VALUE = "Search">
</FORM>
But lets say I choose option1 and submit, my url does not keep the variables first_name and last_name but it just replaces it :
secondPage.php?selectedOption=op1
instead of:
secondPage.php?first_name=hello&last_name=bye&selectedOption=op1
Any help is appreciated.
I'm not sure how you want to pass data around but here is a suggestion to get the values you want with php.
Change this:
<FORM ACTION="secondPage.php?first_name=hello&last_name=bye" METHOD="GET>
to this:
<form action="secondPage.php" METHOD="post">
Your code for secondPage.php coud look something like this:
// the 'if' statements aren't necessary. just an idea for simple server-side
// validation
if(isset($_REQUEST['selectedOption']) && $_REQUEST['selectedOption'] != ''){
$option = $_REQUEST['selectedOption'];
}
if(isset($_REQUEST['first_name']) && $_REQUEST['first_name'] != ''){
$fname = $_REQUEST['first_name'];
}
if(isset($_REQUEST['last_name']) && $_REQUEST['last_name'] != ''){
$lname = $_REQUEST['last_name'];
}
All the values you need to do whatever with are now in $option, $fname, and $lname
You say your new to php, welcome :), and don't ever trust user input. You should take some time and read about SQL Injection.
Hope some of this helps and good luck.
ON THE SECOND PAGE. In form make two hidden fields <input type="hidden" value="<?php echo $_REQUEST['first_name'] ?>" name="first_name"/> and
<input type="hidden" value=""<?php echo $_REQUEST['last_name'] ?>"" name="last_name"/>
my suggestion use post method
Basically, I've been working myself with something exactly like that, but I've found a great solution and a great approach in my own opinion.. let me give you some bit and piece of code as an example if you don't mind ^_^..
to be organised and clear what you did is : -
##**FIRST PAGE** : -
the url: - secondPage.php?first_name=hello&last_name=bye <--- this was the result..
Then you travelled to the 2nd page: - <-- result was still associated
##**SECOND PAGE** : -
$firstname = $_GET['first_name'];
$lastname= $_GET['last_name'];
<FORM ACTION="secondPage.php?first_name=<?php echo $firstname; ?>&last_name=<?php echo $lastname ?>" METHOD="POST">
<select name='selectedOption' >
<option value="op1">option1</option>
<option value="op2">option2</option>
<option value="op3">option3</option>
</select>
<INPUT TYPE="SUBMIT" name ="submit" VALUE = "Search">
</FORM>
##**THIRD PAGE** The page that the form is going to go to: -
$option = $_GET ['selectedOption'];
// you can also get the other data on this page through same method i've done on page two..
I currently have an HTML file, with a form in it, that when submitted POSTs the form & calls a simple short PHP file that calls a function within another PHP file using the POSTed variables as parameters. The files are both below. What I am wondering is whether I can somehow skip the middleman PHP file, and simply call the function from my HTML file.
Ideally, this would set the call to the function:
insert_PE(new PE($_POST[Date],$_POST[Participant],$_POST[Time],$_POST[Result],$_POST[Notes]));
as the form action. Does anyone know how/if this can be achieved?
HTML:
<FORM ID="FORM1" METHOD="POST" AUTOCOMPLETE="off" ACTION = "writeToDL.php">
<INPUT TYPE="hidden" NAME="Date" STYLE="WIDTH:0px; " MAXLENGTH="8" TITLE="Enter Date" Value="<?php $dt = date('Y-m-d'); echo $dt ?>"/>
<INPUT TYPE="text" NAME="Time" STYLE="WIDTH:70px; " MAXLENGTH="7" ONCHANGE="validateTime();" />
<SELECT NAME = "Result">
<OPTION VALUE = OK></OPTION>
<OPTION VALUE = C>C</OPTION>
</SELECT>
<SELECT NAME = "Participant" STYLE = "WIDTH: 187">
<OPTION SELECTED VALUE = "">Select...</OPTION>
<?PHP
$allParticipants = getall_participants();
foreach($allParticipants as &$value) {
$val = $value->get_id();
echo "<OPTION VALUE='",$val,"'>";
echo $value->get_first_name()," ",$value->get_last_name();
echo "</OPTION>";
}
?>
</SELECT>
<TEXTAREA NAME='Notes' COLS='28' ROWS='5'></TEXTAREA>
<INPUT TYPE="image" SRC = "images/submit.png" VALUE="Submit Participant"/>
</FORM>
PHP File:
<?php
include_once('database/PE.php');
insert_PE(new PE($_POST[Date],$_POST[Participant],$_POST[Time],$_POST[Result],$_POST[Notes]));
?>
You COULD do something like this:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
include_once('database/PE.php');
insert_PE(new PE($_POST['Date'],$_POST['Participant'],$_POST['Time'],$_POST['Result'],$_POST['Notes']));
} ?>
<html>
... rest of your page here ...
</html>
That way the PHP code only fires if an POST was actually performed. Some would suggest checking for the presence of a form field, but that's unreliable - you might change the form down the road and forget to update the if(). Checking the request method is guaranteed to be 100% reliable.
What I am wondering is whether I can somehow skip the middleman PHP file, and simply call the function from my HTML file.
No. The client only knows about URIs.
A URI can map to a PHP program. Multiple URIs can map to the same PHP program. You can use logic to determine what functions to run for a given URI. You can't avoid having that logic in your program.
One option is to put method="_the_main_php_file_containing_function_to_be_called_"
I hope it works fine.
I think you could use a hidden field on the form, and populate it with the name of the function you want to run on "destination.php". Then a switch statement on "destination.php" could pull the name of the function from POST variable.