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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化