1

I'm trying to use a value from onsubmit() in php.

More specific: An HTML form is being submitted by the client. The onSubmit passes it to a couple of javascript functions. One of these functions is supposed to fetch a list of registered emails from the database to check if it is already used.

<?php
$email='?> frm.add_fd2.value <?php ';
// execute query
$num_rows = mysql_num_rows($result); ?>
var num_rows = <?php echo (is_int($num_rows)) ? $num_rows : 0; ?>;
if (num_rows > 0) {
    // JAVASCRIPT ERROR
}
1
  • 1
    What exactly is it that you want to know? Commented Feb 8, 2011 at 14:37

1 Answer 1

1

JavaScript is a client-side language. It looks like you're trying to retrieve a value from it through PHP, which will never happen. PHP will be long-executed before JavaScript even comes in to play.

You need to either submit the value to PHP through a form, or use something like an AJAX call and send PHP the value so it can process it after the page has been loaded.

In your case, AJAX appears to be a better option. In which case something like the following should work:

$.getJSON('verify_email.php',{email:frm.add_fd2.value},function(data){
  if (data.matches>0){
    // matches were found
  }else{
    // no matches found
  }
});

then in verify_email.php:

<?php
  // ... establish connection ...
  $email = mysql_real_escape_string($_GET['email']); // note, this should be checked for existance
  $result = mysql_query("SELECT * FROM table WHERE email='{$email}'");
  header('Content-Type: application/json');
  echo '{matches:'.mysql_num_rows($result).'}';
?>

Or something of that sort. (Note I'm using jQuery to make this syntactically easier).

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

2 Comments

Hi Brad, this seems like it makes a lot of sense. Thanks for pointing me in the right direction!
@Marius: You're welcome, not a problem. If you need any further help, just leave a comment (or post a new question). ;-)

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.