如何从php获取值以使用ajax请求输入 [英] How to get value from php to input using ajax request
问题描述
您好我的问题是如何使用带有onclick事件的ajax从PHP脚本中获取值。
Hi My question is how can I get the value from php script using an ajax with a onclick event.
我有一个文本字段和一个按钮
I have a text field and a button
<button type="button" class="btn btn-primary" onclick="getid(this)">Generate ID</button>
<input type="text" name="pin" class="form-control" readonly>
这是我的php脚本名为getrowcount.php
And here is my php script named getrowcount.php
include_once 'conx.php';
$query ="SELECT * FROM patientprofile";
$result = $DBcon->query($query);
$count = $result->num_rows;
if ($result) {
if($count >= 0){
$count_res = $count += 1;
$idnum = $count_res;
$test = str_pad($idnum, 5, "0", STR_PAD_LEFT);
}
}
现在我的问题是如何获得价值从 $ test
并使用ajax将其放在输入文本字段中。
And now my problem is how can I get the value from $test
and put it in the input text field using ajax.
推荐答案
您可以使用AJAX在输入字段中显示查询的输出。
You can use AJAX to display the output from the query in the input field.
步骤1:将此行代码添加到 getrowcount.php
:
Step 1: Add this line of code to the bottom of getrowcount.php
:
echo $test;
第2步:修改HTML,使其如下所示:
Step 2: Amend your HTML so that it looks like this:
<form id="get">
<input type="text" id="pin" name="pin" class="form-control" readonly>
<input type="submit" class="btn btn-primary" value="Generate ID">
</form>
步骤3:将此脚本添加到页面底部。
Step 3: Add this script to the bottom of the page.
<script>
$(document).ready(function(){
$("form#get").submit(function(event) {
event.preventDefault();
var input = $("#pin");
$.ajax({
type: "POST",
url: "getrowcount.php",
success: function(data) { input.val(data); }
});
});
});
</script>
这篇关于如何从php获取值以使用ajax请求输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!