I have a form like this
<form action="formprocess1.php">
<input type="text" name="firstname">
<input type="text" name="email">
<input type="checkbox" name="newsletter" value="1">
<input type="submit" name="submit">
</form>
If the checkbox is checked on first form i want to trigger another form
<form action="newsletter.php">
<input type="text" name="firstname">
<input type="text" name="email">
<input type="submit" name="submit">
</form>
Can anyone give me some php code or javascript code to auto trigger it?
Thanks
You cannot trigger two forms at once as you can only send one request to a PHP page at once, unless you are using AJAX.
The solution here would be to hide the second form unless the checkbox is attached, and show it as part of the first form when the checkbox is checked.
Would you like me to demonstrate how you can do this?
Update
Showing/hiding the newsletter form on change of the checkbox state:
HTML:
Javascript (jQuery):
I have not tested the above but it should work, if there are any bugs, let me know and I will fix them. I have clearly set out comments to explain what each step is doing.
In your PHP script you must then test to see if the checkbox is checked, and then validate accordingly. Otherwise you will always receive the newsletter information. Just because they are hidden does not mean they are not submitted.
I would advise combining both of your separate PHP scripts into one file, this will make it much simpler for you.
The alternative to this is AJAX, which is a little more complex but it is good practice as more and more people are using it nowadays. If you would like to see an example of this then let me know.