Reputation: 267
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);
}
}
?>
Upvotes: 0
Views: 325
Reputation: 2749
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...
Upvotes: 1