Checking number of seats available in PHP - php

This is the seat booking form for a cinema's website that I need to check the number of seats available when users select their seats.
There are 3 seat types (S, F, B) with the capacity of 40, 12, 13 respectively.
In HTML I have:
<input name="Stype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input name="Ftype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input name="Btype" type="number" value="" max="<?php echo /the number of available seats/?>" min="0" />
<input type="submit" name="submit" value="submit" />
Then I use php to get user inputs like this:
<?php
session_start();
$SSeats = '';
$FSeats = '';
$BSeats = '';
if(isset($POST['submit']))
{
//get inputs by method POST
//pass input data to the next shopping-cart page via SESSION
$_SESSION['SSeats'] = $SSeats;
$_SESSION['FSeats'] = $FSeats;
$_SESSION['BSeats'] = $BSeats;
}
else
{
$_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
$_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
$_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];
//Then I write above SESSIONs into a file, then output the number of
//seats available to the HTML code
}
?>
Before I test this code, I did tested many things else and the SESSIONs already have values in it, so the code can run well. But when I reopen the website and go to this page, it throws an exception that the indexes 'SSeats', 'FSeats', 'BSeats' are undefined.
The requirement also includes after users view their booking cart, they can add/remove seats in the reservation.
So could somebody suggest me the way to fix it or how to make it easier.

You have two errors:
Change $POST to $_POST
if(isset($_POST['submit']))
And you need $_POST['html_element_name'] to get the value
$_SESSION['SSeats'] = $_POST['SSeats'];
$_SESSION['FSeats'] = $_POST['FSeats'];
$_SESSION['BSeats'] = $_POST['BSeats'];
The code:
if(isset($_POST['submit']))
{
//get inputs by method POST
//pass input data to the next shopping-cart page via SESSION
$_SESSION['SSeats'] = $_POST['SSeats'];
$_SESSION['FSeats'] = $_POST['FSeats'];
$_SESSION['BSeats'] = $_POST['BSeats'];
}
else
{
$_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
$_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
$_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];
//Then I write above SESSIONs into a file, then output the number of
//seats available to the HTML code
}
?>

You have a typo in the if(isset). $_POST is missing an underscore, as well as you were not calling the $_POST variables when defining the session variables. You were calling blank variables.
<?php
session_start();
$SSeats = '';
$FSeats = '';
$BSeats = '';
if(isset($_POST['submit'])) //typo fixed here
{
//get inputs by method POST
//pass input data to the next shopping-cart page via SESSION
$_SESSION['SSeats'] = $_POST['SSeats'];
$_SESSION['FSeats'] = $_POST['FSeats'];
$_SESSION['BSeats'] = $_POST['BSeats'];
}
else
{
$_SESSION['SAvai'] = 40 - $_SESSION['SSeats'];
$_SESSION['FAvai'] = 12 - $_SESSION['FSeats'];
$_SESSION['BAvai'] = 13 - $_SESSION['BSeats'];
//Then I write above SESSIONs into a file, then output the number of
//seats available to the HTML code
}
?>

Related

how to change input text into url link

I have four input texts and one submit button
enter image description here
what I want is for the results of the input text to turn into a link that I use as a whatsapp message like this.
https://wa.me/628123456789?text=Hai%20My%20Name%20Andi
what should i do?
this is my code
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<!-- https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4 -->
<form action="https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
<input type="text" name="textInput1" id="textInput1" ><br><br>
<input type="text" name="textInput2" id="textInput2" ><br><br>
<input type="text" name="textInput3" id="textInput3" ><br><br>
<input type="text" name="textInput4" id="textInput4" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
//target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
$url = null;
if(isset($_POST['submit']))
{
$textInput1 = $_POST['textInput1'];
$textInput2 = $_POST['textInput2'];
$textInput3 = $_POST['textInput3'];
$textInput4 = $_POST['textInput4'];
$url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
}
?>
</body>
</html>
this is var_dump result for my code
enter image description here
Simply set your form to method post, see my code below. Add name attributes to your inputs fields and submit button so you can retrieve their values through http post. Then check to see if the submit button has been posted using isset($_POST['submit']). If isset, we assign the values of your input fields to variables in order to recreate the urls post key/value pairs.
IMPORTANT NOTE: I am not covering cleaning of your input fields make sure to read up on proper cleaning of inputs depending on what you are allowing to be processed by the back-end code in order to recreate your url.
<form action="/https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
<input type="text" name="textInput1" id="textInput1" ><br><br>
<input type="text" name="textInput2" id="textInput2" ><br><br>
<input type="text" name="textInput3" id="textInput3" ><br><br>
<input type="text" name="textInput4" id="textInput4" ><br><br>
<input type="submit" name="submit" value="Submit">
</form>
PHP:
//target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
$url = null;
if(isset($_POST['submit'])){
$textInput1 = $_POST['textInput1'];
$textInput2 = $_POST['textInput2'];
$textInput3 = $_POST['textInput3'];
$textInput4 = $_POST['textInput4'];
$url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
}
Example OUTPUT. Simply outputting the url within an html page in example:
In your html simply echo out the variable $url. it will only display in your code when it is actually set. <?=$url?> or <?php echo $url;?>
NOTE: If you are trying to place the link into your action attribute of your form prior to submitting the page so that the values set in the input fields are a part of the forms action, you will need to get the values before submitting the page by using JS or JQuery and getting the values of the inputs on change or something of that nature, then build the url in js/jquery then set your forms attribute action using JS/Jquery.
EDIT: I want the result of the input text to be a whatsapp message, so it will be placed in the url link. I have updated my post
Okay, to redirect user once the form is filled out and you have sanitized your inputs, set the url in your a header() function and redirect your user to the desired url.
*Make sure you remove the action attribute from your form as you will be redirecting using the php header() function instead.
if(isset($_POST['submit'])){
// I am using filter_var(FILTER_SANITIZE_STRING) in this example.
$textInput1 = filter_var ( $_POST['textInput1'], FILTER_SANITIZE_STRING);
$textInput2 = filter_var ( $_POST['textInput2'], FILTER_SANITIZE_STRING);
$textInput3 = filter_var ( $_POST['textInput3'], FILTER_SANITIZE_STRING);
$textInput4 = filter_var ( $_POST['textInput4'], FILTER_SANITIZE_STRING);
// make sure to test this url by echoing it out before you run the header redirect.
$url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);
header("Location: $url");
exit();
}
Using a conditional with a foreach loop with header to contruct url from post values:
if(isset($_POST['submit'])){
$url = "https://wa.me/628123456789?text="; // declare the core of your url without the post values
$i = 1; // increment
$k = 0; // key value for $inputs
$userinput = ''; // empty variable to hold user inputs for encoding
$num = count($_POST) - 1; // count the number of items in the array to properly format spaces in url string subtract one for submit button
foreach($_POST as $value){ // run a foreach loop on the $_POST
if($value !== "Submit"){ // we remove the submit post value from our array by omitting it using does not equal
$inputs[] = filter_var ( $value, FILTER_SANITIZE_STRING); // create a new array and push values into it
if($i < $num){ // all but last iterations will produce the space
$url .= $input[$k]." ";
}else{ // last iteration will not have a space
$url .= $input[$k];
}
}
}
$url .= urlencode($userinput);
echo $url; // for testing purposes to make sure the string is populating the input values as you have entered them delete this line after testing.
//header("Location: $url"); <-- Uncomment this line to redirect
//exit(); <-- uncomment exit() if you uncomment header() to close after redirect to make sure code stops on this page.
}
Just add the function urlencode() before the content:
$url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);

How to retain the values in an array when submit button is clicked [duplicate]

This question already has answers here:
PHP Pass variable to next page
(9 answers)
Closed 8 years ago.
php newbie here. How do I retain the value of an array when I click the submit button? So this how the program works. The user will input a name and a seat number(between 1-25) and when the the user clicks submit button, the name will be added on the array and will be displayed on the table. It works for the first input but when i entered another name and seat number, the first input is deleted. Please help.
//this is where the user will input...
Student Name: <input type="text" name="name" id="name"><br>
Seat Number: <input type="text" name="seat" id="seat"><br>
<input type="submit" name="assign" value="Assign"> <?php echo $warning; ?>
//and this is my php code.
<?php
$students = array_fill(0, 25, NULL);//this is to make an empty array with the size of 25.
if(isset($_POST['assign'])){
$student = $_POST['name'];
$seat = $_POST['seat'];
if($seat > 25 || $seat < 1){
$warning = 'Seat number does not exist.';
}
else{
$warning = '';
$students[$seat-1] = $student;
}
}
?>
//This code here is just a part of the HTML code for the table. this is where the name will display.
<td id="box"><?php echo $students[0]; ?></td>
<td id="box"><?php echo $students[1]; ?></td>
<td id="box"><?php echo $students[2]; ?></td>
<td id="box"><?php echo $students[3]; ?></td>
<td id="box"><?php echo $students[4]; ?></td>
Because every time you click on submit button,the $students array is created again and again and therefore you lose the previous values which the $students was holding.So create your array only once while submitting your form the first time.
EDIT: alternative: use php SESSION for this(just one of the many alternatives)
Before you can store user information in your PHP session, you must first start up the session.
<?php session_start(); ?>
<html>
<body>
</body>
</html>
And then you can store or retrieve anything in the session by manipulating $_SESSION array in php.So instead of storing seat no.s in a normal php variable you can use $_SESSION.Just keep everything the same(ie method="post" etc etc) and instead of $students use $_SESSION["students],where students will be an array in the $_SESSION(and it will remain there till your session expires ie till user logs out or closes the page in your case)
See here for more example: http://www.w3schools.com/php/php_sessions.asp
Use sessions like this:
<?php
session_start(); // To start the session, must be called before trying to manipulate any session variables. Preferably the first php-line on each page who will use sessions.
$_SESSION['students'] = array_fill(0, 25, NULL); // Sets an array of 25 indexes to session variable 'students'
?>
The code block above should not be called each time the user clicks submit - it should only be called once.
<?php
session_start();
if(isset($_POST['assign'])){
$student = $_POST['name'];
$seat = $_POST['seat'];
if($seat > 25 || $seat < 1){
$warning = 'Seat number does not exist.';
}
else{
$warning = '';
$_SESSION['students'][$seat-1] = $student; // Sets index [$seat-1] of array to $student
}
}
?>
Session variables are stored temporarily on the server and can be accessed throughout the website as long as you remember to start the session on each page (as shown in the code).
As Anmol said before, read more about sessions here: http://www.w3schools.com/php/php_sessions.asp
Edit:
<?php
session_start(); // To start the session, must be called before trying to manipulate any session variables. Preferably the first php-line on each page who will use sessions.
if(!isset($_SESSION['students'])){
$_SESSION['students'] = array_fill(0, 25, NULL); // Sets an array of 25 indexes to session variable 'students'
}
?>

how to prompt the user for for input repeatedly in php

here is what I want to do, i want to build a program that prompts the user for a string, stores the string in an array and prompts the user for another string, stores that string in an array and repeats the process 5 times, but every time I run the program, the string gets erased and it stores the same string 5 times, how can i modify this php code such that it does what I said? I tried using readline() but it doesn't work, it says it is an undefined function.
<form action = "index.php" method = "GET">
Name: <input type = "text" name = "tell" >
<br><br>
<input type = "submit" value = "submit">
</form>
<?php
$tester = array();
$counter = 5;
while ($counter > 0)
{
echo "please enter a criteria";
$name = $_GET['tell'];
array_push($tester,$name);
$counter = $counter - 1;
}
print_r($tester);
?>
PHP only runs when the page loads, so your code is only getting one value from $_GET. In order to do this, you have to start a session and store your array in a session variable.
<?PHP
session_start();
if($_GET['tell']) {
$tester = $_SESSION['tester'];
array_push($tester,$_GET['tell']);
$_SESSION['tester'] = $tester;
}
if(count($tester) < 5) {
//display your form to ask the user for more information
}

How to call upon a PHP function on button click?

I am trying to setup a website that converts a Steam user ID into an auth ID. It will ask for the visitor to input their regular Steam ID and then hit a button to convert it to auth ID. Steam provides us with the function for ID conversion from one type to the other.
Steam function for converting IDs:
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
Below is my attempt at setting up a basic HTML page that gets user input and then uses the function to convert that input to something else.
<INPUT TYPE = "Text" VALUE ="ENTER STEAM:ID" NAME = "idform">
<?PHP
$_POST['idform'];
$steamid = $_POST['idform'];
?>
Also, this is what the default Steam user ID looks like:
STEAM_0:1:36716545
Thank you for all the help!
If you can make it into two seperate files, then do so.
foo.html
<form method="POST" action="foo.php">
<input type="text" value="ENTER STEAM:ID" name="idform" />
<input type="submit" />
</form>
foo.php
<?php
function convert_steamid_to_accountid($steamid)
{
$toks = explode(":", $steamid);
$odd = (int)$toks[1];
$halfAID = (int)$toks[2];
$authid = ($halfAID*2) + $odd;
echo $authid;
}
$id = $_POST['idform'];
convert_steamid_to_accountid($id)
?>
if you don't have an option of making two seperate files, you can add the php code to 'foo.html' file and make the form to submit to the same file. However if you do this, check if the file is getting requested the first time, or it is requested because the form is submitted, BEFORE you call convert_steamid_to_accountid() function.
You can do this by:
if ($_SERVER['REQUEST_METHOD']=='POST'){
// your php code here that should be executed when the form is submitted.
}

Overwriting PHP variable and remembering it

So I have some data in MySQL being shown on my PHP page inside a table. I've set it up so that each page only displays 3 results each (temporary until it goes live, then it will be more). Everything works fine and it displays those 3 results on each page just fine.
What I want to do is be able to change the amount of results on each page right from the main PHP page. I can change the amount shown on that one page, but as soon as I go to page 2, it resets to 3 results. How can I remember the number stored in the variable, and display it on every page? I tried using SESSIONS, but I couldn't get it to work. I'm still a beginner btw.
$item = $_REQUEST['item'];
if(isset($_REQUEST['item'])){
$limit=$item;
}
else{
$limit=3;
}
//A few lines down -->
<form action="<?php $_SERVER['REQUEST_URI']?>" method="post">
Items: <input type="text" name="item">
<input type="submit" name"go" value="Go">
</form>
You could add SESSION use this way:
$item = $_REQUEST['item'];
if(isset($_REQUEST['item'])){
$limit=$item;
$_SESSION['item_limit'] = $limit;
}
else{
// If we find a limit set in the session use that
if (!empty($_SESSION['item_limit']) {
$limit = $_SESSION['item_limit'];
}
else {
$limit=3;
}
}
If you don't need to remember the value the next time the user visits the page you can simply output the $limit as a value attribute for the "item" input.
Items: <input type="text" name="item" value="<?php echo $limit; ?>">

Categories