1

I have an ajax search on my site that fills in an HTML textbox with the results of the search. That works nicely. But what I want to do is display the text of the article within the textbox but pass the id of the article to $_POST to be saved into the database.

The JS I am using is:

<script type="text/javascript">
    $(function(){
    $(".search").keyup(function() 
    { 
    var searchid = $(this).val();
    var dataString = 'search='+ searchid;
    if(searchid!='')
    {
            $.ajax({
            type: "POST",
            url: "search.php",
            data: dataString,
            cache: false,
            success: function(html)
            {
            $("#result").html(html).show();
            }
            });
    }return false;    
    });

    jQuery("#result").live("click",function(e){ 
            var $clicked = $(e.target);
            var $name = $clicked.find('.name').html();
            var decoded = $("<div/>").html($name).text();
            $('#searchid').val(decoded);
    });
    jQuery(document).live("click", function(e) { 
            var $clicked = $(e.target);
            if (! $clicked.hasClass("search")){
            jQuery("#result").fadeOut(); 
            }
    });
    $('#searchid').click(function(){
            jQuery("#result").fadeIn();
    });
    });
</script>

My search.php file looks like this:

   <?php

    if($_POST)
    {
        $q = $_POST['search'];
        $sql_res = mysql_query("SELECT id,title,author,DATE_FORMAT(added,'%b %e, %Y') AS added FROM news WHERE title LIKE '%$q%' ORDER BY id LIMIT 5");

        while($row=mysql_fetch_array($sql_res))
        {
            $title = $row['title'];
            $author = $row['author'];
            $added = $row['added'];
    ?>

    <div class="show">
        <span class="name"><?php echo $title; ?></span>&nbsp;<br/><span class="showdate">Addded: <?php echo $added; ?></span>
    </div>

    <?php
        }
    }
    ?>

and finally the HTML looks like this:

<div class="block">
<label>master article</label> 
    <input type="text" name="MasterID" maxlength="16" id="searchid" class="search" placeholder="Search for the master article">
</div>

<div id="result"></div> 

The problem is that the textbox displays the title, which is great, but I wish to post the id. I feel so close, and thought I could perhaps work with a hidden input type, but am unsure how to populate it.

Thank you in advance for any assistance!

Mike250
  • 95
  • 10
  • What id do you want to send when? – Andresch Serj May 09 '14 at 08:37
  • 1
    in while loop create a new object for id like this $id = $row['id']; and in your view your can echo () and store in hidden field. where are you stuck in this ? – Dave May 09 '14 at 08:38
  • Ah yes,I would need to append my PHP in search.php to set the variable $articleid = $row['id']; and therefore send $articleid – Mike250 May 09 '14 at 08:39
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 28 '15 at 12:05

1 Answers1

0

So what I did was this. Added to search.php the article id variable:

$artid = $row['id']; 

and changed the div show block to:

<div class="show" align="left">
<span class="name"><?php echo $artid; ?></span> - <span class="showname"><?php echo $title; ?></span>&nbsp;<br/><span class="showdate">Addded: <?php echo $added; ?></span>
</div> 

because the Ajax was populating the textbox with whatever was within the span class="name".

This will work for me. Thanks for the ideas guys!

Mike250
  • 95
  • 10