2017-09-04 48 views
0
<?php 
session_start(); 
$conn =new mysqli("localhost","root","","registration"); 
$userid=isset($_POST['userid'])?$_POST['userid']:''; 

//$re['success']=false; 
$sql="call regtask2('$userid')"; 
$res=mysqli_query($conn,$sql); 
$array = array(); 
if($res) { 

    while($row = mysqli_fetch_assoc($res)) 
    { 
     $array[]=$row ; 
     $re['success']=true; 
     $re['userObj']['firstname'] = $row['firstname']; 
    } 
} 


else { 
    $re['success']=false; 
} 
if(isset($_SESSION['username'])) 
{ 
    $sem=isset($_POST['sem'])?$_POST['sem']:''; 
    $fname=isset($_POST['fname'])?$_POST['fname']:''; 
    $year=isset($_POST['date'])?$_POST['date']:''; 
    $query = mysqli_query($conn,"select * from studentdetails inner join studentmarks on studentdetails.studentid=studentmarks.studentid where firstname='$fname' and sem='$sem'"); 
    $re = array(); 
    while ($row = mysqli_fetch_assoc($query)) 
    { 
     print_r($row); 
     //$options['userObj'][]=$row; 
    } 
} 

echo json_encode ($re); 
return; 
?> 

這是本我的完整的PHP代碼,我需要兩個JSON響應, 1>當我刷新頁面如何發送的PHP多JSON響應回到阿賈克斯

$sql="call regtask2('$userid')"; 

這個查詢有工作並將響應傳遞給ajax,然後我使用單擊按鈕。當我使用單擊按鈕,此查詢有工作並通過響應

$query = mysqli_query($conn,"select * from studentdetails inner join studentmarks on studentdetails.studentid=studentmarks.studentid where firstname='$fname' and sem='$sem'"); 

我這是poosible?

+0

目前還不清楚你期望的結果。你能提供一個你期望的查詢結果的例子嗎? 此外,你是什麼意思_multiple json response_? –

+0

我使用兩個使用相同的PHP Ajax,我期待兩個不同的答覆不同 – Maha

回答

0

很簡單隻需要添加第二查詢結果到以前的JSON,也可以考慮加入一些驗證到用戶的輸入,以防止SQL注入從$ _ POST
獲得用戶ID是非常糟糕的主意

<?php 
session_start(); 
$conn =new mysqli("localhost","root","","registration"); 
$userid=isset($_POST['userid'])?$_POST['userid']:''; 

//$re['success']=false; 
$sql="call regtask2('$userid')"; 
$res=mysqli_query($conn,$sql); 
$array = array(); 
$re = array(); 
if($res) { 
    $re['success']=true; 
    while($row = mysqli_fetch_assoc($res)) 
    { 
     $array[]=$row ; 
     $re['userObj']['firstname'] = $row['firstname']; 
    } 
} 


else { 
    $re['success']=false; 
} 
if(isset($_SESSION['username'])) 
{ 
    $sem=isset($_POST['sem'])?$_POST['sem']:''; 
    $fname=isset($_POST['fname'])?$_POST['fname']:''; 
    $year=isset($_POST['date'])?$_POST['date']:''; 
    $query = mysqli_query($conn,"select * from studentdetails inner join studentmarks on studentdetails.studentid=studentmarks.studentid where firstname='$fname' and sem='$sem'"); 

    while ($row = mysqli_fetch_assoc($query)) 
    { 
     $re['userObj'][]=$row; 
     //$options['userObj'][]=$row; 
    } 
} 

echo json_encode ($re); 
return; 
?> 
0

3個選項!

  1. 只是分裂你的php代碼。刷新時,加載script1.php併爲您的其他ajax調用,加載script2.php
  2. 您需要爲呼叫設置標識符。在你的ajax中,爲查詢添加一個「is_submit = true」。在你的php中,檢查這個值。
  3. 將您的返回值分配給$ return並返回。