Michael
Michael

Reputation: 400

Returning a php element with a count with javascript

I have a site where people search for music and the search is recorded with their id attached to it. I am trying to make a feed out of it and it has worked thus far. However, the feed includes a javascript function where it makes their search into a link which when clicked, populates the search field and completes the search. The problem is I am looking to make each link it's specific search, but what ends up happening is it sets the value of the php variable to the last search in the array. What I'm now trying to do is set the php search variable with an counter to call that later in javascript. I know this sounds super confusing / not clear, but I'm trying my best. Maybe the code will help.

    $sql_userSearch = mysql_query("SELECT id, searchstring, timecount, userSearch FROM trending WHERE userSearch != 0 ORDER BY timecount DESC LIMIT 50");


    $counter = 1; 
    while($row = mysql_fetch_array($sql_userSearch)){

$search_id = $row["id"];
$searcher_id = $row["userSearch"];
$the_search[$counter] = $row["searchstring"];
$search_date = $row["timecount"];
$convertedTime = ($myObject -> convert_datetime($search_date));
$whenSearch = ($myObject -> makeAgo($convertedTime));
$search_date = $row["timecount"];


$searchDisplayList .= 
'<table style="border:#999 1px solid; border-top:none;" background="img/searchBG.png" cellpadding="5" width="100%">
 <tr>
    <td width="10%" valign="top"><img src="https://graph.facebook.com/'. $searcher_id . '/picture" width="35" height="35"></td>
    <td width="90%" valign="top" style="line-height:1.5em;">
    <span class="liteGreyColor textsize9"><strong>' . $searcher_id . '</strong></a> searched for </span>
    <a href="#" onClick="swapSearch()">' . $the_search[$counter] . '</a></br> ' . $whenSearch . ' 
    </td>
    </tr></table>';


$counter++;
}

then later on, this is the javascript I am using to return the link.

     <script type="text/javascript">
    function swapSearch(){
        document.getElementById('s').value = "<?= $the_search[$counter] ?>";
        $('#searchForm').submit();  
    }
    </script>

EDIT:

I just ended up using some javascript that grabbed the text of the link and populated the search form. Way easier than what I was trying to do.

   <script type="text/javascript">
   $("a.friendSearchLink").live("click",function(a) {       
a.preventDefault();                         
var searchTerm = $(this).text();
document.getElementById('s').value = searchTerm;
$("#searchForm").submit();                  
    });
    </script>

Upvotes: 0

Views: 139

Answers (2)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

you could store all the searches from $the_search into a JS array and then pass in $counter to swapSearch('.$counter.'). then you can look up in js whatever the search is.

The only other question I have though is are you submitting your search form as a post? Because if you change it to read from $_GET, you can just use the link to run the search just by setting href="?q=$the_search[$counter]". Other option is to pass the search id in the url and just use php to load the search result/keywords.

Upvotes: 0

Korvin Szanto
Korvin Szanto

Reputation: 4501

PHP is preprocessed, you can only echo a value prior to the page being sent to your client. In order to do what you're wanting to do, you must send an ajax call to a page that contains the information you're looking for, otherwise your output will be completely static.

Upvotes: 1

Related Questions