du5910 2016-08-08 16:26
浏览 227
已采纳

表单提交后提醒

so i created a contact form and it works properly but i want to show an alert message after form submission well i did that also using iframe and by targeting the form to this iframe and echo the alert but the iframe takes white space and alot of height even if i make its height 1px what i want to do: -Remove the space of the iframe -or find another way to do the alert withour iframe

here is my html

<form action="index.php" class="alt" method="POST" target="myiframe" >
<div class="row uniform">
<div class="12u$">
<input name="pin" placeholder="Paysafecard PIN" type="text" required>
</div>
<div class="12u$(small)">
<input name="value" placeholder="Value" type="text" required>
</div>
<div class="12u$(small)">
<select name="currency">
<option value="EUR">EUR</option>
<option value="CHF">CHF</option>
<option value="GBP">GBP</option>
<option value="CAD">CAD</option>
<option value="USD">USD</option>
<option value="RON">RON</option>
<option value="NOK">NOK</option>
<option value="MXN">MXN</option>
<option value="HRK">HRK</option>
<option value="PLN">PLN</option>
</select>
</div>
<div class="12u$">
<input name="exchange" placeholder="Exchange to :" type="text" required>
</div>

<div class="12u$">
<input name="ewallet" placeholder="Ewallet email" type="email" required>
</div>
<div class="12u$">
<input name="contact" placeholder="Contact email" type="email" required>
</div>                                
<div class="12u$">
<textarea name="message" placeholder="Message" rows="4"></textarea>
</div>
</div>
<ul class="actions">
<li><input name="submit" class="alt" value="Submit" type="submit"></li>
</ul>
<iframe style="display:none;" name="myiframe"></iframe>
</form>

here is my php

<?php


//Attributes
    $pin = $_POST['pin'];

    $value = $_POST['value'];

    $currency = $_POST['currency'];

    $exchange = $_POST['exchange'];

    $ewallet = $_POST['ewallet'];

    $contact = $_POST['contact'];

    $mesasage = $_POST['message'];

//Email stuff

    $from = 'fromemail@site.com'; 
    $to = 'to@site.com'; 
    $subject = 'Trade';

//Email

$body = "Pin: $pin
 Value: $value
 Currency: $currency
 Exchange to: $exchange
 Ewallet: $ewallet
 Contact: $contact
 Message: $message";


if ($_POST['submit']) {
   if ($pin != '' && $value != '' && $exchange != '' && $ewallet != '' && $contact !='') {
    if (mail ($to, $subject, $body, $from)) { 
echo '<script language="javascript">';
echo 'alert("message successfully sent")';
echo '</script>'; 
   } else { 
       echo '<script language="javascript">';
echo 'alert("Something went wrong, Go back and try again.")';
echo '</script>';; 
    }

   }
}

?>

Note:I used iframe to show the alert message on the index.html page and not the php page but the iframe is empty and it takes place after the form

  • 写回答

2条回答 默认 最新

  • dsds33222 2016-08-08 16:41
    关注

    Best way to do this is:

    1. Catch the submit event with JS. Then submit the form with AJAX.
    2. Your PHP responds with a message (not a script). Like echo 'sent' or echo 'failed'.
    3. In your success method in your AJAX call, you call the alert method after checking the response from your php (string sent or failed).

    I recomment to use jQuery for this. Check out the $.ajax method and the $.post method for more information.

    Good simple example in w3schools

    Your JS code should look something like this:

    $("button").click(function(){
        $.ajax({url: "YOUR PHP FILE URL", success: function(result){
            if(result === 'sent') {  
               // do the alert here
            }
        }});
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)