-4

I have got a PHP file that received data that has been posted from a form. It then sends it to a database. However I needed to get those variables and put them in JavaScript. I have done the following, but when logging the variables supposed to store the php data (in the script file) it return undefined.

What am I doing wrong? I am new to all the languages. The files are all separate - with the PHP and Script files being external.

SCRIPT:

$(function(){
var data1 = $("#username").val();
console.log(data1);
$.ajax({
  type: "POST",
  url: 'signUp.php',
  data: ({data1}),
  success: function(data) {
    console.log("success");
  }
});

});

PHP

if (isset($_POST['signup'])){
    //The connection to the database
    include_once 'databaseHandler.php';

    //Gets the infomation submitted from the form
    //Protects the database by converting everything to text...
    //The database therefore cannot read the inputs as code
    $username = mysqli_real_escape_string($conn, $_POST['username']);
    $password = mysqli_real_escape_string($conn, $_POST['password']);

    echo (".$user.");

HTML

<form action="signUp.php" method="POST">
        <input id="usernameSignUp" type="text" name="username" placeholder="Username">
        <br>
        <input id="passwordSignUp" type="password" name="password" placeholder="Password">
        <br>
        <button class="BUTTON" type="submit" name="signup">SIGN UP</button>
    </form>
Aftab H.
  • 1,517
  • 4
  • 13
  • 25
  • You're logging what where which returns `undefined`?! – deceze Jan 25 '18 at 11:28
  • The form pushed the users input to a php file called SignUp.php. That php file then pushes that to a database, but that is irrelevant. I then used Ajax and jQuery to then get the information stored on $username to be stored on a script variable called Data1. But when I console.log(data1), the value stored in data1 is 'undefined'. Hope that helps to clear it up a bit more – Jasmine.661 Jan 25 '18 at 11:37

3 Answers3

0

You never set the post parameter 'signup'. Which in turn does not let you enter your if construct and therefor php gives you nothing.

Try:

data:{
    "username":$('usernameSignUp').val(),
    "password":$('passwordSignUp').val(),
    "signup":1,
}
Anubis
  • 481
  • 4
  • 4
0

I think, you don't send the variable correctly.

var data1 = $("#usernameSignUp").val();
var data2 = $("#passwordSignUp").val();
$.ajax({
  type: "POST",
  url: 'signUp.php',
  data: {signup:'1',username:data1,password:data2 },//Supose data1= username data, data2 = password data
  success: function(data) {
    console.log("success");
  }
});
0

There is so much wrong here, it is hard to know where to begin.

  • You make the Ajax request when the page loads. This is before the user has typed anything into the form.
  • When the form is submitted, you don't use Ajax at all
  • You pass a field called data1 to the server (which it is not looking for)
  • You give that field the value of the input with id="username" (which doesn't exist)
  • You don't pass fields called username or password to the PHP (which it is looking for)
  • You don't pass a field called signup to the PHP (which it tests for with isset before doing anything)
  • Your PHP echos a variable called $user which you haven't defined
  • Your JavaScript doesn't look at the response, it just logs the string "success"

You'd need something more like:

$(function() {
  $("form").on("submit", function(event) {
    event.preventDefault();
    $.ajax({
      type: "POST",
      url: 'http://example.com/signUp.php',
      data: ({
        signup: 1,
        username: $("#usernameSignUp").val(),
        password: $("#passwordSignUp").val()
      }),
      success: function(data) {
        console.log("Success", data);
      },
      error: function(data) {
        console.log("This is a cross origin request to a dummy URL, what did you expect");
      }
    });
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="signUp.php" method="POST">
  <input id="usernameSignUp" type="text" name="username" placeholder="Username">
  <br>
  <input id="passwordSignUp" type="password" name="password" placeholder="Password">
  <br>
  <button class="BUTTON" type="submit" name="signup">SIGN UP</button>
</form>

Then there are the things which are bad practises but which don't directly affect the ability of the code to work.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I replaced it with your code from above and changed it so that it uses my variables and updated the echo in the PHP file to be correct. However it doesn't have a success or fail response. For the url, do I need to write localhost etc or will it do it automatically if I just write "signUp.php"?. Also I do hash the passwords further down in the code before sending them to the database. I just left that out of the snippet above :) Thank you though. – Jasmine.661 Jan 25 '18 at 12:05
  • Also, upon successful sign up, it directs you to another page called game.php. Does that need to be taken into account? – Jasmine.661 Jan 25 '18 at 12:21
  • "However it doesn't have a success or fail response" — Did you click the submit button? Are any errors shown on the Console of the Developer Tools in your browser? – Quentin Jan 25 '18 at 12:39
  • "For the url" — Relative or absolute is fine. – Quentin Jan 25 '18 at 12:39
  • "Also I do hash the passwords further down in the code" — You're doing it in the wrong order. You escape the data you want to put into the database. So you need to take the user input, hash it, **then** escape it for the DB. – Quentin Jan 25 '18 at 12:40
  • "Also, upon successful sign up, it directs you to another page called game.php. " — This code doesn't. If you want to load a new page: **why are you using Ajaz?**. The **entire** point of Ajax is that it **doesn't** load a new page. – Quentin Jan 25 '18 at 12:40
  • "Did you click the submit button" - yes. Nothing was displayed on the console. – Jasmine.661 Jan 25 '18 at 15:54
  • "You're doing it in the wrong order......" - I didn't think that that. I will switch it around now. Thank You. – Jasmine.661 Jan 25 '18 at 15:54
  • "This code doesn't. If you want to load a new page: why are you using Ajaz-....." - I am new to these languages. I googled how to pass variables from php to javascript and ajax was mentioned a lot so I went with it. If there is a better way to do this, I would appreciate if you could explain how? Thank You – Jasmine.661 Jan 25 '18 at 15:56
  • Remove all the JavaScript. Just submit the form. – Quentin Jan 25 '18 at 16:05