0

I am finding some difficulty to solve this problem. What I am trying to do is to pass a variable that is selected from a Database to javascript function.

At this line $('.form-container a.startStampleDiv').click(function(), I am trying to start a new query to the database using the selected data from the old one. So what I need is to return the selected costumerId from the database to the ajax so I can pass it through. I hope you guys understand my issue and try to help me out.

Note: this code is working for me, the only missing thing is to return variable from php to ajax.

Here is the js code:

$('#searchCostumer-btn').click(function(){
var phonenumber = $('#phonenumber').val();
var phonenumberReg = /^[\s()+-]*([0-9][\s()+-]*){6,20}$/;
var phonenumberError = Boolean(true);

if (phonenumber==""){
    $('#phonenumber').css("border", "1px solid red");
    phonenumberError= false;
}
else if(!phonenumberReg.test(phonenumber)){
    $('#phonenumber').css("border", "1px solid red");
    phonenumberError= false;
}
else{
    $('#phonenumber').css("border", "1px solid white");
}
if(phonenumberError===true){
    $.ajax({
        type:'POST',
        data:{phonenumber: phonenumber},
        url: 'php/searchCostumer.php',
        fail: function(data){
            alert('An internal Error has been occured please contact adminstrator.');
        },
        success: function(data){
            document.getElementById("search-Costumer").reset();
            $('#search-result').html(data);



            // Event handlar click on costumer
            $('.form-container a.startStampleDiv').click(function(){
                document.getElementById('stample-container').style.display = 'block';
                // here i want to pass the costumerID to another php file or code. So what i need to do is to pass the costumerId that is returned by the php file...


            });  
        }
    });
}

});

and here is the php code:

<?php
ob_start();
session_start();
include("figaroStampledb.php");

if(isset($_POST['phonenumber']))
{
    $phonenumber = trim(htmlentities($_POST['phonenumber']));

    try {
        $con = new PDO("mysql:host=$sql_login_host;dbname=$sql_login_db", $sql_login_user, $sql_login_pass);

        $selectCostumers = $con->prepare("SELECT * FROM fCostumers WHERE phonenumber=:phonenumber");
        $selectCostumers ->execute(array(':phonenumber' => $phonenumber));
        $resultCostumers = $selectCostumers->fetchAll(PDO::FETCH_ASSOC);

        print json_encode($resultCostumers);

        $count=$selectCostumers->rowCount();

        if($count>0){
            print '<script src="js/validation.js" type="text/javascript"></script>';
            foreach ($resultCostumers as $costumer)
            {                   
                print '<div><i class="glyphicon glyphicon-user"> </i><a href="#stample-container" class="startStampleDiv">' ." " .$costumer['firstname'].'</a></div><hr>';
                //$_SESSION['$costumerId'] = $costumer['id'];
            }
        }
        else{
            print'Kunden är icke registrerad';
        }
    }
    catch(PDOException $e) {
        trigger_error('Error occured while trying to insert into the DB:' . $e->getMessage(), E_USER_ERROR);
    }
}

?>

3
  • echo out your variable, then use the success callback to use it. Commented Aug 8, 2015 at 22:42
  • as you see under the following line $resultCostumers = $selectCostumers->fetchAll(PDO::FETCH_ASSOC); i try to echo json_encode, i tried both echo and print, but it doesnt work for me. Commented Aug 8, 2015 at 22:51
  • Make sure you don't echo anything else out. Commented Aug 8, 2015 at 22:52

1 Answer 1

1

You can add this line along with the print statement in your php script..

<input type="hidden" id="custmid" value="$your_custmerid_variable">

So ajax response will also add it in html but it won't make any changes to html because its hidden..

Than get its value where you mentioned using $("#custmid").value() ; and further send it to php page where you want using another ajax request.... Hope you will figure out how to do that...

Sign up to request clarification or add additional context in comments.

4 Comments

bro one fast questuon, how to do if i have more then one costumerId?
Well if you have more than 1 customer id than it would be a different approach.... You could use an array to store all those ids and use a for loop to get all those values plus you have to change id to a class because id support only 1 unique value...
i am trying to vote for ur answer but i dont know why it doesnt working.
No moji, it wasn't about voting the answer, a user who ask question can accept the 1 answer as the right answer if it satisfies the user's solution.. So you can mark my answers right if you think it solves your problem... Which help other user as well having similar problem like you to look for answer marked accepted .... Nevermind don't bother if you are not able to vote.. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.