Javascript 如何基于下拉选择和按钮单击从MySql数据库检索数据?
我试图通过选择更改并单击按钮从MySql数据库检索一些数据。来自结果行的数据应该显示在隐藏div中的两个文本框中,该div在单击按钮时显示 empstatus.php empstatest.phpJavascript 如何基于下拉选择和按钮单击从MySql数据库检索数据?,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,我试图通过选择更改并单击按钮从MySql数据库检索一些数据。来自结果行的数据应该显示在隐藏div中的两个文本框中,该div在单击按钮时显示 empstatus.php empstatest.php 我是Jquery新手,ajax代码可能有问题,特别是我试图在输入字段中显示数据的地方。请帮我解决这个问题。我认为,问题是您已经将数据类型声明为JSON,但实际上您只是发送了一个字符串。如果您完全省略了数据类型,它可以工作,尽管处理响应和我不使用jQuery在没有进一步修改的情况下似乎无法工作-我添加了
我是Jquery新手,ajax代码可能有问题,特别是我试图在输入字段中显示数据的地方。请帮我解决这个问题。我认为,问题是您已经将数据类型声明为JSON,但实际上您只是发送了一个字符串。如果您完全省略了数据类型,它可以工作,尽管处理响应和我不使用jQuery在没有进一步修改的情况下似乎无法工作-我添加了JSON.parse数据 幸运的是,下面的演示将演示这些更改,并允许您让代码正常工作
<?php
/* this emulates the remote script ( empstatest.php ) */
if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();
/* payload to emulate db call */
$data=new stdClass;
$data->ordblock=sprintf( 'ord-%d-%s', $_POST['selectemp'], uniqid() ); #randomness abounds!
exit( json_encode( $data ) );
}
?>
<html>
<head>
<title></title>
<script src='//code.jquery.com/jquery-latest.js'></script>
<script>
let url=location.href;/*empstatest.php*/
$( document ).ready(function () {
( function($) {
$('#selectemp').change(function () {
$('#subemp').click(function(){
$('.hiddendiv').toggle();
$.ajax({
type:'POST',
url:url,
data: $('#selectemp').serialize(),
success:function( data ){
let json=JSON.parse( data );
$('#open').val( json.ordblock );
$('#opendel').val( json.ordblock );
}
});
});
});
})(jQuery);
});
</script>
</head>
<body>
<select name='selectemp' id='selectemp'>
<option selected hidden>Select
<?php
for( $i=1; $i<=20; $i++ )printf('<option value=%d>Option - %d',$i,$i);
?>
</select>
<button type='button' class='btn btn-lg btn-dark' id='subemp'>Search</button>
<div class='hiddendiv' style='display: none;'>
<label for='open'>Open Order: </label>
<input type='text' class='form-control' name='open' id='open'/>
<br>
<label for='open'>Open Delivery: </label>
<input type='text' class='form-control' name='opendel' id='opendel' />
</div>
</body>
</html>
你没有展示正在发生的事情,我们怎么知道出了什么问题?你得到了什么结果,你要把它送到哪里?但是有一件事对您没有帮助,那就是您的表单方法被设置为GET,并且您正在尝试访问POST,因此我怀疑您的变量是未分配的。首先,empstatest.php没有将任何内容发送回AJAX调用。添加一个echo$is;在最后一行之后,这就是我写的所有代码,先生。我没有输出。仅显示输入字段。添加echo$为;在empslatest.phpWhere的最后一行中,是您的jQuery代码,当您控制台记录响应时,您得到了什么输出?
<?php
session_start();
header('Content-type: application/json');
$db = mysqli_connect("credentials");
$value = $_POST['selectemp'];
$sql = "select ordblock, delblock from mx_crlimit where kunnr = '$value'";
$res = pg_query($db, $sql);
$is = '';
$data = pg_fetch_assoc($res);
$is = json_encode($data);
echo $is;
?>
<?php
/* this emulates the remote script ( empstatest.php ) */
if( $_SERVER['REQUEST_METHOD']=='POST' ){
ob_clean();
/* payload to emulate db call */
$data=new stdClass;
$data->ordblock=sprintf( 'ord-%d-%s', $_POST['selectemp'], uniqid() ); #randomness abounds!
exit( json_encode( $data ) );
}
?>
<html>
<head>
<title></title>
<script src='//code.jquery.com/jquery-latest.js'></script>
<script>
let url=location.href;/*empstatest.php*/
$( document ).ready(function () {
( function($) {
$('#selectemp').change(function () {
$('#subemp').click(function(){
$('.hiddendiv').toggle();
$.ajax({
type:'POST',
url:url,
data: $('#selectemp').serialize(),
success:function( data ){
let json=JSON.parse( data );
$('#open').val( json.ordblock );
$('#opendel').val( json.ordblock );
}
});
});
});
})(jQuery);
});
</script>
</head>
<body>
<select name='selectemp' id='selectemp'>
<option selected hidden>Select
<?php
for( $i=1; $i<=20; $i++ )printf('<option value=%d>Option - %d',$i,$i);
?>
</select>
<button type='button' class='btn btn-lg btn-dark' id='subemp'>Search</button>
<div class='hiddendiv' style='display: none;'>
<label for='open'>Open Order: </label>
<input type='text' class='form-control' name='open' id='open'/>
<br>
<label for='open'>Open Delivery: </label>
<input type='text' class='form-control' name='opendel' id='opendel' />
</div>
</body>
</html>