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).
Related
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;
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.
This has been bugging me and I can't seem to figure out how to get around it. Say I have an index.php that includes another page dostuff.php that's just a big function. On my index page I have a conditional that gets called by a button in a form:
---index.php---
<?php
include 'dostuff.php';
if (isset($_POST['test'])) {
test();
}
echo '<form id="test" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">'.PHP_EOL;
echo '<button name="test" form="test" type="submit" value="test">Test</button>'.PHP_EOL;
echo '</form>'.PHP_EOL;
In my included function I have another form that calls another conditional within the function.
---dostuff.php---
<?php
function test() {
if (isset($_POST['dostuff'])) {
echo '<h1>Testing Do Stuff.</h1>';
}
echo '<form id="dostuff" method="post" action="'.htmlentities($_SERVER['PHP_SELF']).'">'.PHP_EOL;
echo '<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>'.PHP_EOL;
echo '</form>'.PHP_EOL;
}
When I click the button on the index page the include is called and populates the page with its form. However, when I click the button from the function, the form disappears and the conditional never executes.
Now, if I add the following to the index page:
print "CONTENT_TYPE: " . $_SERVER['CONTENT_TYPE'] . "<br>";
$data = readfile('php://input');
$contents = file_get_contents('php://input');
print "<br>";
print "DATA: <pre>";
var_dump($contents);
var_dump($data);
var_dump($_POST);
print "</pre>";
Clearly the form button is being called as verified by the var_dump call but the conditonal never executes.
CONTENT_TYPE: application/x-www-form-urlencoded
dostuff=dostuff
DATA:
string(15) "dostuff=dostuff"
int(15)
array(1) {
["dostuff"]=>
string(7) "dostuff"
}
I searched here and just about everywhere else for an answer to this to no avail, so my question is why won't anything inside the conditionals work? This seems very odd to me so I cobbled this together to test with.
It's just pure logic - it'll be a lot clearer if you make your forms use method="GET" instead of POST.
When you load it in the beginning you click on the button named "test" and so you have a $_POST['test'] value when you load the next time.
Your code looks like this in index.php:
if (isset($_POST['test'])) {
test();
}
so of course test() is called and you see your HTML from dostuff.php.
The button in dostuff.php is named "dostuff" so when you push that button the form is loaded again with a value in $_POST['dostuff'] ''BUT NO VALUE IN $_POST['test']!!''
Once again your logic at the top of index.php is this:
if (isset($_POST['test'])) {
test();
}
and since there is no value in $_POST['test'] your test() function never gets called and you never see the button you're expecting.
Really - change your forms to GET (you'll have to use $_GET or $_REQUEST instead of $_POST) so you can see exactly what is set each time without having to resort to all the fancy file reading and I think it'll be really clear.
The question hasn't changed, see below as what I have to show doesn't fit in comments.
If I'm in index.php and I view my source, I get this:
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
If I click test I now get:
<form id="dostuff" method="get" action="/test/index.php">
<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>
</form>
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
If I use a condition to call my function I get nothing back. If I eliminate the condition on index.php, and just call test(); I get back:
<form id="dostuff" method="get" action="/test/index.php">
<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>
</form>
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
Now if I click "Do Stuff!" I get back:
<h1>Testing Do Stuff.</h1>
<form id="dostuff" method="get" action="/test/index.php">
<button name="dostuff" form="dostuff" type="submit" value="dostuff">Do Stuff!</button>
</form>
<form id="test" method="get" action="/test/index.php">
<button name="test" form="test" type="submit" value="test">Test</button>
</form>
So, the question remains the same. Why doesn't this work?
First time i try to create a simple form using the POST method.Problem is when i click the button nothing gets echoed.
here is my insert.php file :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
EDIT: I tried the GET method and it works...Any ideas why that happened? Server configurations?
NEW EDIT: So it turns out i switched method to GET and it worked.Then i switched back to POST (like the code i posted on top) and it works...I have no clue why this happened.Any suggests?
The code you have posted is perfectly valid and should work.
I'm going to guess that you do not have PHP enabled, or it is not working.
<?php ... ?> looks to the browser like a long, malformed HTML tag, and therefore ignores it, making the effect invisible.
Try right-clicking the page and selecting View Source. If you see your PHP there, then the server is indeed not processing it.
The most likely reason for this is probably the same problem I had with my very first bit of PHP code: you're trying to "run" it directly in your browser. This won't work. You need to upload it to a server (or install a server on your computer and call it from there)
Use !empty($_POST['newitem'] instead:
if(!empty($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
empty()
Try the following:
if($_POST) {
if(!empty($_POST['newitem'])) {
$itemnew = $_POSΤ['newitem'];
echo $itemnew;
// or leave it as is: echo $itemnew = $_POSΤ['newitem'];
}
}
?>
<form action="insert.php" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
The if($_POST) will make sure the code is only executed on a post. The empty() function will also check if it isset() but also checks if it is empty or not.
Try this :
<?php
if(isset($_POSΤ["newitem"])){
echo $itemnew = $_POSΤ["newitem"];
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" >
<input type="text" name="newitem">
<input type="submit" value="Save">
</form>
$_SERVER['PHP_SELF']; is pre-defined variable in php.It allows the user to stay on same page after submitting the form.
I'm trying to change the form tag below in order to use jQuery. Already, clicking the buttons changes the display from rows to columns and vice-versa but I want to avoid the page refresh. I'm really new at jQuery and can't honestly say what my mistakes are when trying to change it myself.
<form id="rowsToColumns" action="index.php?main_page=specials&disp_order=1" method="POST">
<input type="hidden" name="style_changer" value="columns"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Column</button>
</form>
<form id="columnsToRows" action="index.php?main_page=specials&disp_order=1" method="POST">
<input type="hidden" name="style_changer" value="rows"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Rows</button>
</form>
I'm also trying for the buttons to call a different stylesheet upon click. This stylesheet is not needed for the display to change from/to rows/columns as I mentioned above. The actual page is written using php as shown below:
<?php $this_page = zen_href_link($_GET['main_page'], zen_get_all_get_params()); ?>
<div id="style_changer">
<?php if($current_listing_style == 'rows') {?>
<form id="rowsToColumns" action="<?php echo $this_page;?>" method="POST">
<input type="hidden" name="style_changer" value="columns"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Column</button>
</form>
<?php } else { ?>
<form id="columnsToRows" action="<?php echo $this_page;?>" method="POST">
<input type="hidden" name="style_changer" value="rows"/>
<button type="submit" class="btn btn-large btn-primary" type="button">Change to Rows</button>
</form>
<?php } ?>
</div>
If the question is "how to change a form in order to use jQuery and avoid the page refresh", then the jquery form plugin is your friend, as it turns any html form into an ajax-powered one.
Simply follow their instructions and you'll get it working in no time (provided your form already works as is).
You can prevent the Default form Submission by preventing the default action on the submit button..
$('button[type=submit]').submit( function(e){
e.preventDefault(); // Stops the form from submitting
});
Well, for a very vague method you can use $.ajax and take advantage of reading the <form>'s pre-existing attributes to decide on submission method and read the elements' values as submissiong data:
$('form').on('submit',function(e){
var $form = $(this);
// submit the form, but use the AJAX equiv. instead of a full page refresh
$.ajax({
'url' : $form.attr('action'),
'method' : $form.attr('type'),
'data' : $form.serialize(),
'success' : function(response){
// process response (make CSS changes or whatever it is
// a form submission would normally do)
}
});
// prevent the normal submit and reload behavior as AJAX is now
// handling the submission
e.preventDefault();
});
However, for this to work you'll need some variation of a stripped-down PHP response just for the purpose of the AJAX request (avoid resending headers, script tags, etc. and just return the raw data that jQuery can use to make a UI decision).