tags:

views:

107

answers:

2

This is beyond both making sense and my control. That being said here is my dilema. I need to have two separate forms, each using a different PHP script that I cannot modify to process them. I need to have both of these forms share the same submit button. Here is a simplified version of the html. Form 1

<div id="formOne>
  <form name="returning" method="post" action="PHPscript1.php">
    <label>Are you a returning user?</label>
    <input type="text" name="retUser" id="retUser" />
  </form>
</div>

<div id="formTwo">
 <form name="newUser" method="post" action="PHPscript2.php">
  <label>Are you a new user, register here</label>
  <label>First Name</label>
  <input type="text" name="fname" id="fname" />
  <label>Last Name</label>
  <input type="text" name="lname" id="lname" />
  <label>Email</label>
  <input type="text" name="email1" id="email1" />
 </form>
</div>

<div id="submitContain">
 <input type="submit" id="sbtBtn" value="submmit" />
</div>

I think I could possibly switch the submit script by checking the input's value?

$("#sbtBtn").click(function() {
 if($("#retUser").val().is('') {
   do something;
 } else if .....
});

I will also need simple validation on these and know Ill have to do all this via jQuery's AJAX function. Any advice is much appreciated. I don't sign the checks at work, so this is how it is.

thanks in advance!

+1  A: 

Not ideal, but you can call the .submit() for the appropriate form based on your if check, like this:

$("#sbtBtn").click(function() {
  if($("#retUser").val()) {      //check if #retUser has a value
    $("#formOne form").submit();
  } else {
    $("#formTwo form").submit();
  }
});
Nick Craver
+1  A: 

Yuck, there must be quite a story behind having to do this.

Yes, what you have would work. You would want the "do something" line to be replaced with $('#formOne').submit();, and the other with formTwo.

Something like:

$("#sbtBtn").click(function() {
 if($("#retUser").val()) {
   $('#formOne').submit();
 } elseif ($("#email1").val()) {
  $('#formTwo').submit();
 } else {
  // Don't do anything.
 }
});

jQuery validation here: http://docs.jquery.com/Plugins/validation#Validate_forms_like_you.27ve_never_been_validating_before.21

Eli
@Eli and Nick Craver, thanks for clearing that up. What is the difference btwn ('#formOne fomr").submit(); and ('#formOne').submit? Yeah they are software developers who don't understand the web. At All. So to do this with AJAX, would I include this in the success handler? ultimately when either form validates and is submitted I need to hide them and show the appropriate content, either another form to enter a key (returning user) or a table with download options - new user
Dirty Bird Design
@Dirty - The `<div>` doesn't have a `submit` event so this doesn't work :) @Eli - double check your syntax and selectors ;)
Nick Craver
@Nick - not sure i follow, is this not going to work? <div id="submitContain"> <input type="submit" id="sbtBtn" value="submmit" /></div>how would you do it?
Dirty Bird Design
@Dirty - I was saying specifically this answer doesn't work, you can test my answer with your markup here: http://jsfiddle.net/nick_craver/gtPPp/
Nick Craver
@Nick - as always, thanks. Ill give both a try, i feel like their will be some issues applying validation and AJAX functionality, but this is a good start. thanks man.
Dirty Bird Design
@Nick, was messing with your code, I need to add a parameter such as if $("#retuser") so that if all are empty it doesn't submit form 1 or 2...Assuming you added the same ID as the input name for each, respectively
Dirty Bird Design
@Dirty - That's the right approach, but you need a `.val()` on the end of each of those.
Nick Craver
@Nick - why just .val() does that mean empty? I assumed an empty value was .val('') but I fully expect to be wrong. Could you please clarify for me.
Dirty Bird Design
@Dirty - `.val()` (no params) *gets* the value, that's what you want to check right? That the value isn't empty? (~=false)?
Nick Craver
@Nick - yes, i was thinking ill-logically. I was checking to see if it's value was empty. I believe I'm on the right track but have some syntax errors http://jsfiddle.net/vDFjz/
Dirty Bird Design
@Dirty - mine was missing some parens. Fixed now, but still just an example, I haven't run it.
Eli
@Dirty - Also, updated to do nothing if neither email field has a value.
Eli
@Dirty - I think this is what you're after: http://jsfiddle.net/nick_craver/P3bYb/ @Eli - `<div>` elements don't have a `submit` event :)
Nick Craver
@Eli - So if #retUser has any value, it submits #formOne's action. If it doesn't and #fname, #lname, or #email has a value, it submits #formTwo's action. If none have any value it does nothing?
Dirty Bird Design
@Nick, awesome. one final tweak, if no inputs have a value and its submitted alert"foo". Would you create a var with all 4 elements ("fubar") and say if fubar.val() return false? or just enter them each in the parameter?
Dirty Bird Design
@Dirty - I have completely lost what you're trying to do...you've wandered into a completely different question at this point, and it's better off being one I think.
Nick Craver
@Nick, sorry i guess its more validation. if "#retUser" has a value, submit with formOne action. If all of #fname, #lname, #email1 have value submit with formTwo action. If no element has a value, and you submit, run an alert
Dirty Bird Design
@Dirty - I would ask a new question about what you want...there are a half dozen in the comments now, kind of diluting the usefulness of this question as a google resource later :)
Nick Craver
@Nick, will do, sorry.
Dirty Bird Design
@ Dirty - Yes, but I wouldn't bother with checking the names. I assume if the email isn't present, then it would be an invalid submission, so just if the one email is present, it submits the first form, and if the other, then the second.
Eli