英文:
echo value on page posted by ajax
问题
I want to make a web page taking a number as input and search its name in the database, then print the name on the page (without reloading). But I cannot update the span. Ajax response returns HTML of the page.
The code in pra.php:
if (isset($_POST['sk']))
{
$sk = $_POST['sk'];
$formatted= '\''.$sk.'\'';
$ka='SELECT itemName FROM '.$TT.' WHERE itemCode='.$formatted;
$view=odbc_exec($conn,$ka);
$Resultenter=odbc_Result($view,1);
}
?>
<form id="kod" class="" action="" method="post">
<input name="sk" type="text" class="form-control">
<span><?php if($Resultenter!=null) {echo $Resultenter;}?></span>
</form>
<script>
$(document).ready(function() {
$('input[name=sk]').change(function() {
var skValue = $(this).val();
$.ajax({
type: 'POST',
url: 'pra.php',
data: { sk: skValue },
success: function(response) {
console.log('skValue:', skValue);
console.log('Received response:', response );
// handle success response
},
error: function(xhr, status, error) {
console.log('AJAX request error:');
console.log('Status:', status);
console.log('Error:', error);
}
});
});
});
</script>
英文:
I want to make a web page taking a number as input and searchs its name on the database,then prints name on the page (without reloading ). But I cannot update span.Ajax response returns html of the page.
the code pra.php
<php
if (isset($_POST['sk']))
{
$sk = $_POST['sk'];
$formatted= '\''.$sk.'\'';
$ka='SELECT itemName FROM '.$TT.' WHERE itemCode='.$formatted;
$view=odbc_exec($conn,$ka);
$Resultenter=odbc_Result($view,1);
}
?>
<form id="kod" class="" action="" method="post">
<input name="sk" type="text" class="form-control">
<span><?php if($Resultenter!=null) {echo $Resultenter;}?></span>
</form>
<script>
$(document).ready(function() {
$('input[name=sk]').change(function() {
var skValue = $(this).val();
$.ajax({
type: 'POST',
url: 'pra.php',
data: { sk: skValue },
success: function(response) {
console.log('skValue:', skValue);
console.log('Received response:', response );
// handle success response
},
error: function(xhr, status, error) {
console.log('AJAX request error:');
console.log('Status:', status);
console.log('Error:', error);
}
});
});
});
</script>
答案1
得分: 0
需要在Ajax返回时更新innerHTML。
假设Ajax响应是您要放入span标签内的实际数据,然后:
- 给span添加一个ID,因此,请将以下内容更改为:
<span id="returnvalue"><?php if($Resultenter!=null) {echo $Resultenter;}?></span>
- 创建一个单独的PHP文件(例如pra2.php)如下:
<?php
if (isset($_POST['sk'])){
// ... 用于检索数据并存储在$Resultenter中的语句
echo $Resultenter;
}
?>
- 告诉系统更新span的innerHTML,因此,请将以下代码块更改为:
$.ajax({
type: 'POST',
url: 'pra2.php',
data: { sk: skValue },
success: function(response) {
document.getElementById("returnvalue").innerHTML = response;
},
});
最后但同样重要的是,请将您的查询更改为参数化的准备语句,以防止SQL注入攻击。
英文:
You need to update the innerHTML on ajax return.
Assuming the ajax response is the actual data you want to put inside the span tags, then
- Add an ID to the span, hence, change
<span><?php if($Resultenter!=null) {echo $Resultenter;}?></span>
to
<span id=returnvalue><?php if($Resultenter!=null) {echo $Resultenter;}?></span>
- Create a separate php (e.g. pra2.php) like
<php
if (isset($_POST['sk'])){
.... statements to retrieve the data into $Resultenter
echo $Resultenter;
}
?>
- tell the system to update the span innerHTML, hence, change the block
$.ajax({
type: 'POST',
url: 'pra.php',
data: { sk: skValue },
success: function(response) {
console.log('skValue:', skValue);
console.log('Received response:', response );
// handle success response
},
to
$.ajax({
type: 'POST',
url: 'pra2.php',
data: { sk: skValue },
success: function(response) {
document.getElementById("returnvalue").innerHTML=response;
},
Last but not least, please change your query to parameterized prepared statement which is resilient against SQL injection.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论