Flames Posted January 18, 2009 Share Posted January 18, 2009 <script type="text/javascript"> function checkUser() { var userID; userID = document.send_form.userid.value; var question; <?php $usercheck = get_table("<script type=\"text/javascript\">userID</script>"); //This Line if($usercheck['userName'] == $userrow['userName']) { ?> alert('The entered User ID corresponds to your own ID. \n Please change the user ID to attempt to send the items again.'); document.send_form.submit.disabled=true; <?php } elseif($usercheck == "") { ?> alert('The entered User ID does not correspond to any user. \n Please change the user ID to attempt to send the items again.'); document.send_form.submit.disabled=true; <?php } else { ?> question = confirm("The user ID entered currently corresponds to user "<?=$usercheck['userName']?>" \n Are you sure this is the user you want to send the items to. \n Copper - "document.my_form.copper.value" \n Banked Copper - "document.my_form.bank.value" \n Untrained Soldier - "document.my_form.soldiers.value" \n Attack Turns - "document.my_form.atturns.value" ); if(question==true) { document.my_form.submit=true; } <?php } ?> } </script> well before i get too much into it that is the code, a single JS function called when one tries to submit a form. I believe that the problem is with the line commented saying this line, the idea of that line is to call a PHP function using the JS variable, the function does work so that is not the problem. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted January 18, 2009 Share Posted January 18, 2009 That function will evaluate to: get_table("<script type=\"text/javascript\">userID</script>"); with "<script type=\"text/javascript\">userID</script>" being the argument. PHP is processed on the server before it gets sent to your client. Javascript gets processed when they client loads. The javascript isn't interpreted yet so you are not getting the value of whatever "userID" is, but rather all that that you put in quotes. Quote Link to comment Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 without the quote marks it doesnt work either though Quote Link to comment Share on other sites More sharing options...
xtopolis Posted January 18, 2009 Share Posted January 18, 2009 It won't work like that. the javascript is only text as far as PHP is concerned. You need to pass it to PHP in a $_GET or $_POST variable afaik... you could use ajax to do this after the page loads, but I think rather you should find a different way to do what you want. You could use Ajax in your javascript to check the userid and run your php function.. then based on the result show the alert you want. Quote Link to comment Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 Any easy way of doing this? edit: by easy way i mean something that wont require a lot of changes to the code already there. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted January 18, 2009 Share Posted January 18, 2009 From what I can tell from your code, you want to do two things with that function: a) Ask the server if the ID exists b) Ask the server if the ID is not the current user's ID For b), you could hard code the current user's id into a hidden form value (<input type="hidden" value="users id here" />) and have javascript check that your field doesn't equal that field when submitting. As for a), there's not really a way around it.. you can either make a regular PHP form that returns an error if the userid does not exist, or use ajax to check when they enter it and not allow them to send the form until they correct it. Either way, you'll have to query the server eventually to find out the answer. The question is how do you want to do it? Quote Link to comment Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 The PHP side is already done, i just want to change it because already users have sent things to the wrong user because there if everything fits in the form then it does it without asking the user again. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted January 18, 2009 Share Posted January 18, 2009 Then you need to provide better verification ... The javascript solution would be using ajax to query the server in the background after they enter a user. You could hook into the form submit action and have it take the value of the "user" input and query to see if that user exists. If the user doesn't exists: alert("User doesn't exist, please enter another user"); If it does exist pop up your confirm like normal, displaying the username with php's answer to javascript.. It is confusing because you're only showing me one function... From what I can tell.. this is called after the form is submitted? Quote Link to comment Share on other sites More sharing options...
Flames Posted January 18, 2009 Author Share Posted January 18, 2009 Called when you attempt to submit the form, but yeah i could set ID as an hidden input check that submit onClick and then check again on php side and then do the confirm, thanks alot xtopolis, not the first time you've helped me with JS or Ajax 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.