Jump to content


Photo

Automatic form submission after sometime

form php javascript

Best Answer jazzman1, 04 April 2014 - 08:55 PM

Because js submit() method works like <button type="submit"> Click Me</button>, so the button element isn't as input elements, when the form is being submitted it's omitted from the form. Just create an input hidden element and check its value in the server side. 

Go to the full post


  • Please log in to reply
10 replies to this topic

#1 Jaswinder

Jaswinder

    Advanced Member

  • Members
  • PipPipPip
  • 127 posts

Posted 02 April 2014 - 01:42 AM

Hi

I want to submit the form automatically after sometime,. I have written the code , but its not redirecting/submitting. Don't know why.. Have a look

Thanks

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
setTimeout(function(){document.getElementById("exam").submit()},2000);
</script>
</head>
<body>
 <?php 
date_default_timezone_set('Asia/Kolkata');
echo"Start Time: ".$starttime=date('H:i:s')."<br/><br/>"; 
 $startstamp=time();
?>

<form action="inde x1.php" method="post" id="exam" name="exam">

	Q1 Your Gender<br/>
	<input type="radio" value="male" name="gender"> Male

	<input type="radio" value="female" name="gender"> Female
	<br /><br /><br />
	Q2 You Married<br/>
	<input type="radio" value="yes" name="marriage"> Yes

	<input type="radio" value="female" name="marriag e"> No
	<br /><br /><br />

	Q3 2+2<br/>
	<input type="radio" value="4" name="add"> 4

	<input type="radio" value="22" name="add"> 22
	
	<input type="hidden" value="<?php echo $starttime; ?>" name="starttime"><br/>
	<input type="hidden" value="<?php echo $startstamp; ?>" name="startstamp"><br/>
	
	<input type="submit" name="submit">
</form>


</body>
</ html>
 


#2 gristoi

gristoi

    Advanced Member

  • Members
  • PipPipPip
  • 824 posts

Posted 02 April 2014 - 07:35 AM

action="inde x1.php"

 

to

action="index1.php"


To err is human... to really foul up requires the root password

#3 Jaswinder

Jaswinder

    Advanced Member

  • Members
  • PipPipPip
  • 127 posts

Posted 02 April 2014 - 09:14 AM

Oh sorry.. thats a copy/paste error..

 

Its not working  yet.. Any other help



#4 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,450 posts
  • LocationMississauga, Canada

Posted 03 April 2014 - 03:40 PM

Change name="submit" to something else, for instance - <input type="submit" name="send" />



#5 Jaswinder

Jaswinder

    Advanced Member

  • Members
  • PipPipPip
  • 127 posts

Posted 04 April 2014 - 12:22 PM

Thanks jazzman1 , that works.

but i can't get the logic.



#6 Jaswinder

Jaswinder

    Advanced Member

  • Members
  • PipPipPip
  • 127 posts

Posted 04 April 2014 - 01:44 PM

There is a problem.. No value is displaying on index1.php

 

Here is the code for index1.php

<?php

if(isset($_POST['submit']))
{
	extract($_POST);
	
	echo "Start time: ".$starttime."<br/>";
	echo "End time: ".$endtime=date('H:i:s')."<br/>";

	$endstamp=time();
	
	echo "Time-taken : ".$timetaken=($endstamp-$startstamp)/60;
}

?>


#7 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,450 posts
  • LocationMississauga, Canada

Posted 04 April 2014 - 06:58 PM

Thanks jazzman1 , that works.

but i can't get the logic.

 

The submit method submits the specified form as if a submit element (button) was pressed. But the problem is that you also have in the form  a submit object element whose property name is using the same name (submit). So, when you set a property name to submit (it doesn't matter which type of the form), you override the submit() function on the form.

 

@To the 2nd question, most likely you need to change if(isset($_POST['submit'])) to if(isset($_POST['send'])), where $_POST('send') is the name of the submit button.


Edited by jazzman1, 04 April 2014 - 07:02 PM.


#8 Jaswinder

Jaswinder

    Advanced Member

  • Members
  • PipPipPip
  • 127 posts

Posted 04 April 2014 - 08:11 PM

Thanks for reply

That was a silly mistake on my part

Now, I changed it to

if(isset($_POST['send']))

Still blank page, but i tried this

if(!isset($_POST['send']))

Now it showing , so the question is, why script  is not setting it ?



#9 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,450 posts
  • LocationMississauga, Canada

Posted 04 April 2014 - 08:55 PM   Best Answer

Because js submit() method works like <button type="submit"> Click Me</button>, so the button element isn't as input elements, when the form is being submitted it's omitted from the form. Just create an input hidden element and check its value in the server side. 



#10 jazzman1

jazzman1

    Advanced Member

  • Gurus
  • 2,450 posts
  • LocationMississauga, Canada

Posted 04 April 2014 - 09:30 PM

you can make this simple test to see what I'm talking about,

<form name="f1" action="display.php" method="post">  
        <input type="text" name="test_input" value="some input text" />
        <input type="submit" name="test_submit" value="query" />
        <button type="submit">Click Me!</button>
</form>

Result:

 

Array ( [test_input] => some input text )


Edited by jazzman1, 04 April 2014 - 09:51 PM.


#11 Jaswinder

Jaswinder

    Advanced Member

  • Members
  • PipPipPip
  • 127 posts

Posted 06 April 2014 - 09:55 PM

Thanks , that hidden trick works :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com