简体   繁体   English

将值从JQUERY传递到PHP,然后以FORM输出到INPUT

[英]Pass values from JQUERY to PHP then output to INPUT in FORM

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 我需要从表单获取值,然后使用jquery将其发送到php,然后将结果输出到下拉选择菜单

get the value of using jquery 获取使用jquery的价值

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

send it to php and perform a query 将其发送到php并执行查询

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

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

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

my php file farm.php 我的php文件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 jQuery部分是如此混乱,这是我真正需要帮助的地方

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

    });
});

try this, it will help you. 试试这个,它将对您有帮助。

JQuery: jQuery的:

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

PHP: 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? 您的变量$ q的用途是什么?

Your jquery can be like : 您的jQuery可以像:

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

    });
});
$("#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);
    }

});

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM