[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Weird submission issue (Javascript, PHP)

I'm having a very strange problem.

I have this code:
 
function submitformss() 
{ 
 setTimeout( function(){document.lead_passenger_form.submit();}, 500);
 document.extra1.submit(); 
 
 if (document.extra2){
	 
  document.extra2.submit();
 }
  
  if(document.extra3){
	document.extra3.submit();  
  }
  
 
 if(document.extra4) {
	document.extra4.submit(); 
 }
 
 if(document.extra5){
	document.extra5.submit(); 
 }
 
 if(document.extra6) {
	document.extra6.submit(); 
 }
 
 if(document.extra7) {
	document.extra7.submit(); 
 }
 
 if(document.extra8) {
	document.extra8.submit(); 
 }
 
 if(document.extra9) {
	document.extra9.submit(); 
 }
 
 if(document.extra10)
 {
	document.extra10.submit(); 
 }
 
 if(document.extra11)
 {
	document.extra11.submit(); 
 }
 
 if(document.extra12) 
 {
	document.extra12.submit(); 
 }
 
 if(document.extra13)
 {
	document.extra13.submit();
 }
 
 if(document.extra14)
 {
	document.extra14.submit(); 
 }
 
 if(document.extra15)
 {
	document.extra15.submit(); 
 }
 
 return false;
}

Open in new window


Now each one of those forms which may be submitted sends information to the server, which is then printed out. This works, ONLY, if say in Firefox I step through using the Firebug debugger, and step through each individual submit.

If I do not do this, and have no breakpoints, it does not work. The submissions fail and no data is uploaded.

Why on earth should this be the case? All firebug is doing is stopping the code at any point I see fit, it doesn't change how it runs. So why would this happen?
0
Lexx877
Asked:
Lexx877
  • 8
  • 8
  • 3
  • +1
1 Solution
 
Ray PaseurCommented:
Hmm... Is there anything else you might want to show us, or is that the entire script?
0
 
Lexx877Author Commented:
Well i'll give an example of form  'extra1':

 
<form name="extra1" method="POST" action="extrad.php" target="hiddenFrame"> 

<fieldset><legend>Extra passenger 1</legend>
<div><label for="title1">Title: </label><input name="title1" type="text" id="title1"></div>
<div><label for="firstname1">First Name: </label><input name="firstname1" type="text" id="firstname1"></div>
<div><label for="surname1">Surname/Family name: </label><input name="surname1" type="text" id="surname1"></div>
<div><label for="bags1">Bags: </label><input name="bags1" type="text" id="bags1"></div>
<div><input name="busi1" type="checkbox" id="busi1" value="Y"><label for="busi1">Business class </label></div></fieldset></form>

Open in new window


Now the PHP script that links to is:
 
<?php
session_start();
require_once('main_data.php'); //reads in all files needed to run functions such as those found in 




 $_SESSION['title1'] = $_POST['title1'];
 $_SESSION['firstname1']= $_POST['firstname1'];
 $_SESSION['surname1'] = $_POST['surname1'];
 $_SESSION['bags1'] = $_POST['bags1'];
 $_SESSION['busi1'] = $_POST['busi1'];


?>

Open in new window


Those values are then called out to be printed:  
echo $_SESSION['title1'];
echo  $_SESSION['firstname1'];
echo $_SESSION['surname1'];
echo $_SESSION['bags1'];
echo $_SESSION['busi1'];

Open in new window


Now the problem could be in that I have two return functions going on. You see I have a validation script for the forms:  
if (document.extra1) {

if ((document.extra1.title1.value ==="") || (document.extra1.firstname1.value==="") || (document.extra1.surname1.value==="") || (document.extra1.bags1.value==="") ||(document.extra1.bags1.value > 3) || (IsNumeric(document.extra1.title1.value)) || (IsNumeric(document.extra1.firstname1.value)) || (IsNumeric(document.extra1.surname1.value)) || (!IsNumeric(document.extra1.bags1.value)))
{
document.getElementById("error").innerHTML="You need to fill in all details for Extra Passenger 1. Ensure text and numerals have been used as appropiate. You can have a maximum of three bags per passenger.";
valid = false;
}

}


if (valid == true) 
{ 
	submitformss(); 
}
document.getElementById('my_fees').value = fees;

 return valid;

}

Open in new window


That's just a snippet of that function for this example, but you see it called submitformss(), which holds a return statement, and then has a return statement on its own. No idea if that would cause an issue.

Like I say, all this works if I step through with Firebug, it falls over if I don't.

0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:

Welll can you please try changing 500 to 5000 or more and moving line below to the line after last extra form's submit.

setTimeout( function(){document.lead_passenger_form.submit();}, 500);

BTW : As i'd mentioned in one of your related questions, probably this is not the elegant way of doing the thing you wish to achieve. Can you explain us why do you need so much forms. What are they for ?  So that we can maybe suggest you a more practical and sensible method.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Lexx877Author Commented:
They are because you can choose to have up to 16 people, the user picks 1-16, and depending on the number depends how many 'extra' forms are made available. It does not make sense to append them to another form.

I will try your method and get back, it is so odd that it works perfectly under 'break' conditions but not otherwise.
0
 
Lexx877Author Commented:
Same deal, works if I debug and step through, fails if not.
0
 
crazedsanityCommented:
Is it even possible to submit multiple forms on the same page?  It seems to me that only one form would submit; if there were 16 submit buttons on a page, a user has the opportunity to only click on one, don't they?

Either way, I'd suggest doing something a bit more elegant.  Consider using jQuery and post the data via AJAX; once done, you can send the user to a different page.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
form's with target element can post forms to a page in another window. There may be a problem with losing focus maybe.
0
 
Lexx877Author Commented:
It is possible, as they cannot submit each one individually. One submit submits them all, and the problem is that it works!

It really works perfectly as long as I 'step through' using a debugger such as Firebug, if I remove all breakpoints however it doesn't work. Which makes no sense.
0
 
crazedsanityCommented:
I can't seem to find anything that indicates whether or not multiple forms can be submitted or not, but my gut tells me it was never intended to work.  I look at it like this: if a user comes across a page with 6 different submit buttons, they can only click one of them; trying to submit the others would involve going back to that page several times.

That said, I would suggest using jQuery .ajaxForm().  Check out [ http://docs.jquery.com/Plugins:Forms ].  
0
 
Lexx877Author Commented:
You didn't read what I said, there are not 6 submit buttons. There is one submit button, which submits ALL forms in full go.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
I look at it like this: if a user comes across a page with 6 different submit buttons, they can only click one of them; trying to submit the others would involve going back to that page several times
No my friend not indeed. Each form that has a target="_blank" as a property open it's own window to show the result page to the visitor. Main page which contains the forms remain opened in the background.

Although this sound weird to me too. But if Lexx877 wants this can be achieved. The main problem here is does visitor like to face with so many new windows with submit results. So i agree using AJAX is a better way for this.

And i'm sure the results like this usage can be achieved by combining all extra forms within one form tag. Just number their elements and process them in the result page according to this.

i can show you an example in my mind quickly -syntax not tested-

 
<form ................. >
<?php 
$number_of_users=16;

for($i=0 ; $i<$number_of_users ;$i++ ){
$user_number=$i+1;
?>
<fieldset><legend>Extra passenger <?=$user_number?></legend>
<div>
<label for="title_<?=$user_number?>">Title: </label>
<input name=title[] type="text" id="title_<?=$user_number?>"></div>
<div>
<label for="firstname_<?=$user_number?>">First Name: </label>
<input name="firstname[]" type="text" id="firstname_<?=$user_number?>">
</div>
<div>
<label for="surname_<?=$user_number?>">Surname/Family name: </label>
<input name="surname[]" type="text" id="surname_<?=$user_number?>">
</div>
<div>
<label for="bags_<?=$user_number?>">Bags: </label>
<input name="bags[]" type="text" id="bags_<?=$user_number?>">
</div>
<div>
<label for="busi_<?=$user_number?>">Business class </label>
<input name="busi[]" type="checkbox" id="busi_<?=$user_number?>" value="Y">
</div>
</fieldset>
<?php } ?>
</form>

Open in new window


code above is for instance of all extra forms and a sample return page process code may be like this ;

 
<?php
//i assume you use POST  

if(isset($_POST["title"])){$titles = $_POST["title"]}

echo $title[0];
// prints the user 1's title
// you can use $title[0] like  $_POST["title"] as if in your code.
?>

Open in new window


I advice you to implement this usage even if other does not give any errors. And if you have any problems upon this just drop a line.
0
 
crazedsanityCommented:
I did read what you said, though I may not have explained it properly.  Javascript might be able to programatically submit all the forms, but the end result is basically the same: only one form can actually be submitted at a time.  I've seen some code that attempts to submit multiple forms with a single button, but I have yet to see one that actually works.

I have no doubt that you have managed to submit all the forms in one go with one button using Firebug and some breakpoints.  As you have seen, however, it doesn't work without that bit of magic that Firebug provides.

I simply don't think submitting multiple forms through a single button will work in practice.  I would suggest reconfiguring the page, if possible, to use a single form; if that is not possible, I would again suggest using AJAX to get the intended result.

I apologize if I sound like I'm repeating myself, or if you feel like I'm not listening.  I'll refrain from suggesting on this question again until/unless you request it.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
if you insist using multiple forms please supply the pages you use. That must not be so complicated to submit forms in order with JS/Jquery i guess  
0
 
Lexx877Author Commented:
I have one more day to do this which is why I don't have time for another method :)

In response to erdincqc, I actually used the hidden frame method which means that many pages are not opened to the user, they see nothing of the processing at all.

As i've stated this works! The method does work! Yet, for whatever reason, only if I step through the code before it is submitted, which really makes no sense, as it's the same code.

I will attempt your method though!
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Then just attach the full pages after clearing any personal info .  Code parts will just make things slower.
0
 
Lexx877Author Commented:
So your code does create the forms, which is great :)

Yet it doesn't give the correct values, for example:

label for="title_<?=$user_number?>">Title: </label>

is the label when expecting the HTML code. I'm not sure what is wrong with what you have done however.
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
i havent changed your code structure just assigned them with the relevant input's id
as you can see at w3schools label's targets are ids . so label and input ids are same in the html output.

http://www.w3schools.com/tags/att_label_for.asp


But as i'd mentioned i havent tested code just wrote on the fly to code interface. What was the error ?
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
if <?=$user_number?> does not place a number in your HTML output your php configuration may not be allowing short codes then just change all

<?=$user_number?>
to
<?php echo $user_number; ?>
0
 
Lexx877Author Commented:
Yeah that's the error, just no number in the HTML. Okay i'll give that a shot!

I really appreciate the time you have spent helping with my odd requests :)
0
 
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
np my friend , we all learn everyday even while helping to eachother :)

if visitor does not see anything just pass all from just single form to the processor page ..
if you have any problems with the return page process i'll be round for a while
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 8
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now