daniel__
daniel__

Reputation: 11845

pass PHP variable value to jquery function

This code is not working properly. What i want is just send the variable $something to the page.php

What is the correct way to do this ? : data: <? php $something; ?>,

script

$something = "text";

    $.ajax({
    url: "page.php",
    type: "post",
    dataType: "html",
    data: <? php $something; ?>, 
    success: function (data) {
        $('#total').load('xxx.php');
    }
    });

Upvotes: 1

Views: 2925

Answers (2)

Clive
Clive

Reputation: 36957

myFile.php:

<?php $something = 'text'; ?>

<script>
$.ajax({
  url: "page.php",
  type: "post",
  dataType: "html",
  data: '<?php echo $something; ?>', 
  success: function (data) {
    $('#total').load('xxx.php');
  }
});
</script>

Upvotes: 4

Tadeck
Tadeck

Reputation: 137310

First of all, I think you have mistakenly mixed PHP and JavaScript. In your code the line:

$something = "text";

may be understood in two ways. If this is the whole code you have, then you are actually initializing JavaScript variable called $something. Later in the code you are trying to use the value of PHP variable called $something.

What you need to do is to change the code into (assuming you want to pass variable from PHP):

<?php $something = "text"; ?>

$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: '<? php $something; ?>', 
success: function (data) {
    $('#total').load('xxx.php');
}
});

or into (assuming you want JS variable):

var $something = 'text';

$.ajax({
url: 'page.php',
type: 'post',
dataType: 'html',
data: $something, 
success: function (data) {
    $('#total').load('xxx.php');
}
});

Upvotes: 2

Related Questions