DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Submit Form Programmatically From PHP Using JavaScript

02.19.2008
| 18120 views |
  • submit to reddit
        It´s something quite easy but may help a newby (as I´m now) in the future. 
This is a simple function in PHP to insert a JavaScript sentence which will do the same action as if the user would be pressing the Submit Button.
It can be used to use any control as a Submit Button or in case you make a form you need to send without interaction from the user.
the $FormName must be the name specified in the Form tag (i.e.: <form action="MyActionURL" method="post" enctype="application/x-www-form-urlencoded" name="MyFormName">), if you didn´t specified any, you must now :)
Lastly, just a tip regarding a trouble I had and may be you case too... if you have a form with all hidden fields, it won´t work, you have to have at least something echoed inside the form, just a letter can help, anyway, as it will redirect to the action URL, it should not be a problem as the form will disappear.

function SubmitForm($FormName)
//JavaScript function to submit a form programmatically
//$FormName must be form´s name (as specified in the opening form tag)
{
	echo '<script language="javascript">document.'.$FormName.'.submit()</script>';
}