I am looking for only PHP. NOT javaScript.
I have a drop down which has 1 of the 15 week. If i chooses any value from that drop down it should go to selected page and say..
Welcome you have selected week #
<?php
if (isset($_POST['attendance'])) {
header('Location:attendance_page.php');
} else if (isset($_POST['handout'])) {
header('Location:handout_page.php');
} else if (isset($_POST['assignment'])) {
header('Location:assignment_page.php');
}
?>
<form action="#" method="post" >
<p>Welcome Professor Mr.XYZ !</p><br/><br/>
<select name="sweek">
<?php
for($i=1;$i<=15;$i++)<!--i am populating dropdown with 15 weeks-->
{
echo "<option value='Week$i'> Week $i</option>";
}
?>
</select><br/><br/>
<input type="submit" name="attendance" value="Attendance" />
<input type="submit" name="handout" value="Handout" />
<input type="submit" name="assignment" value="Assignment" />
</form>
Can anyone help please
To post to two differenet pages you need to post to one and then redirect to the other. But you can't redirect with post data in PHP.
You'd need to use get instead of post. To transform post to get you can do:
$get = http_build_query($_POST) . "\n";
header('Location: otherpage?' . $get);
Related
I've been trying to build a program using PHP cookies. The user interface basically looks like this.
My basic program
The logic is this. When the user enters the product name, price and quantity and clicks on the "Add" button, the total value which is price*quantity will fall on the "Total bill value" textbox. The user can go on adding more and more values for "Price" and "Quantity" and each time he clicks on "Add", all the values get added. So this should be done by using cookies. The "Clear" button will reset the cookie, and the "Print" button will also be using the same cookie to print out everything the user has entered upto that point.
Anyways I just started coding, and I'm already stuck. I'm not sure what my mistake is. When I click on the "Add" button, the value doesn't get added every time I enter a price and quantity. Instead the page refreshes to give only the product of the price and quantity entered at one time. This is my code.
`
<form method="POST" >
<ul style="list-style-type:none;">
<li>
Product Name: <input type="text" name="ProductName"> <br>
Price: <input type="number" name="Price" value="price"> <br>
Quantity: <input type="number" name="Quantity" value="qty"><br>
<input type="submit" name="btnClick" value="Add">
<input type="submit" name="btnReset" value="Clear">
<input type="submit" name="btnPrint" value="Print"><br>
Total Bill Value: <input type="text" name="Bill">
</li>
</ul>
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$result=0;
if(isset($_POST["btnReset"]))
{
setCookie("result", $result);
}
elseif(isset($_POST["btnClick"]))
{
if(isset($_COOKIE["result"]))
{
$result=$_COOKIE["result"];
}
$result= $_POST["Price"]*$_POST["Quantity"];
echo $result;
setCookie("result", $result);
}
else
{
echo "Product: ".$_POST["ProductName"];
echo "Price: ".$_POST["Price"];
echo "Quantity: ".$_POST["Quantity"];
}
}
?>
</body>
`
I'm sure that this is a stupid mistake I'm struggling on. However I am an absolute beginner and would so appreciate if somebody could point out my mistake and explain it to me.
So there are two things I want to know mainly:
Why is my cookie not being set when the Add button is clicked?
How to put the value calculated in PHP back to the html form, so that the "Total Bill Value" textbox will have the total value?
Thanks in advance!
Edit: I did figure out my mistake. I should have put $result=$result + $_POST["Price"]*$_POST["Quantity"];
Then the values get added up.
However would like to know how to put the total bill value calculated in PHP back to the HTML form
You should call setcookie() before you output any HTML, so move the PHP code above the rest.
To set the form input:
<?php
if (isset($result)) {
$bill = $result;
} else {
$bill = '';
}
?>
Total Bill Value: <input type="text" name="Bill" value="<?php echo $bill; ?>">
please use setcookie instead of setCookie
This will work
Happy Coding
The problem I'm working on is this: I have a form which ask user to type in a name. Then i search this name in my database, the database will return several addresses. Then i need to use those addresses to generate second radio button form. User will choose one address, and then they get redirected to that address.php
The entire process is like when you shop on amazon, after you log in with your username, it will ask you to select your shipping address and once shipping address is select, i want to redirect user to their address page.
what i current have is following
//error checking----------------------------------
if (empty($_POST['name'])===true) {
echo"filling in your name"
}
else{
//get value from database------------------------------
$query="get address";
$sql_result=mysql_query($query);
if(mysql_num_rows($sql_result)==0){
echo "no result found";
}
else {
//second form--------------------------------------------------------
while ($row = #mysql_fetch_assoc($sql_result)){
echo "<form name=\"select\" method=\"post\">";
echo '<input type="radio" name="name" value="'.$row['name'].'"> '.$row['name'];
echo '<br>';
}
echo "<input type=\"submit\" name=\"choice\" value=\"Submit\" />";
}
}
}
//first form-----------------------------------------------
<form action="" method="post">
<ul>
<li>name: <br> <input type="text" name="name">
</li>
<li><input type="submit" value="Find it">
</li>
</ul>
</form>
I have no idea how to proceed from here. Where shall i put my first form so that only first form is displayed when the page is loaded. Only second form is displayed when first form is submitted. I tried to use $_GET like
if(isset($_GET['success'])&& empty($_GET['success'])){
form2;
redirect(location:address.php);
}
else{
get name and search address;
get address;
redirect(location:samepage?success)
form1;}
But this didn't work since address is generated in else statement, i can not access it from if statement. Thanks for the help.
You have to redirect the first form to a new page then look at the $_POST variable.
You can do it this way :
firstpage
<form action="secondpage.php" method="post">
<ul>
<li>name: <br> <input type="text" name="name">
</li>
<li><input type="submit" value="Find it">
</li>
</ul>
</form>
secondpage.php
if(!isset($_POST['name']))
{
redirect(location:firstpage.php);
}
else
{
// MySql request to check username
$resultArray // you result as an array
echo '<select>';
foreach ($resultArray as $value){
echo '<option value="'+$value+'">'+$value+'</option>
}
echo '</select>';
}
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' />";
}
I am using a get method to select options with years and months. The URL after submitting this selection looks as follows:
www.mywebsite.com/transactions/?yr=2013&mo=6
I can reload this page and the selection is still there. However, when i submit a form to add a record, the page reloads as follows:
www.mywebsite.com/transactions/?#
And the selection is gone. How can I keep the seletion and reload the exact same url after submitting the form?
I am using the following code:
<form action="?" method="post">
<SELECT options with different inputs>
<input type="submit" value="Add">
</form>
In my PHP it looks the following:
header('Location: ?yr='. $GLOBALS['yearselect'] .'&mo=' . $GLOBALS['monthselect']);
It creates the right URL after submit, only the global variables are not updated. I defined those as follows:
$GLOBALS['monthselect'] = date('m');
$GLOBALS['yearselect'] = date('Y');
And they are changed when I select an option.
You could traverse through submitted variables and add them to the HTML code.
<form action="target.php?var1=<?= $_GET["value2"]; ?>&var2=value2" method="get">
Or just use $_SERVER['REQUEST_URI'] inside action="".
Your form element probably has an attribute action="#".
You could replace this with the original request uri (assuming you use the POST method, if you use GET the query parameters in the form's action attribute will be overridden in most browsers.
<form method="POST" action="<?= $_SERVER['REQUEST_URI'] ?>">your form</form>
In your PHP code, after you get the values by GET method, use the header() function as :
header("Location: www.mywebsite.com/transactions/?yr=2013&mo=6");
It will redirect to the page with the form.
EDITED:
This is a sample code for your need:
<?php
$years=array(1999,2000,2001,2002,2003,2004);
if(isset($_POST['year']))
{
/*
Do your coding here
*/
echo <<<EOT
<form action='{$_SERVER['PHP_SELF']}' method='POST'>
<select name='year'>
EOT;
for($i=0;$i<6;$i++)
{
if($years[$i]==$_POST['year'])
echo "<option value='{$years[$i]}' selected='selected' >{$years[$i]}</option>";
else
echo "<option value='{$years[$i]}'>{$years[$i]}</option>";
}
echo <<<EOT
</select>
<input type='submit' value='Add'>
</form>
EOT;
}
else
{
echo <<<EOT
<form action='{$_SERVER['PHP_SELF']}' method='POST'>
<select name='year'>
EOT;
for($i=0;$i<6;$i++)
{
echo "<option value='{$years[$i]}'>{$years[$i]}</option>";
}
echo <<<EOT
</select>
<input type='submit' value='Add'>
</form>
EOT;
}
?>
It will print the first value of the drop-down list on the first time, and after that it'll save the values.
Inside form tag action attribute should b set to the page which handles your request,i think u have set action='#' .
Please specify more detail.so that i can help u
<select name="gamelist" id="gamelist">
<option value="1">Backgammon</option>
<option value="2">Chess</option>
</select>
<input type="submit" name="submit" id="submit" value="Submit" />
i want to grab the selected value and place in in a var
any idea?
thanks
Depends on your form tag.
<form method="post">
Will pass the value to $_POST['gamelist']
While
<form method="get">
Will pass the value to $_GET['gamelist']
Ofcourse, only after hitting the submit button. As morgar stated, this is pretty basic form procession. I doubt you've used google or followed a tutorial, this is almost one of the first things one learn when working with forms and PHP. We arent here to give you full solutions, take this as an example and create the full page yourself:
if($_SERVER['REQUEST_METHOD'] == "Y") {
$choice = $_Y['gamelist'];
// other stuff you want to do with the gamelist value
} else {
echo '<form method="Y" action="file.php">';
// the rest of your form
echo '</form>';
}
Replace Y with either GET or POST.
$choice = $_REQUEST['gamelist']; //works with get or post
$choice = $_POST['gamelist']
if it is a POST, or $_GET if not.