rhyspaterson Posted July 17, 2007 Share Posted July 17, 2007 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 17, 2007 Share Posted July 17, 2007 Javascript cannot work with PHP like that. I would suggest using AJAX. Actually, there is a way you can get what the user chose with javascript, you need to use getElementById(). Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 17, 2007 Share Posted July 17, 2007 variables of php cant be change if the page is not refresh or with ajax the only thing you can change is the value of the html element like the texbox with the js Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 17, 2007 Author Share Posted July 17, 2007 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? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted July 17, 2007 Share Posted July 17, 2007 I'm sorry, I think I read your post wrong...your trying to set a PHP variable via a javascript function...that isn't possible. You are going to have to use AJAX. Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 17, 2007 Author Share Posted July 17, 2007 Ahah, that's fine. At least i know what is wrong now The XmlHttpRequest Object is where i should be looking, yes? Quote Link to comment Share on other sites More sharing options...
lur Posted July 17, 2007 Share Posted July 17, 2007 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);"> Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 17, 2007 Author Share Posted July 17, 2007 Thanks for your help lur 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 Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 17, 2007 Author Share Posted July 17, 2007 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..? Quote Link to comment Share on other sites More sharing options...
lur Posted July 17, 2007 Share Posted July 17, 2007 <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> Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted July 17, 2007 Author Share Posted July 17, 2007 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.. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.