i have created html form (in MyTestFile.php file) with several options, and what i want to do is to check if post request has been recievied. Or in other words when user chooses something from my combobox and pushes button "Go" then it goes to the same page and sends choosed size value and echoes the size, and then form is not visible any more when size has been set. So here is my form code which isn't really working:
if (isset($_POST('submit'))) {
?>
<html>
<h2>Select size:</h2>
<form action="MyTestFile.php" method="POST">
<div>
<select name="SIZE">
<option value="big">big</option>
<option value="medium">medium</option>
<option value="small">small</option>
</select>
<input type="submit" value="Go">
</form>
</html>
<?php
} else {
echo $_POST('submit');
//and do some stuff here
}
The main idea: check if post request with size has been recieved, if not - show the form, where user can choose size.
$_POST is an array, so you must use square brackets to access the elements, not parenthesies. So change this:
if (isset($_POST['submit'])) {
// ^ ^ square breackets not ()
Secondly your submit button doesn't have a name, so your isset() check will never be true. Give it a name:
<input type="submit" name="submit" value="Go">
Or use the SIZE select in the isset check:
if (isset($_POST['SIZE'])) {
Finally your isset check needs reversing, in order to show the form when the post value is not present:
if (!isset($_POST['SIZE'])) {
// ^ not operator to reverse the check
I can see you tried so here you go:
Just add an exclamation mark before isset, add a name to the submit button and replace those parentheses with square brackets.
if (!isset($_POST['submit']))
{
?>
<html>
<h2>Select size:</h2>
<form action="MyTestFile.php" method="POST"><div>
<select name="SIZE">
<option value="big">big</option>
<option value="medium">medium</option>
<option value="small">small</option>
</select>
<input type="submit" name="submit" value="Go">
</form>
</html>
<?php
}
else
{
echo $_POST['size'];
//and do some stuff here
}
Use [ don't use ( in $_POST method. Because $_POST is array so use square brackets. Use same submit as name in submit button.
$_POST['submit'] instead of $_POST('submit')
and add ! (Not Set) in if condition, use below code,
if(!isset($_POST['submit'])){
// Your Form Code
}
else{
print_r($_POST);
}
Related
I have a select list, but on page reload , the data in the list is not saved of corse.
I have fixed this with TextBoxes and Radio buttons by reading the variables from $_GET.
Here is an example of the form I have now:
<form action="" id="exampleForm" method="get">
<input type="checkbox" name="exampleCheckbox" <?php if (isset($_GET['exampleCheckboxStatus'])) {echo "checked";} ?>>Check this
</br>
<select name="exampleList" multiple>
<option>Apple</option>
<option>Banana</option>
<option>Cherry</option>
</select>
<input type="submit" value="Submit" id="submitButton"> </form>
I would like to keep the values of the 'exampleList' once submitted
(I stay on the same page)
I have seen posts on here that almost look like what I ask, but most of them want to use javascript. Is there an solution for my problem, wich look similiar to what I already have right now? I would like to fix this with php because I dont think I have enough knowledge of Javascript (yet)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var opts = localStorage.getItem('opts'); // get selected items from localStorage key
opts = opts.split(','); // split result from localstorage to array
$('#exampleList').val(opts); // select options with array
});
</script>
<html>
<body>
<select id="exampleList" multiple>
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="cherry">Cherry</option>
</select>
</body>
</html>
When you POST the form you only need to write the selected option values, comma separated, to the localstorage.
I finally found a solution:
The only flaw is the order of the :)
But since I use a plugin for displaying it does not matter much.
The fix:
I created 2 Array lists
list1 with everying in it
list2 with all selected values
Then I subtract list2 from list1 and dont have duplicates
So I can print both in different print methods.
<?php error_reporting(E_WARNING);
$fruitArray = array("Apple", "Banana", "Cherry", "Durian", "Eggfruit", "Fig", "Grapefruit");
$selectedFruitArray = $_GET['exampleList'];
$fruitArray = array_diff($fruitArray, $selectedFruitArray);
?>
<form action="" method="get">
<select name="exampleList[]" multiple>
<?php
foreach($fruitArray as $value) {
echo "<option value='$value'>$value</option>";
}
foreach($selectedFruitArray as $value) {
echo "<option value='$value' selected>$value</option>";
}
?>
</select>
<input type="submit">
</form>
Use FormRepo, a plugin specially made for retaining form data
on page refreshes.
Its usage is also simple:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script src="FormRepo.js"></script>
<script>
var $form = $('#input')
, $output = $('#output')
, repo = new FormRepo('restclient')
;
// get the last submitted values back
repo.restore($form/*, $form.attr('id')*/ ); // don't necessarily need an identifier
$form.submit(function (e) {
// preserve the last submitted values
repo.preserve($form/*, $form.attr('id')*/ ); // don't necessarily need an identifier
});
console.log( repo.all() );
</script>
You can do it by using session. This is the way using it you can store last selected value in session. Session value will not be destroyed even if you reload paga.
For e.g.,
<?php
session_start(); // Other Code
<div>
<p>Subtitle needs to be
<input type="radio" name="subTitleRadio" <?php if ($_SESSION['subTitleRadio'] != "LIKE") echo "checked"; ?> value="LIKE">contain
<input type="radio" name="subTitleRadio" <?php if ($_SESSION['subTitleRadio'] == "=") echo "checked"; ?> value="=">be equal to
</p>
<input type="search" name="subTitleSearchBox" placeholder="filter for subtitle" class="chosenStyle" value="<?php echo $_GET['subTitleSearchBox'];?>">
</div> //Other Code
?>
PHP Code for set value in session after submit :
<?php
session_start(); //Not required if your form action is on same page, else required //Rest code
$_SESSION['subTitleRadio'] = $_GET['subTitleRadio'] // OR $_POST['subTitleRadio']; // Rest code
?>
Same code works for me.
first of all at value parameters to the options, then you can check if exampleList has the right value and use that. for example:
<option value="apple" <?php if (isset($_GET['exampleList']) && $_GET['exampleList'] == "apple") echo "selected=\"selected\""; ?>>Apple</option>
Well, you could try something along these lines. It's a bit lengthy, you could shorten it up quite a bit. By showing it this way, I hope it's simpler to understand.
<form action="" id="exampleForm" method="get">
<?php
if (isset($_GET['exampleCheckboxStatus'])) {
echo '<input type="checkbox" name="exampleCheckbox" checked> Check this';
} else {
echo '<input type="checkbox" name="exampleCheckbox"> Check this';
?>
<br />
<select name="exampleList[]" multiple>
<?php
if( in_array('apple', $_GET['exampleList']) ) {
echo '<option value="apple" selected>Apple</option>';
} else {
echo '<option value="apple">Apple</option>';
}
if( in_array('banana', $_GET['exampleList']) ) {
echo '<option value="banana" selected>Banana</option>';
} else {
echo '<option value="banana">Banana</option>';
}
if( in_array('cherry', $_GET['exampleList']) ) {
echo '<option value="cherry" selected>Cherry</option>';
} else {
echo '<option value="cherry">Cherry</option>';
}
?>
</select>
<input type="submit" value="Submit" id="submitButton">
</form>
Note that I added [] to the select's name and corrected the br tag.
Adding [] will change the type from "string" (text) to an array (several texts). Then we can check what texts are included.
Try it for yourself, play around with the code a bit.
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?
drop down select "sales" go to sales.php, other options selected go to addFund.php...
<form name='searform' method='post' action='<?php echo $home;?>addFund.php'>
Search : <input type='text' id='sear' name='sear'> by
<select id='psel' name='psel' onchange='change()'>
<option value='email'>Email</option>
<option value='name'>Username</option>
<option value='domain'>Domain name</option>
<option value='sales'>Sales</option>
</select>
<input type='submit' name='sub' id='sub' value='Go' onclick='gopage()'>
<input type='submit' name='dir' id='dir' value='Direct'>
</form>
How to submit different pages
You could use some Javascript nastiness to achieve this with the change() function, but the usual way to do this is to route all requests through a controller and include() the appropriate page. For example, point your form to action.php, and in that file, do this:
action.php
<?php
if (isset($_POST['psel']) && $_POST['psel'] == 'sales') {
include 'sales.php';
} else {
include 'addFund.php';
}
...or you could just put roughly that code into addFund.php, since you only seem to have one other script that you would want to send requests to.
You could do this with javascript:
function change(el) {
if(el.value === 'sales') {
el.form.action = 'sales.php';
} else {
el.form.action = 'addFund.php';
}
}
Change the onchange to onchange="change(this)". A better way would be to check the variable on serverside and include the right file.
Change your select to this:
<select name="vote" onChange="document.forms['searForm'].submit()">
And make your form action go to something like pageChange.php where it returns a different page depending on the $_POST value.
I have a pege (index.php) with some text and a form with a single text input field. What i want is for the user to be sent to another page, upload.php if the input is "upload" or browse.php if input is "browse" and so on, how can this be achieved?
Thank you
<?php
$page=$_POST['txtpage'];
$redirect=$page.'.php';
if(file_exists($redirect))
{
header('Location:'.$redirect);
}
?>
You could use header() to send a location header depending on the value of the input.
switch ((string) $_POST ['mode'])
{
case 'browse':
case 'upload':
// Redirect
header('Location: http://www.example.com/' . $_POST ['mode'] . '.php');
die ();
break;
default:
// invalid input
break;
}
<form action=<?php echo ($_SERVER ['SCRIPT_NAME']); ?>
<select name="mode">
<option value="">select...</option>
<option value="upload">Upload something</option>
<option value="browse">Browse files</option>
<input type="submit" />
</select>
</form>
if you want redirect on another page when blur this textfield
<input onblur="window.location.href='YOUR_URL?value='+this.value" />
if you want redirect on another page when keyup on this textfield
<input onkeyup="window.location.href='YOUR_URL?value='+this.value" />
This will work for you
<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.