1

When changing the select box that particular <options>'s value has to pass to PHP variable on same page. everything happening on create_post.php file, Below code is my check box for that values are getting generated from database

<select id="Category" name="Category">
    <?php
      $sql1 = "SELECT DISTINCT(Cat) FROM categories ORDER BY Cat ASC";
      $qry1 = mysql_query($sql1,$link);
    ?>
      <option value="" selected="">Select Category</option>
    <?php

      while ($row1=mysql_fetch_assoc($qry1)) {                                                                   

    ?>
        <option value="<?php echo $row1['Cat']?>"><?php echo $row1['Cat']?></option>
    <?php 
      }
    ?>
</select>

then when changing the select box option that value have to print on same page.

<?php
  echo "<pre>";
  print_r($_REQUEST);
  echo "</pre>";

  if (isset($_REQUEST['Catgy'])) {
  $mainCat = $_REQUEST['Catgy'];
  echo $mainCat;
  }
?>

Sending select box value through ajax to same page.

$(document).ready(function() {

 $('#Category').change(function(){
   var Category = $(this).find('option:selected').attr('value');

   $.ajax({
     url:'create_post.php',
     data:{'Catgy':Category},

   });

    });
});

Out put is an empty array for print_r and nothing for echo. How to pass select box values when changing to PHP variable on same page?

user466061
  • 91
  • 11
  • 4
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 18 '16 at 17:08
  • AJAX to the same page? You shouldn't need to do that unless you need to send the variable to PHP for processing. The PHP in your page isn't being run again because of the AJAX call, hence you will see no change unless the page is reloaded. – Jay Blanchard Apr 18 '16 at 17:09
  • @jayBlanchard Thanks for the information i will improve my knowledge.. – user466061 Apr 18 '16 at 17:09
  • @jayBlanchard is there any way to set the select box value to php variable on same page? – user466061 Apr 18 '16 at 17:11
  • When the page loads you can set the select box to a PHP value. – Jay Blanchard Apr 18 '16 at 17:13
  • ok after page loaded if the user change the select box value is possible to to set that value to PHP variable on same page? – user466061 Apr 18 '16 at 17:15
  • Yes - you would reload the page unless you want to do it with JS. – Jay Blanchard Apr 18 '16 at 17:17
  • Hey, why don't you save the category in localStorage using javascript? So, when ever you are performing some action such as post, just send the value along. – Manikiran Apr 18 '16 at 17:17
  • Maybe if you can provide some information about what are you trying to do in the big picture we can help you more accurately – sminutoli Apr 18 '16 at 17:17
  • @JayBlanchard what if we put the $mainCat = $_REQUEST['Catgy']; echo $mainCat; this part of code inside a DIV and refresh it? will it work? – user466061 Apr 18 '16 at 17:20
  • That is easily testable. Why don't you try it? – Jay Blanchard Apr 18 '16 at 17:21
  • @sminutoli i am trying to do is choosing category part for e-commerce website. when the user selects first category for example if user select 'Electronics' according to that other sub categories has to load for example sub category Mobile phones then it's brand lists and condition...etc for that i am trying to get the selected value of category and run a SQL and select sub categories from database.. – user466061 Apr 18 '16 at 17:25

0 Answers0