0

Is anything like this possible?

var name = "<?php username_exists( $('input#register_name').val() ); ?>"

where

$('input#register_name').val()

is jquery code? So basically combining the two codes together so that PHP function is using that JQuery code?

2
  • Ajax and JSON are really easy to use. You should look into Jquery $.post - api.jquery.com/jQuery.post and PHP json_encode - php.net/manual/en/function.json-encode.php Commented Nov 4, 2012 at 22:07
  • What I would like to know is, how are you posting the form data? To the same page? Commented Nov 4, 2012 at 22:42

3 Answers 3

3

Not really.

  1. PHP runs on the server.
  2. It outputs some text.
  3. The browser interprets that text as HTML/JavaScript

By the time you hit step 3, the PHP has finished.

If you want to pass data from JS to PHP you have to do so in a new HTTP request (via a link, form submission, Ajax, etc).

Sign up to request clarification or add additional context in comments.

1 Comment

As I feared :( Really not good with this AJAX stuff. Trying to get it so that when my form is submitted, it checks that the username is not already registered and if it is then it displays a message. I can do it fine with just PHP but this refreshes the page. I don't want this to happen. I want just the message to appear.
1

No, simple answer. Server side code cant operate on client side code directly. However with AJAX you can send jQuery data back to PHP

Like:

 var name = $('input#register_name').val();
 $.post('checkuser.php', { name: name }, function(data) {
      alert("Hey");
 });

2 Comments

Whats the { name: name } part?
@Tenatious First name is the data key $_POST['name'] that you can retrieve in your PHP script. Second is the input value sent from your jquery. Of course it looks a little funny but I wanted to use the same js variable as you did.
1

Quick answer: no. You can use ajax to send PHP the value of the input and parse the response :).

In response to your comment above:

$('#my_input').blur(function() { // Change 'my_input' to ID of your input
    if ($(this).val() != '') { // Make sure it isn't blank to avoid unnecessary requests
        $.post('my_file.php', $(this).serialize(), function(response) {
            alert(response); // Variable 'response' contains response form php script
        });
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.