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.

<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>

Recommended Answers

All 6 Replies

You are not loosing anything actually, at a time you can handle only one form request.

If you submit form1 then only email and mail-submitting will be sent to action page
If you submit form2 then only email,subject and contact-submitting will be sent to action page

Is there a way around it? what i am trying to do is a bigger task actually, the first form is submitted from .html,

the second form is submitted from .php file where the variables from the .html gets null.

i can provide the actual code of the actual program that i am writing. i am writing this as a practice but if it works in a way how i want it to work it will help 25 employees to do their calculations faster (and get them lazier :)

Ok in the smaller version you explain little bit more what are you trying to acheive?

you can have 1 form with multiple submit buttons

you can send on all the post vars again

foreach($_POST as $k=>$v){
$allowedchars = array('@','-','.','_');
$tk = str_replace($allowedchars,'',$k);
$tv = str_replace($allowedchars,'',$v);
if(ctype_alnum($tk) && ctype_alnum($tv)){
echo "<input type='hidden' name='{$k}' value='{$v}'/>\r\n";
}
}

add to the allowed chars if you want to let more chars through, or just validate it some other way - it just can't contain quotes really. also watch for ones with the same name

I think you can only do this, if you're gonna post your form data using javascript (AJAX).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.