0

I have edited my code and it goes something like this and it is not working.. Please help me..

<?php
    $variable = "krishna";
?>

<script>
    $.ajax({
        type:"POST",
        url:"ajax.php",
        data:{
            variable:<?php echo $variable; ?>
        },
        success:function(msg){
            $("#val").html(msg);
        }
    });
</script>

<div id="val"></div>

ajax.php

<?php
    echo $_POST['variable'];
?>

thank you all

1
  • you can use echo, if its not dinamically; cant you generate the variable with javascript? Commented Sep 12, 2013 at 3:26

3 Answers 3

1

Try this

<script type="text/javascript">
    $.ajax({
            type: "POST",
            url: "",
            data: 'var=<?php echo $variable;?>',
            success: function(){

            }
       });
</script>
Sign up to request clarification or add additional context in comments.

Comments

1
<script>
$.ajax({
type:"POST",
url:"",
data:{data:'<?php echo $data; ?>',data1:'<?php echo $data1; ?>'}
success: function(data)
{   }
});
</script>

You can add any number of varibles using data{data1:data1, data2:data2, data3:data3} and it's stand like {variablename:value}

2 Comments

Not proper syntax, values must be quoted.
For posting code that does not work as an answer, yes. Since removed after fix.
0

Use an echo statement inline with the javascript. Because PHP executes on the server all the PHP processing will be done by the time the javascript runs.

<?php
$variable = "php";
?>

<script>
$.ajax({
type:"POST",
url:"",
data:{
variable:"<?php echo $variable; ?>"
},
success:
});
</script>

Comments

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.