multiple form submits looses data!!! Please Help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fnyahoo
Forum Newbie
Posts: 2
Joined: Mon Nov 21, 2011 5:41 pm

multiple form submits looses data!!! Please Help

Post by fnyahoo »

ok here is my problem, I am kind of new to Php but have done programming before. I am recently learning php so am not sure what php can or cant do.
What i am trying to do here is i have a multiple submit buttons which i input some data and press submit, it works finr but when i enter more data and press another submit button the previous data is lost. What is the solution to that?

The following code is just written as an example.

Code: Select all

<html> 
<head> 
<title>New Tip Calculator</title> 
</head> 
<body> 

<center>



<?php 

echo "<form name='mailinglist' method='post'>
<input type='text' name='email' />
<input type='submit' name='mailing-submit' value='Join Our Mailing List' />
</form>
 
<form name='contactus' method='post'>
<input type='text' name='email' />
<input type='text' name='subject' />
<input type='submit' name='contact-submit' value='Send Email' />
</form>";


if (!empty($_POST['mailing-submit'])) {
$var = $_REQUEST['email'];
	echo"$var";
}
 
if (!empty($_POST['contact-submit'])) {
$email = $_REQUEST['email'];
$subject = $_REQUEST['subject'];

echo"$email $subject";
echo"$var";

}


?>

</center>

</body>
</html>
Last edited by Benjamin on Mon Nov 21, 2011 8:09 pm, edited 1 time in total.
Reason: Added [syntax=php||htm||css||javascript||sql||etc] - Please use [syntax] tags when posting code in the forums! Thanks.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple form submits looses data!!! Please Help

Post by Celauran »

With the user's email address being the common field between the two forms, you could write it to session data when one form is submitted and have both forms check for said session data when the page loads. There are surely a million other ways to accomplish the same, this is just the first that sprang to mind.
fnyahoo
Forum Newbie
Posts: 2
Joined: Mon Nov 21, 2011 5:41 pm

Re: multiple form submits looses data!!! Please Help

Post by fnyahoo »

ok This was an example code, what I am trying to do is a bigger task with more values. But the purpose is almost the same. Can I still use the session as the solution to my problem???

what i am doing is following,

1- (as starting point) i have a seperate html file called tipCalculator.html
it has a form and calls tipCalculator.php page where all the variable from the html being sent.

2- I have tipCalculator.php page which gets the data (by data i mean 6 variables) from the tipCalculator.html
and creates a table and does some calculations and puts them in a table. ALSO in another function, WITHIN the same .php page
I have another form which user inputs 3 more variables and presses the submit button again.

As soon as the user presses the submit button for those 3 more variables in the .php page all the data coming from .html becomes null (or 0). I need that data coming from previous .html page because that data will be used again.

(Please correct me if I am wrong but I think the reason why all the variables coming from the .html becomes null when press submit in .php file is because A Form in .php file calls the same .php file (itself). what i mean is <form action=" " ...> OR <form action="tipCalculator.php") in tipCalculator.php itself.

If it will help I can post the actual code here. That is something that I am writing for my own practice but the program will help about 25 employees to calculate their tip numbers much faster. It has been a good php practice for me, but now i need help. thank you in advance. i can post the actual code let me know.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: multiple form submits looses data!!! Please Help

Post by Celauran »

fnyahoo wrote:Please correct me if I am wrong but I think the reason why all the variables coming from the .html becomes null when press submit in .php file is because A Form in .php file calls the same .php file (itself). what i mean is <form action=" " ...> OR <form action="tipCalculator.php") in tipCalculator.php itself.
You're wrong. This is quite common practice and works fine. I'd guess it has something to do with how you're populating your second form. Please post the relevant code.
Post Reply