Jump to content

sending back variable to ajax


tommie20

Recommended Posts

Hi guys,

 

I have a login page where the user can login, the input is being send to php where it checks if the input is in the mysql database. If so i would like to send back the username that is typed in to autofill it in a form completed later on. In code:

 

 $(document).ready(function(){
        $("#username").focus();

        $("#submit").click(function(){
                $.ajax({
                        url: "http://serverip/session.php",
                        cache: false,
                        type: "POST",
                        data:{
                                username: $("#username").val(),
                                password: $("#password").val()
                        },
                        success: function(data){
                                if (data == '1')
                                     
                                     window.location.replace("home.html");
                                     
                                else{
                                        alert("error");
                                }
                        }
                });
        });
}); 

 

PHP:

 

<?php

session_start();

if(!isset($_SESSION['username'])){
        if(isset($_POST['username'])){
                $db = new mysqli("localhost", "root", "", "gegevens");
                $stmt = $db->prepare("SELECT username FROM members WHERE username = ? AND password = ?");
                $stmt->bind_param('ss', htmlentities($_POST['username'], ENT_QUOTES), htmlentities($_POST['password'], ENT_QUOTES));
                $stmt->execute();
                $stmt->store_result();
                
                 if($stmt->num_rows() == 1){
                        $stmt->bind_result($username);
                        $stmt->fetch();
                        $_SESSION['username'] = $username;

                        echo "1";
                        exit;
                }
        }else{
                echo "0";
        }
}
?> 

 

I read in other posts that i could use datatype json to send multiple variables back. But i couldn't get it to work properly.

I would like to send back the username typed in but the '1' has to be send back too.

Thanks,

 

Tom

Link to comment
https://forums.phpfreaks.com/topic/251374-sending-back-variable-to-ajax/
Share on other sites

I read in other posts that i could use datatype json to send multiple variables back. But i couldn't get it to work properly.

 

Why not post the code that isn't working then?

 

It's really as simple as:

 

echo json_encode(array('username' => $username, 'status' => true));

 

In your php, then something like....

 

success: function(data) {
  if (data.status == 1) {
    console.log('You have logged in as ' + data.username);
  } else {
    console.log('Login failed');
  }
}

 

in you Javascript.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.