I have a form on page a.php with php code which should set the data from the form into php session variables, however i am having trouble making it work. I have session_start(); at the beginning of every page i want to do this, but its at the top of the html so that is why its not in this piece.
Here is the code:
<form class="form1" method="post" action="" id="form1">
<div class="form-group add_to_cart_prompt">
<span class="">Add something to cart</span>
</div>
<div class="form-group">
<input type="text" name="sticker" class="form_sticker_name" value="something">
<label class="quantity_desc" for="quantity" title="how much?">Quantity</label>
<input class="btn btn-default quantity_input" type="number" id="quantity" name="quantity" placeholder="how much ?" min="0" required>
</div>
<div class="form-group bottom_buttons">
<button type="submit" name="submit" class="btn btn-default add_yes">Add to cart</button>
<button type="reset" class="btn btn-default">Clear</button>
<button type="button" class="btn btn-default add_no">Close</button>
</div>
</form>
PHP which is executing the form is on the same page as the form:
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['sticker'])) {
$sticker_name = $_POST['sticker'];
$_SESSION['sess_sticker'] = $sticker_name;
}
}
?>
I can set session variables to some string, and that works ok, but when i try to put data from the form it gives me this error:
Notice: Undefined index: sticker in
B:\Programs\xampp\htdocs\D2S\shopping_cart.php on line 99
I have looked up how to fix the error, and have been trying a lot and can't fix it. Thank you for trying to help.
In PHP, a variable or array element which has never been set is different from one whose value is null; attempting to access such an unset value is a runtime error.
That's what you're running into: the array $_POST does not have any element at the index "sticker", so the interpreter aborts your program before it ever gets to the nullity test.
You can test for the existence of a variable or array element without actually trying to access it; that's what the special operator isset does:
if (isset($_POST['sticker'])) {
//do something
}
Also I have tested your form code on my machine, and it seemed to be working fine and session variable was set using form data.
Related
This questions looks stupid, but I'm not able to pass the variable $total from my php file to a html form withou using the include_once. I cannot use include_once at the beginning of the html file, because I'm using temp variable and it will give a error if I try to do it. I would like to include it directly into the form.
My php file doRequest.php has all the application logic and I have this variable $total that come from my logic and I want to print inside of the form. My method addGradeValueToUsers receives two parameters from different files. How I do to display $total inside of my method?
<form action="doRequest.php" id='requestForm' method="post" enctype="multipart/form-data">
<input class="btn btn-default" type="button" value="Add Grades" onclick="addGradeValueToUsers(<?php echo $total; ?>"/>
</form>
You can pass the total variable's value through a cookie:
<?php
//doRequest.php
$total = someInternalLogic();
setcookie('total', $total);
?>
<!-- php that contains the form -->
<form action="doRequest.php" id='requestForm' method="post" enctype="multipart/form-data">
<input class="btn btn-default" type="button" value="Add Grades" onclick="addGradeValueToUsers(<?php echo $_COOKIE['total']; ?>)"/>
</form>
Be aware that setcookie() must be called before any HTML output in your doRequest.php file (if any).
I've spent ages trying to figure this out after reading guide after guide and I can't get it. I have 3 files - user.php, map.php and newaddress.php. I've only included what I understand to be the important bits to this issue. Happy to provide more info if needed.
user.php passes a "mapnumber" to map.php.
user.php
<form action="map.php" method="post">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
map.php receives "mapnumber" and generates a list of addresses from the database with matching "mapnumber". From map.php, user can add new addresses which take the "mapnumber" value and is processed in newaddress.php.
map.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
newaddress.php adds the address to database with the "mapnumber" value and then redirects back to map.php where it SHOULD generate the list of addresses based on "mapnumber" but map.php doesn't pick up the "mapnumber" from newaddress.php and hence the list of addresses isn't generated.
newaddress.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php");
exit;
Please help
You can set the get variable like this:
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?nr=". $mapnumber);
exit;
Than in map.php you get get it by:
$mapnumber = $mysqli->escape_string($_GET['nr']);
You could use cookies to store values in the remote browser.
But the real problem you encounter is that the Location redirect is just that. It tells the browser to load another URL.
You can add get requests to that Location redirect URL like that:
header("Location: map.php?mapnumber=".urlencode($mapnumber));
But please keep in mind that the default Location redirect will be a permanent one (HTTP Status Code 301) and it is up to the browser to recheck if the redirecting url/document might have changed or not.
http://php.net/manual/en/function.header.php#78470
So you might want to consider using a 307 redirect:
header("Location: map.php?mapnumber=".urlencode($mapnumber), TRUE,307);
Anyway: passing variables from one page to another is generally not a good practice. Try to avoid it and try to dig into sessions.
http://php.net/manual/en/intro.session.php
I recommend you to use get method in map.php
Then it will receive map number like map.php?mapnumber=somenum
user.php
<form action="map.php" method="get">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
map.php
$mapnumber = $mysqli->escape_string($_GET['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
newaddress.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?mapnumber=$_POST['mapnumber']");
exit;
<form entype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
Hi hello I want to ask a simple question. The code above is search.php,
I want to send the form to a different page based on if the checkbox is checked or not. If the checkbox is checked it will be directed to resqueryexp.php, but if not it will be directed to result.php
I have been trying to adding this code but it doesn't work.
<?php
if (isset($_GET['queryexp'])){
header("Location: resqueryexp.php");
}else{
header("Location: result.php");
}?>
Sorry for my bad English and Thanks in advance.
<?php
if ( isset( $_GET['submit'] )) {
if ($_GET['queryexp'] == 1 ){
header("Location: resqueryexp.php");
exit;
}
else
{
header("Location: result.php");
exit;
}
}
?>
<html>
<head><title>test</title>
</head>
<body>
<form enctype="multipart/form-data" method="GET" action="">
<div class="box-body">
<input class="form-control input-lg" name="keyword" type="text" placeholder="Masukkan kata kunci">
</div>
<div class="box-body">
<input value="1" type="checkbox" class="minimal" name="queryexp" />
Gunakan query expansion
</div>
<div class="box-footer">
<button type="submit" name="submit" class="btn btn-primary" value="Submit">Submit</button>
</div>
</form>
This code won't run here at SO, but this is how it may work on your webserver. The important part is to test if the form was submitted. So, in this case, I gave the submit button the name of "submit" and then tested with PHP to see if the form was even submitted. If the form is submitted and if the checkbox is checked, the redirect via header() occurs. Otherwise, if the checkbox is unchecked, then the redirect occurs via header to result.php. You may avoid header issues by making an adjustment to you PHP.ini settings and adding this line "output_buffering = On".
Note: usually a form with the enctype attribute having a value of "multipart/form-data" involves submitting a file and under such circumstances the method attribute should be a POST request instead of a GET; see MDN.
Remember that header() must be called before any actual output is
sent, either by normal HTML tags, blank lines in a file, or from PHP.
It is a very common error to read code with include, or require,
functions, or another file access function, and have spaces or empty
lines that are output before header() is called. The same problem
exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
http://php.net/manual/en/function.header.php
I'm trying to send form data from Vue Resource to my PHP page. I can see it displayed on the PHP page but when I send it back as JSON I get an empty response. Why is it sending an empty response?
Edit:
It looks like the problem is that the value for the submit button is not set in PHP. I am not sure why that is happening. Tried using $_REQUEST and axios/$.post but it makes no difference.
PHP:
if(isset($_POST['submit']){
echo json_encode($_POST);
}
JS:
this.$http.post('addalbum.php', new FormData($('#submitalbum')))
.then(data => console.log(data));
HTML:
<form class="col s12" id="submitalbum" method="post" action="addalbum.php">
<div class="row">
<div class="input-field col s6">
<input name="artist" placeholder="Artist" type="text">
</div>
<div class="input-field col s6">
<input name="title" placeholder="Title" type="text">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input name="genre" placeholder="Genre">
</div>
</div>
<div class="row">
<div class="input-field col s12">
<input id="released" type="number" name="released" placeholder="Year Released">
</div>
<button #click.prevent="addNewAlbum" name="submit" class="waves-effect waves-light btn">Submit</button>
</div>
</form>
The empty response is returned for multiple reasons. See them explained below.
jQuery selector
It appears that the selector for finding the form uses the jQuery id selector:
this.$http.post('addalbum.php', new FormData($('#submitalbum')))
But the jQuery function (i.e. $()) returns a collection (an array):
var submitAlbumCollection = $('#submitalbum');
console.log('submitAlbumCollection: ', submitAlbumCollection);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="col s12" id="submitalbum" method="post" action="addalbum.php"></form>
However FormData() needs "An HTML <form> element"1, not an array.
So if you are going to use the jQuery function, take the first element from it to get the reference to the form:
var forms = $('#submitalbum');
if (forms.length) {
this.$http.post('addalbum.php', new FormData(forms[0]))
button elements don't send value
A <button> is not a form input and hence there would not be a corresponding value in the form data. This if you want to check for form values, try checking for the artist, title, etc.
if(isset($_POST['artist'])) {
echo json_encode($_POST);
}
If you really wanted to check if $_POST['submit'] is truthy, you could:
add a hidden input:
<input type="hidden" name="submit" value="1">
convert the button to a submit button
<input type="submit" name="submit" value="1">
take the suggestion from this answer and manually append an entry to the form data
See a demonstration in this phpfiddle. Note that phpfiddle doesn't allow multiple pages so the code from addalbum.php was placed at the top, and references to it were replaced with <?php echo $_SERVER['PHP_SELF'];?> (i.e. in the client-side code).
Missing parenthesis
Additionally, there is a missing parenthesis on the first line of the PHP code:
if(isset($_POST['submit']){
To correct this, add the missing closing parenthesis to close the expression:
if(isset($_POST['submit'])){
1https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData
I have a problem with get the connection variable for open a database connection.
This my code in html
<form action="password.php" method="post">
<div class="form-group">
<input type="password" class="form-control" name="current" placeholder="Contraseña Actual..." />
</div>
<div class="form-group">
<input type="password" class="form-control" name="new" placeholder="Nueva Contraseña..." />
</div>
<div class="form-group">
<input type="password" class="form-control" name="confirm" placeholder="Repetir Nueva Contraseña..." />
</div>
</div>
<div class="modal-footer">
<input type="hidden" name="q" value="proofQueries">
<button type="button" class="btn btn-default" data-dismiss="modal">Cerrar</button>
<button type="submit" class="btn btn-primary"><i class="fa fa-plus"></i> Cambiar</button>
</form>
While the code of my class php
$settings = new Datasettings();
require_once('../config.php'); // file of connection of PDO
$conexion = new Conexion();
if(isset($_POST['q'])){ // get the name from html form for go to a function of this class
$settings->$_POST['q']($conexion);
}
class Datasettings {
function __construct(){
session_start();
if(!isset($_SESSION['id'])){
header('location:mystyle.css');
}
}
function proofQueries($conexion){
}
... other functions....
Could change the model how I call a the function? How I could make it?
I assume by this code:
if(isset($_POST['q'])){ // get the name from html form for go to a function of this class
$settings->$_POST['q']($conexion);
}
And submitting the hidden form field called q with value proofQueries, you are trying to call $settings->proofQueries($conexion). This is an extremely bad idea.
You are effectively executing code that comes directly from client side, which is a HUGE vulnerability risk.
It seems like a strange approach to begin with to specify the function client side, and then execute it in PHP (i.e. server side). Why specifying the q value at all, instead of just explicitly doing $settings->proofQueries($conexion) in PHP?
If you somehow must specify the function to be called client side, do something like this:
if(isset($_POST['q'])){ // get member function from submitted form
$f = $_POST['q'];
if ($f=='proofQueries') {
$settings->proofQueries($conexion);
}
else {
die("Nope");
}
}
Or if you have multiple possible functions, explicitly filter them with a whitelist to make absolutely 100% sure that ONLY the function names you decide can be called:
if(isset($_POST['q'])){ // get member function from submitted form
$f = $_POST['q'];
$allowedFunctions = array('proofQueries','doSomething','otherFunction');
if (in_array($f,$allowedFunctions)) {
$settings->$f($conexion);
}
else {
die("Nope");
}
}
But again, it seems like a strange approach alltogether. You should not specify server side specific implementation details through client side.