dongzouhe9734 2014-05-02 21:22
浏览 126
已采纳

使用PHP和Javascript更改输入值

I'm trying to create that simple email subscription form for my website. Every time an email is added, I'd like the input box to confirm that the email was added. It seems like something very simple but I can't find the way to echo an answer in the input box dynamically. I know how to change the input value with Javascript but it doesn't seem to work once included in the PHP.

Edit: I'm not very familiar with PHP. I know I'm not supposed to include Javascript in my function, it is just an example of what I want to achieve.

Is there an easier way to do that?

Thanks!

    <div id="email_form">
    <form action="" method="post">
    <input name="email" class="email" type="text" placeholder="Subscribe" id="emailinput"><button type="submit" name="SubmitButton" class="btn_email"><b>&gt;</b></button>
    </form>
    </div>


    <?php    
if(isset($_POST['SubmitButton'])){ //check if form was submitted

$to = "myemail@email.com";
$from = "no-reply@email.com";

$headers = "From: " . $from . "
";

$subject = "New subscription";
$body = "New user subscription: " . $_POST['email'];


if( filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) )
{ 
    if (mail($to, $subject, $body, $headers, "-f " . $from))
    {
        echo '<script type="text/javascript">';
        echo 'var elem = document.getElementById("emailinput");';
        echo 'elem.value = "Submited!";';
        echo '</script>';   
    }
    else
    {
       echo 'It works but There was a problem with your e-mail (' . $_POST['email'] . ')';   
    }
}
else
{
   echo 'It kinda works but there was a problem with your e-mail (' . $_POST['email'] . ')';   
}

}    
?>
  • 写回答

2条回答 默认 最新

  • dongmi9494 2014-05-02 21:42
    关注

    Do your processing of the submission before you echo the form. Then if the email was processed successfully, when you echo the input box, echo your input with the attribute value="Success!"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)