admin管理员组

文章数量:1389758

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

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

Share asked Jan 31, 2012 at 15:39 GiriGiri 4,90911 gold badges41 silver badges48 bronze badges 3
  • 2 By "trigger" do you mean show/display or submit? – Stefan Commented Jan 31, 2012 at 15:40
  • Do you want to submit both forms, or the second one instead of first? – lorenzo-s Commented Jan 31, 2012 at 15:40
  • @lorenzo-s submit both forms if checkbox checked – Giri Commented Jan 31, 2012 at 16:03
Add a ment  | 

4 Answers 4

Reset to default 6

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:

<form>
    <input type="text" name="firstname">
    <input type="text" name="email">
    <input type="checkbox" name="newsletter" value="1">
    <div id="newsletter" style="display:none;">
         <input type="text" name="news-firstname">
         <input type="text" name="news-email">
    </div>
    <input type="submit" name="submit">
</form>

Javascript (jQuery):

// Set the onchange event for the checkbox
$('input[name="newsletter"]').change(function(){
    // Test is checkbox is checked
    if($(this).is(':checked')){
        // If it is checked then show the newsletter
        $('#newsletter').show();
    }else{
        // If it is not checked then hide the news letter
        $('#newsletter').hide();
    }
})

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 ments 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 bining 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 plex 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.

Use the Jquery library.. something like this.. I just made all the code for you, but you need to add styling and some AJAX stuff for submission, and it works:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.6/jquery.min.js"></script>
<script type="text/javascript">

//this is for Jquery 1.6+
$(function() {
    $("#newscheck").change(function() {
       if ($("#newscheck").prop("checked", true))
           $("#newsletterForm").show("slow");
     }); //End of $("#newscheck").change
}); //End of $(function() {

</script>
</head>

<body>
<form action="formprocess1.php" id="form1">
<input type="text" name="firstname">
<input type="text" name="email">
<input type="checkbox" id="newscheck" name="newsletter" value="0">
<input type="submit" name="submit">
</form>

<form action="newsletter.php" id="newsletterForm" style="display:none;">
<input type="text" name="firstnameNews">
<input type="text" name="emailNews">
<input type="submitNews" name="submitNews">
</form>
</body>
</html>

You could use AJAX (Asynchronous Javascript And XML) for this. If you use the jQuery library, here are some examples of how you could set this up: http://api.jquery./jQuery.post/

use only one form and use jquery to acplish the goal of having different scripts to handle the submit event

<form id="form">
<input type="text" name="firstname">
<input type="text" name="email">
<input id="ckType" type="checkbox" name="newsletter" value="1">
<input type="submit" name="submit">
</form>

javascript

$("#form").submit({
   var urlstr;
   if($("#ckType").attr("checked")){
      urlstr = "newsletter.php";
   }else{
      urlstr = "formprocess1.php";
   }
   $.ajax({
      url:urlstr,
      ...
   });
});

本文标签: phpHow to trigger another form while submitting a formStack Overflow