1

So i have 4 php variables named $description,$hscode,$from,$to I wants to send these variables through a AJAX request.My incomplete attempt is like this:

function searchFilter(page_num) {
  page_num = page_num?page_num:0;

    $.ajax({
        type: 'POST',
        url: 'getData.php',
       data: { page : +page_num, "desc" : "<?php  print $description ?>","hscode" : "<?php  print $hscode ?>","from" : "<?php  print $from ?>","to" : "<?php $to ?>" },

        beforeSend: function () {
            $('.container-fluid').waitMe({
                   effect : 'stretch',
                   bg : 'rgba(255,255,255,0.7)'
                });
             },
        success: function (html) {
            $('#posts_content').html(html);
            $('.container-fluid').waitMe("hide");
        }
    });
}

The problem is, PHP variables are dynamic means they are not always set sometime only $description is set, sometime only $hscode is set ,sometime $description and $hscode only set,There are 6 combination of this type.Please help in this situation how can i send this request.

POSSIBLE Answer:

code is working fine but i am confused is it a right way to do it,Please correct me if i am wrong,my working code is :

function searchFilter(page_num) {
  page_num = page_num?page_num:0;

    $.ajax({
        type: 'POST',
        url: 'getData.php',
        data :  { page : +page_num, 
                  <?php if(isset($description)){ echo '"desc"'.' : '. "'$description'" . ',';} ?>
                  <?php if(isset($hscode)){ echo '"hscode"'.' : '. "'$hscode'" . ',';} ?>
                  <?php if(isset($from)){ echo '"from"'.' : '. "'$from'" . ',';} ?>
                  <?php if(isset($to)){ echo '"to"'.' : '. "'$to'" . ',';} ?> },


        beforeSend: function () {
            $('.container-fluid').waitMe({
                   effect : 'stretch',
                   bg : 'rgba(255,255,255,0.7)'
                });
             },
        success: function (html) {
            $('#posts_content').html(html);
            $('.container-fluid').waitMe("hide");
        }
    });
}
19
  • 1
    Can you include which questions you think this is not a duplicate of? It looks remarkably similar to a 2.5 second search which gave this: stackoverflow.com/questions/8236354/php-is-null-or-empty Commented Mar 27, 2018 at 11:23
  • 1
    and this stackoverflow.com/questions/9073020/… Commented Mar 27, 2018 at 11:24
  • 1
    aaaand this stackoverflow.com/questions/2659837/… Commented Mar 27, 2018 at 11:24
  • actually i have searched too much,my searching parameters was not right,will delete this question if i found major duplicate.thanxxx Commented Mar 27, 2018 at 11:25
  • sidenote: the syntax in data: {...} is wrong. parameters shouldn't have a " around them. Commented Mar 27, 2018 at 11:27

2 Answers 2

1

First you can check your variables are set or not, if its set then on;y append those variables to ajax data. Try following code:

function searchFilter(page_num) {
    page_num = page_num?page_num:0;
    send_data = {};
    send_data.page = +page_num;
    <?php if (isset($description)): ?>
    send_data.desc = "<?php echo $description ?>";
    <?php endif; ?>
    <?php if (isset($hscode)): ?>
    send_data.hscode = "<?php echo $hscode ?>";
    <?php endif; ?>
    <?php if (isset($from)): ?>
    send_data.from = "<?php echo $from ?>";
    <?php endif; ?>
<?php if (isset($to)): ?>
send_data.to = "<?php echo $to ?>";
<?php endif; ?>

      $.ajax({
          type: 'POST',
          url: 'getData.php',
         data: send_data,

          beforeSend: function () {
              $('.container-fluid').waitMe({
                     effect : 'stretch',
                     bg : 'rgba(255,255,255,0.7)'
                  });
               },
          success: function (html) {
              $('#posts_content').html(html);
              $('.container-fluid').waitMe("hide");
          }
      });
}
Sign up to request clarification or add additional context in comments.

11 Comments

This is JavaScript - I don’t think these php tags will work for you unless code is directly within the php file. I would probably move it to a js file and obtain these from some dom elements.
PHP tags can also work in javscript @SebastianSulinski
@B.Desai do php tags work in a .js file? Or are they served without parsing?
Thaxx for answer i will try these answers will reply soon
No @freedomn-m I know PHP tags will not work in .jsfile. but in question OP already using PHP tags in js so its confirm that OP using .php file
|
1

For the sake of clarity, assemble your array in PHP, then use json_encode() to convert it to a JSON object:

<?php
$php_data = [desc => $description, /* initialize other fields */];
?>
function searchFilter(page_num) {
  var data = <?php= json_encode($php_data) ?>;  /* dump as JSON object */
  data.page_num = page_num?page_num:0;          /* assign page_num field */
  $.ajax({
      type: 'POST',
      url: 'getData.php',
      data: data,                               /* pass object */

1 Comment

hmm working fine but i am not working with json anyway it is also an answer.please upvote the question,thanxx,i got my answer

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.