Loop through php $_POST, and emailing values - php

I am building a confirmation page and a simple shopping cart script. But I am unable to send the values by mail using the "Send" button which I have put on the end of the page.
If I don't wrap the mailer part inside the "if" condition for the button, it works and sends the stuff on page load. But as I wrap it inside the "if" condition, I get only a blank email.
UPDATE:
Inside the previous page, wich has the "Ordering" form, I have switched from method="post" to method="get".
And in the second page,
I have changed the method inside the loop from $_POST to $_GET. As for the button kept the $_POST method together with the method="post" inside the form (on the bottom). Now it works, but I can't understand why.
First page with form:
<!DOCTYPE html>
<head>
<title>Alm Chalet Menu</title>
<link href="css/template.css" type="text/css" rel="stylesheet" />
</head>
<body>
<h2>Alm Chalet Menu</h2>
<p>Biologische Säfte </p>
<form method="get" action="order.php">
<p><input type="number" name="orange" min="0" value="0" class="count_style">
Orange</p>
<p><input type="number" name="multivitamine" min="0" value="0" class="count_style">Multivitamine</p>
<input type="submit" name="send" value="Send Menu" />
</form>
</body>
</html>
Second, confirmation page:
<!DOCTYPE html>
<head>
<link href="css/template.css" type="text/css" rel="stylesheet" />
<title>Ordered Food</title>
</head>
<body>
Your order the following:
<table id="order_table">
<tbody>
<?php
$items = '';
foreach($_POST as $key => $value){
if ($value == 0) {
continue;
}
echo "<tr><td>$key</td><td class='value'>$value</td></tr>";
$items .= "$key: $value\n";
}
if (isset($_GET['send'])) {
$message = $items;
mail("****#yahoo.com", $subject, $message, $headers);
echo "<p>Thanks for your order!</p>";
}
?>
</tbody>
</table>
<p>
<form method="get">
<input name="send" type="submit" value="Send Order">
</form>
</p>
</body>
</html>

I assume you are sending post data from some other page to this page? If so after you post to this page then submit another form using the get method, the $_POST variable will no longer have anything in it because you submitted another form to this page (via get). You need some way of saving the posted data (perhaps a php session) so that when you click the Send Order submit button the previously posted data isn't lost.

Related

Is it possible to place two forms and save two cookie values in PHP?

I'm wondering if it would be possible to place two forms on the same page
and saving their cookie values respectively after clicking submit buttons.
With code below, when I click submit button on the first form, its cookie value is saved successfully but when I click on the second form, second value is saved but the first value is overwritten.
<?php
setcookie('username[user111]', $_POST['user111'], time()+60);
setcookie('username[user222]', $_POST['user222'], time()+60);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<p>name:<?php echo $_POST['user111']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user111" value="TOM">
<input type="submit" value="close">
</form>
<p>name:<?php echo $_POST['user222']; ?></p>
<form method="POST" action="">
<input type="hidden" name="user222" value="BOB">
<input type="submit" value="close">
</form>
</body>
</html>
I'd like to save both
Name:username[user111] Value:BOB
Name:username[user222] Value:TOM
If I could save the values respectively, it'd not necessary to use form submit
but if possible I'd like to use PHP instead of JavaScript.
Any advice would be appreciated.

Form won't pass hidden value

I'm working on a database-driven quiz that lets users select a series of answers, then submit the results via a form. It was working great, when it suddenly blew up, and I haven't been able to find the problem.
So before I get into the more complex stuff, I'd like to go back to square one and just make something simple work - like passing a hidden value to another page that echoes that value.
Here's the code for my first page # mysite/form.php:
<html>
<head>
</head>
<body>
<!-- g1/form.php -->
<div id="quiz" rel="key">
<form action="form2.php" method="post" id="quiz">
<input type="hidden" name="PreviousURL" id="url" />
<input type="submit" value="Submit Quiz" />
</form>
</div><!-- quiz-container -->
</body>
</html>
And here's the code for the second page:
<html>
<head>
</head>
<body>
<?php ini_set('display_errors', 1);
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo $_POST['PreviousURL'];
}
echo 'XXX';
?>
</body>
</html>
I also tried moving the closing bracket, like this:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
}
echo $_POST['PreviousURL'];
echo 'XXX';
In both cases, when I click the submit button and am forwarded to form2.php, I see "XXX," but there's no value for $_POST['PreviousURL'].
I must have accidentally deleted or modified something, because it seems so simple, and it worked fine before. Can anyone tell me what the problem is?
there isn't a value for the hidden input.
In your form script you have missed out the value="" from the hidden input. This is the reason why nothing is displaying on the second page.

Linking html to php with $_POST

I have been trying to create a form that reads a post from an HTML form and displays an element from that post IF it detects that the post exists.
However, each time the post is submitted, it simply reloads the form as though no post were provided.
<!DOCTYPE html>
<html>
<head>
<title>Upload from Manifest</title>
</head>
<body>
<?php
if (isset($_POST['manifest'])) {
echo 'we are in the IF';
echo($_POST['manifest']);
}
?>
<h1>Submission from manifest into main db</h1>
<div class="container offset-top120">
<form method="post" action="https://nhsggc.cogiva.com/prism/loadFromManifest.php" enctype="multipart/form-data">
<input id="manifest" type="text" />
<input id="submit" value="Submit" type = "submit" />
</form>
</div>
</body>
</html>
Your form is going to either a different page (https://nhsggc.cogiva.com/prism/loadFromManifest.php so check for that first) if you wanted it to go to same page, you can give the action as just '#', or put in the whole URL like you have.
You're missing the name attribute from your submit input and text input. Read up on the name attribute!
<input id="manifest" type="text" name="manifest">
<input id="submit" value="Submit" type="submit" name='submit' />
Then your PHP should look like this:
<?php
if (isset($_POST['submit'])) {
echo 'Inside an if';
echo $_POST['manifest'];
}
Then it should work.

PHP session back button?

Ok. So I am using Sessions to store data because I am making a multi page form. The thing is, I need a back button with it. I have a submit button that will take the user to the next page and store the data into sessions but I need a back button next to the submit button in case they messed up for whatever reason. Is there anyway to make a back button with php that will take them back to the previous page while showing the data they entered? heres my code of one page.
Also, I have tried using the history.go but that only works for one page.
<?php
//let's start the session
session_start();
//now, let's register our session variables
$_SESSION['opinion1'] = 'opinion1';
$_SESSION['choice1'] = 'choice1';
//finally, let's store our posted values in the session variables
$_SESSION['opinion1'] = $_POST['opinion1'];
$_SESSION['choice1'] = $_POST['choice1'];
?>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href='http://fonts.googleapis.com/css?family=Playball' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="bootstrap.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" type="text/css" href="engine1/style.css" />
<script type="text/javascript" src="engine1/jquery.js"></script>
<script src="jquery.cycle2.js"></script>
<title>Untitled Document</title>
</head>
<body>
<div class="nav">
<div class="container">
<h1 class="pull-left">Securing the Future of Village Walk</h1>
<div class="pull-right">
<ul class="nav nav-pills">
<li class="active">home</li>
<li>about</li>
<li>contact</li>
</ul>
</div>
</div>
</div>
<div class="ammendment">
<div class="container">
<form method="post" action="page4_form.php">
<textarea id="opinion2" name="opinion2" value=""></textarea>
<label for="Yes">Yes</label>
<input type="radio" name="choice2" value="Yes">
<label for="No">No</label>
<input type="radio" name="choice2" value="No">
<label for="Neither">Neither</label>
<input type="radio" name="choice2" value="Neither">
**I NEED BACK BUTTON HERE**
<input type="submit" value="Next">
</form>
</div>
</div>
</body>
</html>
I would first organize session data in a way that it's easy for you to correlate data entered with the page it was entered.
One possibility could be to organize session data in an array:
$_SESSION['formData'] = array(
array(... data for step 1 ),
array(... data for step 2 ),
array(... data for step 3 ),
);
So $formData[0] would hold data entered in step 1, etc.
As for the button, another submit would be enough:
<input type="submit" value="Back" />
Then, you would have to determine which page you are going back to; which you can achieve by sending a hidden field with the current page number.
<form method="post" action="page_form.php">
<input type="hidden" name="page" value="X" />
One thing to note here is that the server side process would not longer be one-per-page; instead there would be only one page_form.php where you'll have to determine the action and the page to move to (although you could use one per page and set the right action in the , there's always several solutions to any problem):
<?php
...
$page = $_POST['page'];
if ( isset($_POST['Back']) ) // Going back
{
$page -= 1; // Previous page requested
// Retrieve data from session
$data = $_SESSION['formData'][$page-1]; // $page-1 because of zero based array indexes.
...
// Dynamically load the proper page processor.
// each pageX_form.php will know how to deal with the variable $data.
include "page{$page}_form.php"
}
else
{
$page += 1; // Next page requested
$data = $_POST; // In this case, the data is whatever was entered
}
When building the form for each page, you'd have to remember to add the hidden field with the page number:
<form method="post" action="page_form.php">
<input type="hidden" name="page" value="<?php echo $page; ?>" />
If you wanted to use one server-side process per page, then you could do something like this instead:
<form method="post" action="page<?php echo $page; ?>_form.php">
The server-side process would look different, since you would not have to ask for the page number; it would be implicit.
You could just add an additional form like so:
<form action="pageX.php" method="post>
<input name="submitBackButton" type="submit" value="Back">
</form>
This can be detected as a normal GET / POST field within PHP:
<?php
if (isset($_POST['submitBackButton']))
{
// Do required actions here
}

onclick input type submit

In a form I have an <iframe> that contains a PHP file (editor.php). This PHP file contains an HTML form.
Well, when I do a "submit form", I call the same PHP file, for example main.php.
When I press the submit button, I have "onclick method" that it calls a Javascript function inside editor.php. This function executes the form.
My problem is that main form is executed correctly but the second form is not.
In the second loop of the form of editor.php receives nothing.
**Check this way it will work as you expected**
//main.php
<html>
<head>
<script type="text/javascript">
function validateMain()
{
alert('main');
}
function validateSub()
{
alert('sub');
}
</script>
</head>
<body>
<form id="main" onsubmit="return validateMain();">
<input type="text" name="first" id="first"/>
<input type="submit" value="Submit Main"/>
</form>
<iframe name="ifr-form" id="ifr-form" src="test.html"></iframe>
</body>
</html>
//test.html the second form included through iframe
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div>
<form id="sub" onclick="return window.top.validateSub();">
<input type="text" name="first" id="second"/>
<input type="button" value="Submit Sub" />
</form>
</div>
</body>
</html>
you must check if php and iframe are compatible, as far as i Know I don't think that frames and php gives any output, hope this helps.

Categories