0

I want to add a javascript value to my php query. The code goes like this

<script>
    $(document).on('click', '.dropdown-menu li a', function() {
    var categoryName = $(this).html();

    <?php
         $one = mysqli_query($con, "SELECT id FROM tb_category WHERE category_name = '?' ");
    ?>

    })

</script>

I want to put the value of categoryName to the query which I made in php. How will I do it.

jim
  • 1
  • 1
  • 3
  • You could do an AJAX call and send the data to a separate PHP file that deals with your database query. – aphextwix Dec 02 '14 at 08:39

5 Answers5

0

Just spend a few hours trying to figure this out myself today. You can use ajax, and send the information using the data tag in an ajax query.

   $.ajax({ url: 'file goes here',
          data: {action: '0'},
          type: 'post',
       success: function(output) { //action }
    });

The data tag gives you the information. In your php:

$data = $_POST["action"];
Josh
  • 718
  • 10
  • 25
0
<script>
$(document).on('click', '.dropdown-menu li a', function(e) {
    e.preventDefault();
    var categoryName = $(this).text(); 
    // !! be careful here !! send web safe category name, `$(this).text();` or even better a numeric value.
    $.get('runquery.php', {'category': encodeURIComponent(categoryName)}).done(function (data) {
        // on success do something if needed
    });
})
</script>

Put your PHP/SQL script in runquery.php

Ghassan Elias
  • 2,213
  • 1
  • 14
  • 17
  • 1
    It would not always work if you don't `encodeURI` the `categoryName` in the GET request URL. What you could also do is: `$.get('runquery.php', {'category': categoryName}, function (data) {});` – Mathieu Rodic Dec 02 '14 at 08:55
  • he didn't ask us to do all his work. for example i didn't correct his sql query to prevent sql injection. http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Ghassan Elias Dec 02 '14 at 08:58
  • indeed. I was suggesting it for the community :) (AJAX calls are a bit tricky for early beginners anyway) – Mathieu Rodic Dec 02 '14 at 09:01
0

you should use ajax to get the attribute from your clicked div and send it to a php page where you receive the "javascript variable" and use it as a php variable with $_POST like this:

  <script type="text/javascript">
  $(function() {   
 $(document).on('click', '.dropdown-menu li a', function() {
 {
 var categoryname = $(this).attr("id");   
 var dataString = 'categoryname'+ categoryname ;


$.ajax({
type: "POST",
url: "yourphppage.php",
data: dataString,
cache: false,

success: function(html)
{

$(".result").html(html);  
} 
});


return false;
});
});

Now in your php page:

     $categoryname=$_POST['categoryname'];

     $one = mysqli_query($con, "SELECT id FROM tb_category WHERE category_name = $categoryname   
     ");
Katyoshah
  • 129
  • 10
  • but how i can I get the data from the query?? I want it because i want to use it to these code echo "$('#region').val('" . $rowOne['category_name'] . "');"; – jim Dec 02 '14 at 08:54
  • you can return the data resulting from the query in a div, you select the div in the success function $(".result").html(html); create an empty div and give it a class name "result" now the result of the query will be loaded into the div of class "result" – Katyoshah Dec 02 '14 at 09:34
0

It doesn't go this way, unless you make AJAX calls (see http://api.jquery.com/jquery.get/ and other answers).

The simplest solution (if you do not have too many categories, say a few hundreds tops) would be to cache the categories client-side in a Javascript variable, as in:

<script type="text/javascript">
    var categoriesCache = <?php
        $cache = array();
        $result = mysqli_query($con, "SELECT id, category_name FROM tb_category");
        while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
            $cache[$row["category_name"]] = $row["id"];
        }
        echo json_encode($cache);
    ?>;
    $(document).on('click', '.dropdown-menu li a', function() {
        var categoryName = $(this).html();
        var categoryId = categoriesCache[categoryName];
    });
</script>
Mathieu Rodic
  • 6,637
  • 2
  • 43
  • 49
0

You can do this with Ajax http://www.w3schools.com/jquery/jquery_ajax_get_post.asp

Php-script in a separate File like:

First you JavaScript file:

$(document).on('click', '.dropdown-menu li a', function() {
var categoryName = $(this).html();
  $.post("php-file.php",{cat_Name: categoryName});
});

then you php-file.php looks like:

<?php
     $categoryname = $_POST['cat_Name'];
     $one = mysqli_query($con, "SELECT id FROM tb_category WHERE category_name = '?' ");
?>

if you want to use them again:

add to your PHP on the end:

$return = new array();
$return[0] = ;//your results in this array
echo json_encode($return);

then in your JavaScript should look like:

$(document).on('click', '.dropdown-menu li a', function() {
    var categoryName = $(this).html();
      $.post("php-file.php",{cat_Name: categoryName}, function(data){
          sccess_function(data);}, "json");
});

then add a function:

function succsess_function(data){
    //do thing here

}
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304
x1x
  • 308
  • 1
  • 7