JavaScript pasando el valor a la consulta php

this is my first post here and hopefuly someone will help me fix this problem. I have this mysql query:

$query_history = "
            SELECT ProductProvider AS NameSS, type, StoreOpinionDate AS dating
            FROM cd_stores 
            WHERE UserFID = '".$row_Profile['UserFID']."' 

            UNION ALL

            SELECT BrandName AS NameSS, type, BrandOpinionDate AS dating 
            FROM cd_brandopinions
            WHERE UserFID = '".$row_Profile['UserFID']."'

            UNION ALL

            SELECT CommentTitle AS NameSS, type, CommentDate AS dating 
            from cd_comments
            WHERE UserFID = '".$row_Profile['UserFID']."'

            UNION ALL

            SELECT brands.ProductBrand AS NameSS, type, date AS dating 
                from brandtrack INNER JOIN brands 
            WHERE UserFID = '".$row_Profile['UserFID']."' and brandtrack.BrandID = brands.BrandID

            ORDER BY dating DESC
            LIMIT 9

and follwing javascript:

<script type="text/javascript" src="../admin/config/js/jquery/jquery-1.3.2.min.js"></script>
    <script type="text/javascript">
            $("#loadmorebutton").click(function (){
                $('#loadmorebutton').html('<img src="facebook_style_loader.gif" />');
                    url: "facebook_style_ajax_more.php?lastid=" + $(".postitem:last").attr("dating"),
                    success: function(html){
                            $('#loadmorebutton').html('Load More');
                            $('#loadmorebutton').replaceWith('<center>No more posts to show.</center>');

now the problem is that when I inside form press submit button the Value "dating" from mysql query is not passed on acebook_style_ajax_more.php page using above JavaScript and Im getting empty value passed. What I need to change inside JS code to make it work?

Muchas Gracias

preguntado el 27 de agosto de 11 a las 17:08

wow - you need to protect your queries against sql injection, you better read this before solving your ajax issue -

can we see your html snippet? atleast -

Im modifying following script… and JohnJohn, this is just test example not a live script, Im familiar with injections and I just linked query to give an example -

3 Respuestas

Use firebug or chrome developer extension to see the xhr request being posted through your javacript to the php code. First of all see that it might be that your selectors didnt pick up the values from the html. If the xhr is fine than go to your php and try echoing the request params. Hope this helps.

Respondido 27 ago 11, 22:08

  1. for your actually are using get method replace ajax with get function
  2. put your query string in a data structure

it is easier to retrieve value in a variable var lastid= $(".postitem:last").attr("dating") || '';

$.get("facebook_style_ajax_more.php", { lastid: lastid } 
   .success(... wahever...)


Ivo Stoykov

Respondido 27 ago 11, 22:08

i think you might have a problem here.. try alerting the value of $(".postitem:last").attr("dating") and make sure it is getting passed to the php page

    url: "facebook_style_ajax_more.php?lastid=" + $(".postitem:last").attr("dating")

also if you are trying to sent the value of field with id "dating" then it should be

   url: "facebook_style_ajax_more.php?lastid=" + $("#dating").val()

ADDED: also your code code doesnt show where the facebook_style_ajax_more.php is processing the $_REQUEST["lastid"] and echoing something back for the ajax function to process.

Respondido 27 ago 11, 22:08

Im trying to modify this script… and make it work for my needs, all files are there - Boris Zegarac

i checked it.. i that in the loadmore.php they are processing the last id by $_GET['lastid'] and echoing some results back to the calling js function .. in your posted code i cant see them.. also u tried alerting the value of $(".postitem:last").attr("dating") before the ajax call? - Mithun Satheesh

The script is working fine at my site and Im processing by last date and Im getting the values when I click "Load More" button however Im the new results are not starting from next one it should but the same results are repeating over and over again but when I manually add values into loadmore.php file you mentioned, script is working OK so that why Im sure Javascript is not passing that "dating" php value - Boris Zegarac

in index.php inside the while loop:-> --------------------- echo '<div class="postitem" id="'.$rec->id.'">Post no '.$rec->id.': '.$rec->text.'"</div>'; --------------------------- here the class name and the id attribute should be there in your code also then only the selector will work fine and fetch the last displayed record id from the page so as to pass it to the loadmore.php.... - Mithun Satheesh

all the records you display in the list should have the class "postitem" and id as "id of the corresponding record table" then only the selector can pick the value correctly - Mithun Satheesh

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.