Jump to content

Passing PHP varialbles in forms with javascript actions


rhyspaterson

Recommended Posts

Hey guys,

 

I have a form that i submit and process with PHP. I just added a function that could disable all the form fields (or as the user sees it; "turn off" the settings in the form) with JavaScript (so the user could see the change instantly without having to submit the form first). Obviously i still need to submit the form so i can process the fields and find out if the user wanted them to be disabled. The fields are disabled with a form button that calls the following JavaScript function:

 

function defaultFileTypesChangeStateBlockGlobal(){
	for (i=0; i<330; i++){
		document.defaultFileTypesForm[i].disabled = true;
	}
}

 

I need to pass a variable to the PHP form processor to find out if the fields were set to disable (if the button was pressed), so i tried adding some PHP into my JavaScript statement (located inside the <head>) and passing it as a URL variable like so:

 

function defaultFileTypesChangeStateBlockGlobal(){
	for (i=0; i<330; i++){
		document.defaultFileTypesForm[i].disabled = true;
	}
<?PHP $globalState = 1; ?>
}

...

echo "<form name=\"defaultFileTypesForm\" action=\"/menu/default/form/process.php?globalState=$globalState\" method=\"post\">";

 

However the variable does not get passed. Any suggestions..?

 

Thanks heaps :D

Link to comment
Share on other sites

Thanks for your reply.

 

I have used the getElementById() command before, however i'm not sure where i would apply it here.. I believe you are telling me to have a PHP variable equal to the output of the getElementById() command? I'm not quite sure where i should be doing this? This would still not solve the dynamic aspect of this problem i don't think..?

 

Looks like AJAX is the only way to get this happening. Could anyone suggest a piece of code to help with this form issue?

Link to comment
Share on other sites

Set a Javascript variable and intercept the POST.

 

var globalState;

function defaultFileTypesChangeStateBlockGlobal(){
for (i=0; i<330; i++){
	document.defaultFileTypesForm[i].disabled = true;
}

//<?PHP $globalState = 1; ?>

globalState = 1;
}

function onSubmitDefaultFileTypes(form) {
form.action = "process.php?globalState=" + globalState;
form.submit();
}

....

<form action="#" onsubmit="onSubmitDefaultFileTypes(this);">

Link to comment
Share on other sites

Thanks for your help lur :D However i am still having some issues;

 

function onSubmitDefaultFileTypes(form) {
form.action = "/menu/default/form/post.php?globalState=" + globalState;
form.submit();
}

 

Works fine up to the point of submission, as it is submitting to the right file. But it does not pass the "globalState=" + globalState;" variables, and no matter what i do i cant seem to get them passed. I dont even see a 'globalState=' in the URL once submitted.

 

Any suggestions?

Ta :D

Link to comment
Share on other sites

Ah.. i need to pass the variable to the function of course, haha.

 

edit: scrap that.

 

function onSubmitDefaultFileTypes(form) {
var globalState;
globalState = 1;
form.action = "/menu/default/form/post.php?globalState=" + globalState;
form.submit();
}

 

Even that does not pass the globalState variable..?

Link to comment
Share on other sites

<script>
var globalState = 0; // variable in global scope.

function setGlobalState() {
	globalState = 1;
	alert("globalState = " + globalState);
}

function onSubmitDefaultFileTypes(form) {
	form.action = "file.php?globalstate=" + globalState;
	alert(form.action);
}
</script>
<form action="#" onsubmit="onSubmitDefaultFileTypes(this);">
<input type="button" onclick="setGlobalState();" />
<input type="submit" value="Submit" />
</form>

Link to comment
Share on other sites

Thanks again for the reply.

 

The alert shows me the correct (and updated) form action, however when its submitted it doesn't pass the new data.

 

I.e, hitting submit gives me the correct alert of '/menu/default/form/post.php?globalState=0', but when it submits itself the next second it doesn't appear to use the said action..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.