I am getting response as standard id in my project & i need to pass this value to php variable any body help me.

My PHP page

<div class="checkbox check-success"> <?php
$b=mysql_query("select * from standard_names ")or die(mysql_error());
$count  = mysql_num_rows($b);
$i = 0;
$breakafter=5; ?><?php
while($row = mysql_fetch_array($b))
{ 
$i++;
?>    
    <input type="checkbox" class="chkNumber" id="<?php echo $row['sn_id']; ?>" value="<?php echo $row['sn_id']; ?>" />
    <label for="<?php echo $row['sn_id']; ?>"><?php echo $row['s_names']; ?></label>

     <?php  if($i % $breakafter == 0){?> <br><?php }
}
?>   </div>

MY Jquery page

<script>
$(document).ready(function() {
$("#standardnameb").click(function(){
var standdis = $("#standdis").val();
    if ($('.chkNumber:checked').length) {
          var chkId = '';
          $('.chkNumber:checked').each(function () {
            chkId += $(this).val() + ",";
          });
          chkId = chkId.slice(0, -1);

        }
        else {
          alert('Nothing Selected');
          return false; 
        }


$.post("ajax.php", 
{ 
standdis:standdis,
chkId:chkId
},
function(response){ 
if(response==0){ 
var str = "Inserted Successfully";
var result = str.fontcolor("green"); 
$("#disp4").html(result);
$("#ediths").hide();
 }
else {
var str = "Already Inserted..! Please Edit Below ";
var result = str.fontcolor("green"); 
$("#disp4").html(result);
<!--$('#ramu').load(' #div1');
-->
$("#ediths").show();
 }
});
});
});
</script>

and my ajax page

if(isset($_REQUEST['standdis']) && isset($_REQUEST['chkId'])){
$standdis=mysql_real_escape_string($_REQUEST['standdis']);
$chkId=mysql_real_escape_string($_REQUEST['chkId']);
$a=mysql_query("select * from   st_name_assign_st_cat where s_id='$standdis' ")or die(mysql_error());
$row=mysql_num_rows($a);
if($row==0){
mysql_query("insert into   st_name_assign_st_cat(s_id,sn_id,status,c_date) values ('$standdis','$chkId','0','$c_date')") or die(mysql_error());
echo "0";
}else{
echo  $standdis;
}
}

Any body help me how to pass response to php variable dynamically

i don't know if it is posible to be done. But for me, it is not a good idea to assign a ajax result into php variable(mixing clint side and server side scripting)

PHP scripts run on the server, obviously, so you would need to post the data back once the AJAX request had finished. You could do that either by submitting the form or doing another AJAX request aimed at the target PHP code.

Well, you can't "just" pass it to PHP. You will need to work with it in Javascript. If you need it in PHP, maybe you shouldn't be handling this stuff with AJAX at all?

Member Avatar for diafol

Any chance that you can indent your code and povide some whitespace between assignments? ? Very difficult to follow.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.