首页 > web前端 > js教程 > 如何在不重新加载页面的情况下提交表单?

如何在不重新加载页面的情况下提交表单?

Barbara Streisand
发布: 2024-12-22 02:23:11
原创
558 人浏览过

How to Submit Forms Without Page Reloading?

提交表单而不重新加载页面

您遇到了以下问题:在分类广告网站上提交表单会导致页面重新加载。这种行为是不可取的,因为它会破坏用户体验。要解决此问题,请考虑以下替代方法:

Iframe 重定向

在页面上创建一个不可见的 iframe,并将表单的 target 属性设置为 iframe 的名称。这会将表单的响应重定向到 iframe,从而防止页面重新加载。

<iframe name="votar">
登录后复制

服务器端脚本

您可以将表单提交到服务器-side 脚本,例如 PHP,它将处理消息发送并将用户重定向回当前页面,而无需

在 PHP 脚本 (tip.php) 中:

<?php
// Get the form data
$tip_email = $_POST['tip_email'];
$ad_id = $_POST['ad_id'];

// Send the tip email

// Redirect back to the current page without reloading
header('Location: ' . $_SERVER['HTTP_REFERER']);
?>
登录后复制

在您的表单中:

<form name="tip" method="post" action="tip.php">
    Tip somebody: 
    <input 
        name="tip_email"
        type="text" 
        size="30" 
        onfocus="tip_div(1);" 
        onblur="tip_div(2);"
    />
    <input type="submit" value="Skicka Tips" />
    <input type="hidden" name="ad_id" />
</form>
登录后复制

记住将“tip.php”替换为PHP 脚本的实际路径并相应地调整表单操作和重定向 URL。

以上是如何在不重新加载页面的情况下提交表单?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板