简体   繁体   English

停留在当前页面上的HTML表单操作

[英]HTML Form action that stays on the current page

I'm making a newsletter signup, a user inputs their email and clicks the form submit button. 我正在进行简报注册,用户输入电子邮件并单击表单提交按钮。 The problem is that the newsletter signup is in my footer, which shows up on every page of the website. 问题在于新闻通讯注册位于我的页脚中,该页脚显示在网站的每个页面上。 Right now I have the action="newsletter.php" which sends me an email with the email they used to sign up. 现在,我有action =“ newsletter.php”,它向我发送了一封电子邮件,其中包含他们用来注册的电子邮件。 I could have a "window.location=""" but I wanted it to go back to the page they came from (because the footer could've been on any page of the website). 我本可以有一个“ window.location =“”“,但我希望它返回到其来源页面(因为页脚本可以位于网站的任何页面上)。

Now, I'm wondering if there's a way to NOT have the form action be "newsletter.php", and instead have the php just be inside the footer file, so that they never even leave the page they were on in the first place, a simple window.alert can just let them know their sign up was successful and they can keep doing what they were doing no matter what page of the website they were on. 现在,我想知道是否有一种方法可以使表单动作不为“ newsletter.php”,而是使php仅位于页脚文件中,以便他们甚至永远都不会离开他们所在的页面一个简单的window.alert可以让他们知道注册成功,并且无论他们在网站的哪个页面上,他们都可以继续做自己的工作。

After getting idea's from google and trying on my own, I tried making a javascript function that contained the php needed to send me an email with the email they type in but then it was obviously sending me a blank email every time any page of the website loaded. 在从Google获得想法并自行尝试之后,我尝试制作了一个javascript函数,其中包含所需的php,用于向我发送一封包含他们输入的电子邮件的电子邮件,但显然,每次网站的任何页面时,它都会向我发送一封空白电子邮件已加载。 I also tried doing "isset" but the problem with that is the action="" just reloads the page before the isset can check if they typed in an email. 我也尝试过执行“ isset”,但是问题是action =“”只是在isset可以检查是否在电子邮件中键入内容之前重新加载页面。

NEWSLETTER.PHP- 新闻通讯

   <?php
$field_emailInput = $_POST['emailInput'];

$mail_to = 'josh@cutephilosophy.com';
$subject = 'NEWSLETTER SUBSCRIBER';

$body_message .= 'This person subscribed for email updates: ';
$body_message .= 'E-Mail: '.$field_emailInput."\n";

$headers = 'From: '.$field_emailInput."\r\n";
$headers .= 'Reply-To: '.$field_emailInput."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        window.alert('You Are Now In The Loop!');
        //window.location = 'index.php';
        window.location ='history.back()';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Something broke, email josh@cutephilosophy.com about the problem!');
        window.location = 'index.php';
    </script>
<?php
}
?>

You'll notice I tried "history.back()" when using the action="newsletter.php" method, this works, but while the window.alert is shown you are on a complete white page instead of the page they were initially on, and then you are redirected back. 您会注意到我在使用action =“ newsletter.php”方法时尝试了“ history.back()”,此方法有效,但是当显示window.alert时,您处于完整的白页,而不是最初的页面上,然后您被重定向回。

Cant you use PHP_Self. 不能使用PHP_Self。 Or add in a back button to your html to allow them to click back to where they were. 或在HTML中添加后退按钮,以允许他们单击返回到原来的位置。

Use php mail() to send the email and set the form action to 使用php mail()发送电子邮件并将表单操作设置为

htmlspecialchars($_SERVER['REQUEST_URI'])

The htmlspecialcharacters part adds security to your form, while the server part returns you to the same page. htmlspecialcharacters部分为表单增加了安全性,而server部分使您返回到同一页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM