1

I'm trying to send form values through ajax request to store in database but the file is not invoked through ajax. On submitting through button it goes to servr.js file and then through ajax it stored into database.

 <button id="submit" class="btn btn-primary" type="submit" onclick="myFunction()" >Create an Account</button>

And js file code as follow.

  function myFunction(){
           var name = document.getElementById("firstName").value;
            var lname = document.getElementById("lastName").value;
            var mail = document.getElementById("email").value;
            var password =document.getElementById("password").value;

            var confpass=   document.getElementById("passwordConfirmation").value;


            if(password != confpass)
            {
                alert ('password doesnot match!');
            }

            // var form_data = $('#edit_user').serialize();
          var datastring= 'name1=' + name + 'name2=' + lname +'email='+mail+ 'pass='+password;

           $.ajax({
        url: "learnapi.php",
        type: "post",
        dataType: "json",
        data: {type: "signup", datastring:datastring },

        ContentType: "application/json",
        success: function (response) {
            alert(JSON.stringify(response));
        },
        error: function (err) {
            alert(JSON.stringify(err));
        }
     });
 };

It goes to another php file which performs insert function .But this file not get call when I checked through inspect element(Q). learnapi.php

    <?php
header('Access-Control-Allow-Origin: *');//Should work in Cross Domaim ajax Calling request
mysql_connect("localhost","root","");
mysql_select_db("learnapp");
if(isset($_POST['type']))
 {
    $res = [];


  if($_POST['type'] =="signup"){
    $name  = $_POST ['name1'];
    $lname = $_POST['name2'];

    $passW = $_POST['pass'];
  //  $passW1 = $_POST['Pass1'];
    $mail  = $_POST ['email'];

    var_dump($_POST);

     $query1  = "insert into signup(firstname,lastname,password,email) values('$name','$lname','$passW','$mail')";
    $result1 = mysql_query($query1);

    if($result1)
    {
        $res["flag"] = true;
        $rest["message"] = "Data Inserted Successfully";
    }
    else
    {
        $res["flag"] = false;
        $rest["message"] = "Oppes Errors";
    }
    } 


else{
$res["flag"] = false;
$rest["message"] = "Invalid format";
    }

 echo json_encode($rest);
}  
?>

I tried but my ajax request is not called. What did I missed in this

oceanier
  • 87
  • 10
  • try `$("#submit").click(function(e){ ............. })` – Islam Zedan Jul 26 '16 at 10:47
  • You have to make a jquery form submit. – Avishake Jul 26 '16 at 10:49
  • what error got in console – Aslam Patel Jul 26 '16 at 10:50
  • I think there are no error but linking of pages may be not proper – oceanier Jul 26 '16 at 11:05
  • [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! [Don't believe it?](http://stackoverflow.com/q/38297105/1011527) – Jay Blanchard Jul 26 '16 at 12:29
  • ***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 Jul 26 '16 at 12:30
  • **Never store plain text passwords!** 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). Make sure you ***[don't escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jul 26 '16 at 12:30

1 Answers1

0
data: {type: "singup", ... }

Instead of

data: {type: "signup", ... }
jeanj
  • 2,106
  • 13
  • 22