0

i need to get a value from the a form then send it to php using jquery then output the result a dropdown select menu

get the value of using jquery

 <input id="search" name="search" type="text">

send it to php and perform a query

  <select  id="farmertype" name="farmertype" >

         <option value="" > - PLEASE SELECT FARM -</option>

         ////   output here as options                                      
  </select>

my php file farm.php

<?php
include_once("../init.php");
$q = ($_POST["search"]);

$db->query("SELECT * FROM farmers ");
  while ($line = $db->fetchNextObject()) {

      $idno = $line->idno;
      echo "<option value='$idno'>$idno</option>";

   }
 }

?>

the jquery part is so messy this is where i really need help

$("#search").click(function() {
    search = $(this).attr('#search');
    $.ajax({
        type: 'GET',
        url: 'farm.php',
        data: "#search=" + search,

    });
});
4
  • saw a question just like this earlier but from another account and was deleted. Commented Oct 29, 2015 at 13:59
  • @Fred-ii- yet this one managed to get 2 upvotes and 2 favourite!! Commented Oct 29, 2015 at 13:59
  • @Steve and they think they can truly hide ;-) hah - I guess some just can't take the pressue. Hm... upvotes,... from who's "account"? Commented Oct 29, 2015 at 14:00
  • 1
    thanks let me try it out Commented Oct 29, 2015 at 14:00

3 Answers 3

1

try this, it will help you.

JQuery:

$("#search").click(function() {
    search = $(this).val();
    $.ajax({
        type: 'POST',
        url: 'farm.php',
        data: {searchValue:search},
        success:function(result) {
            console.log(result);
        }
    });
});

PHP:

<?php
    include_once("../init.php");
    $q = ($_POST["searchValue"]);
    $db->query("SELECT * FROM farmers");
    $result = [];
    while ($line = $db->fetchNextObject()) {
        $idno = $line->idno;
        $result = "<option value='$idno'>$idno</option>";
    }
    print_r($result);
?>

what is the purpose of your variable $q?

Sign up to request clarification or add additional context in comments.

2 Comments

this one works. how do i output the result to a particular id in the form
$('#id').text(result[array_index]) if you need to replace a text or $('#id').val(result[array_index]) if you need to replace a value
0

Your jquery can be like :

$("#search").click(function() {
    search = $('#search').val();
    $.ajax({
        type: 'GET',
        url: 'farm.php',
        data: {search : search},
        success: function(html){
            alert(html);
        }

    });
});

1 Comment

$(this).attr('#search'); Whats that?? Also, the php file specifies a POST request (though it never uses it).
0
$("#search").click(function() { /* I think you should use keyUp or use click on a button, nobody clicks an input box */
var search = $(this).val();
$.ajax({
    method: 'POST', // 
    url: 'farm.php',
    data: {'search' : search},
    success: function(data){
        alert(data);
    }

});

});

1 Comment

the php file specifies a POST request (though it never uses it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.