-1

Please find my code below...This is basically 2 files javascript and php file.

I am trying to call the php file from the javascript. But I get a 500 internal server error when the jquery function is called.

When I checked chrome web developer tools, I can see that the execution is stopping in one of the jquery library functions. But I could be wrong in that judgement since I am not totally familiar with developer tools.

Could you please let me know whats going wrong?

validation.js

function validateName(){
var uname=name.val();

$.post('validate.php',{names:uname},function(data){

if(data!=0){

name.removeClass("valid");
nameInfo.removeClass("valid");
name.addClass("error");
nameInfo.addClass("error");
nameInfo.text("This name is already registered");
state=true;
}else
{
name.removeClass("error");
nameInfo.removeClass("error");
name.addClass("valid");
nameInfo.addClass("valid");
nameInfo.text("Name is available");
state=true; 
}
});

return state;
}



$( "#send" ).click(function() {
var all = $("form").serialize();
$.ajax({
url: 'insert.php',
type:'POST',
data: all,
dataType: "text",
success: function (response) {
if (response==1){
document.location.href="/view.html";
}else{
alert("you have errors");
}
}
});

insert.php

<?php

$name=$_POST['name'];
$email=$_POST['email'];
$pass=$_POST['pass1'];

mysql_connect("localhost","root","root") or die("we couldnt connect");
mysql_select_db("swiftyfood");
mysql_query("INSERT INTO userCredentials(userid,name,password) VALUES('',                      
'$name','$pass1')");
echo 1;
?>

Validate.php

<?php
echo "hello";
ini_set('display_errors', 1); 
error_reporting(E_ALL);

$name=$_POST['names'];
$email=$_POST['emails'];
$pass=$_POST['passws'];

if ($name!=""){
mysql_connect('localhost','root','root') or die("we couldnt connect");
mysql_select_db("swiftyfood");
$username=mysql_query("select name from userCredentials where
name='$name'");
$count=mysql_num_rows($username);
if (count!=0){
echo 0;
}else{
echo 1;
}
?>

image of chrome developer tools.

Errors in Chrome

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
lordzeus1989
  • 55
  • 1
  • 7
  • 3
    A 500 error indicates that you sent something bad to the PHP and it reacted by failing. Look at your PHP error logs for more insight to the problem. – Jay Blanchard Apr 12 '16 at 15:43
  • 1
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 12 '16 at 15:44
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 12 '16 at 15:44
  • 1
    Please use PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html) to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). – Jay Blanchard Apr 12 '16 at 15:44
  • I don't see the variable `pass1` being defined anywhere. Did you mean to write `pass` instead? – Oisin Apr 12 '16 at 15:44
  • All of your PHP files are returning a `500` error. What do the logs say about it? – mferly Apr 12 '16 at 15:47
  • Always check the error logs - it will give you exact line numbers, telling you what you did wrong. Basic problem solving. – Novocaine Apr 12 '16 at 15:55

1 Answers1

0

Here's the issues I've found:

insert.php

Change $pass1 to $pass

mysql_query("INSERT INTO userCredentials(userid,name,password) VALUES('',                      
'$name','$pass')");

validate.php

You're missing the closing } for 'if ($name != "") {'

if ($name!=""){
    mysql_connect('localhost','root','root') or die("we couldnt connect");
    mysql_select_db("swiftyfood");
    $username=mysql_query("select name from userCredentials where
name='$name'");
    $count=mysql_num_rows($username);
    if (count!=0){
        echo 0;
    }else{
        echo 1;
    }
} // This was missing
LMS94
  • 1,016
  • 1
  • 7
  • 14