Jump to content

[SOLVED] Merging JS and PHP


Flames

Recommended Posts

<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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

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.