Javascript AJAX调用不替换HTML

Javascript AJAX调用不替换HTML,javascript,php,ajax,forms,Javascript,Php,Ajax,Forms,我有一个表单,可以根据用户的邮政编码输入计算运费。我正在检索用户的邮政编码文本输入,检索通过PHP发送该邮政编码的成本,然后使用AJAX调用将其传输回HTML中的输出,总计。然而,AJAX调用并没有取代HTML (相关)HTML: PHP: <?php $postcode = (is_numeric($_GET['postcode']) ? (int)$_GET['postcode'] : 0); if ($postcode >= 2000 && $p

我有一个表单,可以根据用户的邮政编码输入计算运费。我正在检索用户的邮政编码文本输入,检索通过PHP发送该邮政编码的成本,然后使用AJAX调用将其传输回HTML中的输出,总计。然而,AJAX调用并没有取代HTML

(相关)HTML:

PHP:

<?php

   $postcode = (is_numeric($_GET['postcode']) ? (int)$_GET['postcode'] : 0);

   if ($postcode >= 2000 && $postcode <= 2234) {
     $shipping = 55.00;
   } elseif ($postcode >= 2250 && $postcode <= 2310) {
     $shipping = 105.00;
   }

    echo $shipping;
?>
<?php

   $postcode = (is_numeric($_GET['postcode']) ? (int)$_GET['postcode'] : 0);

   if ($postcode >= 2000 && $postcode <= 2234) {
     $shipping = 55.00;
   } elseif ($postcode >= 2250 && $postcode <= 2310) {
     $shipping = 105.00;
   }

    echo $shipping;
?>

  • 如果我在控制台中键入msg,它将返回undefined。它不应该有一个值(邮政编码输入不在正确的条件范围内)
通过使用:

$postcode = (is_numeric($_POST['postcode']) ? (int)$_POST['postcode'] : 0);
以及:


您正在使用GET通过查询字符串发送post代码,但您的PHP代码正在尝试从post正文中读取值。

您的ajax正在发送GET,而您的PHP正在读取$\u post,请尝试以下操作:


将该行更改为:

$postcode = (is_numeric($_GET['shipping']) ? (int)$_GET['shipping'] : 0);

id为“result”的HTML元素在哪里?是否尝试了警报(msg)以确保有msg?
$.ajax{ type: "GET", ... }
<?php

   $postcode = (is_numeric($_GET['postcode']) ? (int)$_GET['postcode'] : 0);

   if ($postcode >= 2000 && $postcode <= 2234) {
     $shipping = 55.00;
   } elseif ($postcode >= 2250 && $postcode <= 2310) {
     $shipping = 105.00;
   }

    echo $shipping;
?>
$postcode = (is_numeric($_GET['postcode']) ? (int)$_GET['postcode'] : 0);
$postcode = (is_numeric($_GET['shipping']) ? (int)$_GET['shipping'] : 0);