[web] Geting button name on PHP after Js submit

Started by
4 comments, last by Wan 16 years, 8 months ago
I'm using PHP and &#106avascript. I want to know if it is possible to, using &#106avascript, submit a form as if it where submitted by the user pressing a button himself. Problem is part of the PHP code is expecting the button's name on the $_POST variable, but, because the form is being submitted using the following &#106avascript, it doesn't show up on the $_POST variable: document.form_name.submit(); Is there anyway I can make the button's name appear on the $_POST PHP array variable using &#106avascript alone?
Advertisement
It's not the most elegant of solutions, but this should work: just before you submit the form, add an input field and clone the submit button's properties.
Like so:

function submitForm(){	var frm = document.forms[0];		for (var i = 0; i < frm.elements.length; i++)	{		if (frm.elements.type == "submit")		{			/* create replacement for submit button */			var dummy = document.createElement("input");						dummy.style.display = "none";						dummy.name = frm.elements.name;			dummy.value = frm.elements.value;						frm.appendChild(dummy);						break;		}	}		frm.submit();}
Thank you for your solution, I ended up using a similar hidden field solution.

R++! [wink]
Could you just have selected the submit button and called input.click()?

document.getElementById('#submitbutton').click(); // Voila!
I found another easier solution.

This is your Html code for your submit button:
Quote:<input name="bTest" type="submit" id="bTest" value="Test" onclick="return Validate();" />


So, once you click the "Test" submit button, the submittal will only occur if the Validate function returns true:
Quote:<script language="&#106avascript" type="text/&#106avascript"&gt;<br><br>function Validate( )<br>{<br> // Perform Validation here...<br><br> return true; // If all is well, return true!<br>};<br>&lt;/script&gt;<!--QUOTE--></td></tr></table></BLOCKQUOTE><!--/QUOTE--><!--ENDQUOTE--><br><br>The hidden field solution is also good because it allows you to pack other type of info apart of what submittal button was pressed, but for my particular problem, this is the easier solution.<br><br>Here it stays archived in case someone else stumbles upon the same issue.
The following variation is actually used a lot for client-side form validation:
Quote:<form onsubmit="return Validate();">
...
</form>

This will catch all submit events, including the one that's fired after the user hits the enter key in a text input field.

This topic is closed to new replies.

Advertisement