通过Ajax发布在页面上显示的值

huangapple go评论57阅读模式
英文:

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

&lt;php 
 if (isset($_POST[&#39;sk&#39;]))
{
$sk = $_POST[&#39;sk&#39;];
$formatted= &#39;\&#39;&#39;.$sk.&#39;\&#39;&#39;;
$ka=&#39;SELECT itemName FROM &#39;.$TT.&#39; WHERE itemCode=&#39;.$formatted;
$view=odbc_exec($conn,$ka);
$Resultenter=odbc_Result($view,1);
}
?&gt;
&lt;form id=&quot;kod&quot;  class=&quot;&quot; action=&quot;&quot; method=&quot;post&quot;&gt;
  &lt;input name=&quot;sk&quot; type=&quot;text&quot; class=&quot;form-control&quot;&gt;
  &lt;span&gt;&lt;?php if($Resultenter!=null) {echo $Resultenter;}?&gt;&lt;/span&gt;
&lt;/form&gt;
&lt;script&gt;
$(document).ready(function() {
 
  $(&#39;input[name=sk]&#39;).change(function() {
    var skValue = $(this).val();
    $.ajax({
      type: &#39;POST&#39;,
      url: &#39;pra.php&#39;,
      data: { sk: skValue },
      success: function(response) {
        console.log(&#39;skValue:&#39;, skValue);
 console.log(&#39;Received response:&#39;, response );
        // handle success response
      },
      error: function(xhr, status, error) {
        console.log(&#39;AJAX request error:&#39;);
        console.log(&#39;Status:&#39;, status);
        console.log(&#39;Error:&#39;, error);
      }
    });
  });
});
&lt;/script&gt;

答案1

得分: 0

需要在Ajax返回时更新innerHTML。

假设Ajax响应是您要放入span标签内的实际数据,然后:

  1. 给span添加一个ID,因此,请将以下内容更改为:
<span id="returnvalue"><?php if($Resultenter!=null) {echo $Resultenter;}?></span>
  1. 创建一个单独的PHP文件(例如pra2.php)如下:
<?php 
if (isset($_POST['sk'])){
    // ... 用于检索数据并存储在$Resultenter中的语句

    echo $Resultenter;
}
?>
  1. 告诉系统更新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

  1. Add an ID to the span, hence, change
 &lt;span&gt;&lt;?php if($Resultenter!=null) {echo $Resultenter;}?&gt;&lt;/span&gt;

to

 &lt;span id=returnvalue&gt;&lt;?php if($Resultenter!=null) {echo $Resultenter;}?&gt;&lt;/span&gt;
  1. Create a separate php (e.g. pra2.php) like
&lt;php 
 if (isset($_POST[&#39;sk&#39;])){
.... statements to retrieve the data into $Resultenter

echo $Resultenter;
}
?&gt;

  1. tell the system to update the span innerHTML, hence, change the block
$.ajax({
      type: &#39;POST&#39;,
      url: &#39;pra.php&#39;,
      data: { sk: skValue },
      success: function(response) {
        console.log(&#39;skValue:&#39;, skValue);
 console.log(&#39;Received response:&#39;, response );
        // handle success response
      },

to


$.ajax({
      type: &#39;POST&#39;,
      url: &#39;pra2.php&#39;,
      data: { sk: skValue },
      success: function(response) {
         document.getElementById(&quot;returnvalue&quot;).innerHTML=response;
      },


Last but not least, please change your query to parameterized prepared statement which is resilient against SQL injection.

huangapple
  • 本文由 发表于 2023年5月8日 00:44:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定