首页 > 解决方案 > 如何将以下提交按钮代码重定向到同一页面,如果可能的话不重新加载

问题描述

我创建了一个在 WordPress 网站弹出窗口中运行的代码,该代码的目的是验证 mysql db 表中可用的输入数据(如果可用)它显示相关代码,否则它执行其他代码。

我努力了

 action="<?php echo $_SERVER['PHP_SELF']; ?>"

headlocation(.......) 

但没有一个工作,它正在提交代码并重定向index.php到显示结果。

<style><?php include 'search-zip.css'; ?>
</style>

<?php 

include 'search-config.php';
$conn = OpenCon();
?>
<h3 class="formhead">Available in</h3>
<h2 class="formhead">Melbourne Inner Suburbs</h2>
<form class="form-wrapper"  action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="$POSTCODE" id="search" size="25" maxlength="35" value="<?php echo $_REQUEST['$POSTCODE'] ?>" />
<input type="submit"
name="submit" value="Search" id="submit"></form>
<p class="form-text">Please search your postal code for service availability</p>

<?

if (isset($_POST['submit'])) {

{
   $zipcode=$_REQUEST['$POSTCODE'];
   $sql = "SELECT * FROM service_location WHERE zipcode = $zipcode";
$result = $conn->query($sql);



if ($result->num_rows > 0) {
    ?>
    <style>.formhead{display:none;} .form-wrapper{display:none;} .form-text{display:none;}</style>
<div class="available">
<img class="zipimage" src="https://www.hastygrocer.com.au/wp-content/uploads/2019/07/hasty-grocer-available-popup-image.png" alt="Happy Minion" >
<h2 style="text-align: center;">Experience Melbourne's</h2>
<h2 style="text-align: center;"><i style="color: #3aafa9; font-family: Faster One script=all rev=2; font-size: 60px;">Fast</i> &amp; <b style="color: green; font-family: Aladin; font-size: 60px;">Fresh </b></h2>
<h2 style="text-align: center;">Grocery Delivery</h2> 
<span class="pum-close popmake-close "><button  class="zipbutton" type="button">Shop</button></span></div>

    <?

} else {

?>
<style>.formhead{display:none;} </style>
<div class="available">
   <h3 style="text-align: center;">Get Notified When Available in <b style=" text-align: center;font-weight: 800; font-family: arial; color: #3aafa9;"><?php echo "$zipcode"?></b></h3> 
<div class="zipform"><?php echo do_shortcode( '[contact-form-7 id="613" title="PopUp"]' ); ?></div>
</div>
<?
}
}}

?>

我创建了此代码以重定向到同一页面。

但这是重定向index.php到显示结果。

请帮助我使用任何 php 修复此问题,或者ajax可以修复此代码。

抱歉,我不是专家开发人员,我从 Internet 获取代码并创建了以下代码。

提前致谢

标签: phpjquerymysqlajaxwordpress

解决方案


请参阅这部分代码:action="<?php echo $_SERVER['PHP_SELF']; ?>"

提交将执行到从 $_SERVER 数组传递的链接,但 PHP 手册说:

$_SERVER 是一个包含标题、路径和脚本位置等信息的数组。此数组中的条目由 Web 服务器创建。无法保证每个 Web 服务器都会提供其中任何一个。

尝试制作<? echo $_SERVER['PHP_SELF']; ?>并查看打印的内容。您可能会得到 index.php 的位置。

尝试将操作设置为包含表单的页面。


推荐阅读