Skip to main content
gamedev.net
🔒 Locked 🤖 Godot

[web] Geting button name on PHP after Js submit

Started by Prozak Aug 8, 2007 at 10:40 AM 4 replies 900+ views
Original Post
Prozak
Prozak
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?
Wan
Wan
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();}
Pipo DeClown
Pipo DeClown
Could you just have selected the submit button and called input.click()?

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

This is your Html code for your submit button:
Quote:


So, once you click the "Test" submit button, the submittal will only occur if the Validate function returns true:
Quote:
Wan
Wan
The following variation is actually used a lot for client-side form validation:
Quote:

...

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.