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");
        }
    });
}
Ankit Mishra
  • 130
  • 11
  • 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: https://stackoverflow.com/questions/8236354/php-is-null-or-empty – freedomn-m Mar 27 '18 at 11:23
  • 1
    and this https://stackoverflow.com/questions/9073020/the-best-practice-for-not-null-if-statements – freedomn-m Mar 27 '18 at 11:24
  • 1
    aaaand this https://stackoverflow.com/questions/2659837/check-if-a-variable-is-empty – freedomn-m Mar 27 '18 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 – Ankit Mishra Mar 27 '18 at 11:25
  • sidenote: the syntax in `data: {...}` is wrong. parameters shouldn't have a `"` around them. – Jeff Mar 27 '18 at 11:27
  • actually my code is working if all php variable is set,and if anyone is not set code doesn't work – Ankit Mishra Mar 27 '18 at 11:29
  • `print $description` where's that defined? There isn't enough php to support this post. – Funk Forty Niner Mar 27 '18 at 11:31
  • @freedomn-m have you read my question carefully ?..in these links we can check only php variable is set or not ,I am asking about how to send with ajax. – Ankit Mishra Mar 27 '18 at 11:31
  • @FunkFortyNiner i am getting 4 variables with $_GET and wants to send to getData.php. i am stuck at sending,i dont think there is a need of my getData.php code – Ankit Mishra Mar 27 '18 at 11:34
  • use error reporting and look at your developer console then. – Funk Forty Niner Mar 27 '18 at 11:34
  • @FunkFortyNiner You can assume 4 given variable.Why you are downvoting question if you have no idea how to solve. – Ankit Mishra Mar 27 '18 at 11:35
  • I did read your question. You have 4 variables that are sometimes not defined, therefore you need to *check if they are defined*. Your question boils down to: *"how do I check if a php variable is defined?"* As I'm not a php developer, quick search on how to check if a variable is defined gave those results and others. Can you explain how your question is *not* a simple case of checking if a variable is defined? – freedomn-m Mar 27 '18 at 11:36
  • Please include error messages in your question. Without those it is incredibly hard to answer. if you have questions please review [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Stefan Crain Mar 27 '18 at 11:36
  • I told you what to do, but you seem set to want us to debug your code now and to give you a working solution "now". What have you done to do this and how much time have you spent in debugging? Honestly now. – Funk Forty Niner Mar 27 '18 at 11:39
  • you have an answer below now; see that. Edit: answers, plural form. – Funk Forty Niner Mar 27 '18 at 11:39
  • I would go with something like: `{ page : +page_num, }` (whatever the php for concat strings is). – freedomn-m Mar 27 '18 at 11:39
  • @freedomn-m yes you got my point ,I can always check which variable is set or not.but what to do when assume in first request two variable is set and on second request 4 all variables are set. – Ankit Mishra Mar 27 '18 at 11:41
  • That's nothing to do with how you *send* the data, but rather how you *receive* the data - ie the `getData.php` - which would check if the values had been provided or not. – freedomn-m Mar 27 '18 at 11:42
  • @freedomn-m ok i will try your mentioned idea,i think it will work.Thanxxx – Ankit Mishra Mar 27 '18 at 11:45

2 Answers2

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");
          }
      });
}
B. Desai
  • 16,414
  • 5
  • 26
  • 47
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 */
Leonhardt Wille
  • 557
  • 5
  • 20
  • 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 – Ankit Mishra Mar 29 '18 at 09:04