hey there,
i have three pages:
(1) bookingfacilities.php
(2) booking_now.php
(3) successfulbooking.php
and they are link together.
i want to pass data from bookingfacilities.php to successfulbooking.php by using hidden field/value. however, my data doesn't get print out in successfulbooking.php.
here are my codes:
from 'booking_now.php':
$date="$day-$month-$year";
from 'successfulbooking.php';
<input type="hidden" name="date" id="hiddenField" value="<?php print "$date" ?>"/>
i would greatly appreciate your help as my project is due tomorrow :(
You should never assume register_global_variables is turned on. Even if it is, it's deprecated and you should never use it that way.
Refer directly to the $_POST or $_GET variables. Most likely your form is POSTing, so you'd want your code to look something along the lines of this:
<input type="hidden" name="date" id="hiddenField" value="<?php echo $_POST['date'] ?>" />
If this doesn't work for you right away, print out the $_POST or $_GET variable on the page that would have the hidden form field and determine exactly what you want and refer to it.
echo "<pre>";
print_r($_POST);
echo "</pre>";
Maybe a little late to the party but why don't you use sessions to store your data?
bookingfacilities.php
session_start();
$_SESSION['form_date'] = $date;
successfulbooking.php
session_start();
$date = $_SESSION['form_date'];
Nobody will see this.
You have to use $_POST['date'] instead of $date if it's coming from a POST request ($_GET if it's a GET request).
I'm not sure what you just did there, but from what I can tell this is what you're asking for:
bookingfacilities.php
<form action="successfulbooking.php" method="post">
<input type="hidden" name="date" value="<?php echo $date; ?>">
<input type="submit" value="Submit Form">
</form>
successfulbooking.php
<?php
$date = $_POST['date'];
// add code here
?>
Not sure what you want to do with that third page(booking_now.php) too.
Related
Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.
I have a normal form in a PHP page, I send data to the php page from another for using POST. The PHP page runs some scripts to update data to SQL but on that page I have a second form that needs to be completed with data from the initial form prior to updating the SQL.
$recipient_nr = $_REQUEST['recipient_nr'];
Here I draw the info from the first form
Now I want to use this in a new form on the current PHP page how do I state this in the new form
<input type="text" name="recipient_nr" id="recipient_nr" value=".$recipient_nr.">
This is what I am attempting but it is not working I know I have too many "'" xxx"'" in the lines but not sure how to remidy this
Do you generate the new form in PHP? If so, where is the code where you do that?
If this is some kind of ...?> <input type="..."...> <?php ... page generation then you'll need to echo that $recipient_nr into the PHP-generated response:
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?php echo $recipient_nr; ?>">
<?php
...
Or, if you have short echos turned on,
...
?>
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?= $recipient_nr ?>">
<?php
...
use this:
<input type="text" name="recipient_nr" id="recipient_nr" value="<?php echo $recipient_nr; ?>">
Something like this:
$recipient_nr = array_key_exists('recipient_nr', $_REQUEST)
? (string) $_REQUEST['recipient_nr']
: 'default value or empty string';
And output it:
<input type="text"
name="recipient_nr"
id="recipient_nr"
value="<?=$recipient_nr?>">
But if you want store this data for/between other pages you can use $_SESSION global array for save it.
I got this and I have no idea what I'm missing here:
<?php
//Some validation for the SUBMIT form
if(isset($_POST['submit'])&&$_POST['submit']=='add'){
$_POST = array_map("mysql_real_escape_string", $_POST); //This little fella is responsible for the mess ¬¬
$campus_string = $_POST['campus']; //To get a checkboxes Array
....
print_r($campus_string); //to see if I am getting the checkboxes when submitting
}
?>
....
//Now inside <body> of the HTML
<form action="" method="post" name="filosofal">
//A little loop to create the checkboxes from a DB
foreach($campi as $keyCampi => $valueCampi){
echo '<tr>
<td>
<input type="checkbox" id="campus[]" name="campus[]" value="'.$value['Id'].','.$valueCampi['Id'].'" />'.$valueCampi['Nombre'].'<br />
</td>
</tr>';
}
</form>
But print_r doesn't show anything, the array is not being stored when submitting via POST. Hope you can help me to pinpoint where I'm screwing it.
EDIT: Solved
Well, I finally figured it out, it's kind of embarrasing.
In my code, I use:
$_POST = array_map("mysql_real_escape_string", $_POST);
to avoid some encoding conflicts (like names with 's on them), security and such.
I commented the line and it works now (didn't add that part since I wasn't aware its relevance on the issue), no changes needed to be done.
Don't know why it took me five days to find that little thing over there, but now is done. Anyways, thanks everyone.
Try adding
<input type="hidden" name="submit" value="add" />
Into your form, at the moment your if statement will be returning false...
Try dumping the post data:
echo "<pre>";
print_r($_POST);
//if you check the radio then it will be listed in your $_POST dump
//add action to your form
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php
foreach($campi as $keyCampi => $valueCampi){
?>
<tr>
<td>
<input type="checkbox" id="campus[]" name="campus[]" value="<?php echo $valueCampi['Id'].','.$valueCampi['Id']; ?>" /><?php echo $valueCampi['Nombre'].'<br />
</td>
</tr>';
<? php } ?>
</form>
Generally speaking, the way I see it is that checkbox is either ON or OFF. Therefore, in my mind, the name of the checkbox is the value. For instance:
<input type="checkbox" name="single"> Single?
If that checkbox is checked (value="on"), then the answer is yes. If it is not checked (value="off" or no value), then the answer is no. Therefore, my PHP code looks something like:
if ($_POST['single'] == 'on')
$single = true;
else
$single = false;
Basically, as far as I'm concerned, the "value" of a checkbox should never be set. That's my particular preference though, and it's worked well for me. It may not suit your needs, though.
Good luck.
I can't set a variable from a post array.
I have a simple form with a hidden field in it:
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
This hidden field gets sent off to a second file (exec.php) where I have the following code:
$sid = $_POST['sid'];
For some reason, when trying to set $sid, it gets a NULL value. For haha's, I ran the following:
foreach($_POST as $var => $value)
{
echo $var . ' : ' . $value . "<br>";
}
This provided a correct value of 1938 for sid. I've looked at this for 3 hours and can't find what is happening. I expect something extremely stupid...any thoughts?
Here is the form on enter.php
<form name="form1" method="post" action="exec.php">
<input name="sid" type="hidden" id="sid" value="<? echo($sid); ?>">
<input name="ticket_totals" type="hidden" id="ticket_totals" value="<?=$ticket_totals?>">
<input name="emp" type="hidden" id="emp" value="<?=$emp?>">
<input name="submit" type="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Close">
</form>
Here is the POST output on exec.php:
type : Other
ticket_totals : 0
emp : 105
sid : 1939
submit : Submit
Okay - this was poor syntax on my part but now I'm curious as to why.
I left out quotation marks - the solution is as simple as this:
$sid = $_POST["sid"]
Now it works like a champ.
Any takers on why? I'd guess there is a setting in the php.ini that requires the quotes. Strangely enough, I have other variables called from the POST array that i'm not using quotes for and they're working fine...
Use Console in FireBug to inspect the POST request to see what is the sid value that is being sent.
If the sid value in request is ok, use var_dump($_POST["sid"]); to see the results on the server.
EDIT: it's considered good PHP style to use the quotes when accessing the associative array because quote-less keys are indistinguishable from constants:
define('myVar',3);
echo $array[myVar]; // retrieves $array[3], not $array['myVar'];
Try to echo the $sid instead of the <?=:
// Change that
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
// With that
<input name="sid" type="hidden" id="sid" value="<?php echo $sid; ?>">
also for the test time try to change the input type from hidden to text in order to be 100% sure the $sid contains a value.
Using quotes for associative array keys is mandatory, and while it may work without them, it's incorrect and erratic behavior is expected.
I had this same problem, trying to use $_POST[sid] as a variable. I'm am thinking that "sid" is a reserved or restricted variable name, because I changed my variable to $_POST[snid] and it worked just fine. This was my code
$sid = $_POST[sid];
$recipient = "($sid) ($_POST[sid])";
if ($_POST[sid] > 0)
{
$recipient = "It Worked";
}
print $recipient;
When I posted "&sid=15", the result was:
() (15)
Unbelievable. Impossible, right? All I did was change from using "sid" as the index to "snid", and it worked no problem.
So, don't ever use $_POST[sid].
i am trying to create a donate page. the hole page is php,
there is a textbox that hold the amount value which should be send via hidden input with the url to the the payment gateway. i have tryed many time but it is not working. i am still a beginner in this could any one please help me in fixing my code here
<div class="donate">
<?php
$amount = $_REQUEST['amount'];
$txtCurrency = 840;
$txtAmount = number_format($amount, 2, '.', '');
echo $amount;
$key = "TEST";
$txthttp = "http://test.com/you.php";
?>
<form action="payment.php" name="form1">
<input type="text" id="amount">
<input type="submit">
<input type="hidden" name="txtAmount" value="<?= $txtAmount; ?>">
<input type="hidden" name="txthttp" value="<?= $txthttp; ?>">
<input type="hidden" name="signature" value="<?= $key; ?>">
</form>
</div>
In general, you should avoid using $_REQUEST when you can use $_GET or $_POST. $_REQUEST allows variables to be set by either an HTTP GET or POST, which can pose a security risk, since your site will presumably use one or the other. With that said, here's what I would do:
Add method="post" to your form tag.
Access the input elements by looking at $_POST['txtAmount'], $_POST['txthttp'], etc.
In general, you can view all variables set in the POST by doing this:
var_dump($_POST);
You can access these values from payment.php with $_POST['txtAmount'], $_POST['txthttp'], $_POST['signature']. How you handle them will be up to your code. I see that you used the $_REQUEST array, which will work, however I believe it's better form to be specific and use the $_POST array.