开发者

jquery grabbing post details and returning them to the form

开发者 https://www.devze.com 2022-12-27 19:28 出处:网络
I\'ve been trying to use jQuery to grab the information from $_POST and return the user back to the actual form if their email address already exists in the system, however I can\'t seem to get jQuery

I've been trying to use jQuery to grab the information from $_POST and return the user back to the actual form if their email address already exists in the system, however I can't seem to get jQuery to grab the php variable $post (which has all the information from $_POST) and spit them back to the original form. Could someone help me out please? :)

**************** EDIT ****************

So here's the basic structure.

Form: Name, Email, 开发者_C百科Address.

Submitting the form calls a php function to check if user exists in the database. If the email address is in the database, I want it to say "sorry this email already exists, click here to try again".

After clicking on the link to try again, I want the form to re-display with the fields they just typed in to display again. <- this is what I thought jQuery could do to re-post the information back to the form page..?

If the user doesn't exist, the user is saved into the database.

Does that make more sense?


From the sound of your question what you're trying to do doesn't make sense. This is because PHP is a server side language while Javascript is a client side (in the browser). This means that Javascript, and therefore jQuery, don't have access to PHP's variables ($_POST included).

There are two common ways to solve this:

  • Have PHP generate the form. You would output the values from $_POST, or another data location, into the form (ex., echo the variable into the input tag's value attribute). This is by far the easiest method. For example: printf('<input type="text" name="foo" value="%s"/>', $someCleanVariable);

  • Have the PHP generate JSON or XML, and use AJAX to get the data from the PHP script. You would have to parse out the values into the form. This is basically the same as the previous example, except it's more portable: you can have any source consume the data instead of just your form.

However, make sure that you protect your users when doing this: you need to clean the data that you're sending back to the user to make sure that there is no HTML, Javascript, or other malicious code in it. You don't want people being able to alter the look of your page by passing it data. See Cross-site Scripting Attacks.

Cheers.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号