-3

I want to check the inputs of a form if they are empty and then i want to use php codes for user registeration.

    <script> 
    $('#submitbutton').click(function(){
       if($('#usernameinput').val() == ''){
          alert('Input is blank');
       }
       else {
             //here i want to use php codes for example;
$bgl = new mysqli("localhost", "root", "", "users");
if ($bgl->connect_errno) {
echo "Failed to connect to MySQL: (" . $bgl->connect_errno . ") " . $bgl->connect_error;
}
$username = mysql_real_escape_string($_POST['username']);
          .....
          .....
          ..... /and so on.. 
       }
    });
    </script>
ctarimli
  • 322
  • 4
  • 25

3 Answers3

1

In this case you most likely want to use $.ajax() to call a PHP page that supplies this information.

jQuery.ajax()

There is no such thing as php and javascript working together like that. Javascript in run on client side and php is run on server side. The best you can to is to pass and retrieve values to and from a php page and then use those values as intended.

Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
1

You have a few options here. The first is to post the form to a PHP page after the JavaScript validations (just use the form action) or use Ajax and call the PHP page, as Robin says.

warwickf
  • 123
  • 6
1

That's not posible, you can embed javascript into php code but not vice versa. The best way to implement that is developing a REST service and retrieving data with AJAX.

Serginho
  • 7,291
  • 2
  • 27
  • 52