PHP表单 - 将当前URL作为隐藏值发布

时间:2014-07-22 13:41:09

标签: php email

我在获取填写表单的当前网址时遇到了麻烦。

我想为更多网站使用相同的表单。

这是我的表单代码:

<form method="post" action="send_c_box.php">
    <input type="text" class="textbox" name="meno" value="meno" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'meno a priezvisko';}">
    <input type="text" class="textbox" name="email" value="email" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'e-mail';}">
    <input type="hidden" name="current_url" value="<?php echo'http://',$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>"  readonly/>
    <div class="clear"></div>
    <div>
        <textarea value="Vaša správa:" name="mytext" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Vaša správa...';}">Vaša správa...</textarea>
    </div>
    <span><input type="submit" name="submit" class="" value="Odoslať"></span>
</form>

发送电子邮件的PHP。我需要显示网址的$ message,但我无法通过调用 $ url

来使其工作
<?php

header( "refresh:3;url=index.html" );

if(isset($_POST['submit'])) {

    $to = "szabo@atria.sk";
    $from = $_POST['email'];
    $first_name = $_POST['meno'];
    $url = $_POST['current_url'];

    $subject = "Správa z kontaktného formuláru";
    $subject2 = "Potvrdenie o odoslaní formuláru";
    $message = $first_name . " napísal vo formulári:" . "\n\n" . $_POST['mytext'] . "from" . $url;
    $message2 = "Dakujeme za odoslanie formuláru. V krátkom case Vás kontaktujeme.";
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;

    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2);

    echo "Dakujeme za odoslanie vyplneného formuláru " . $first_name . ", za okamih budete presmerovaný spät.";
}

?>

我哪里错了?

5 个答案:

答案 0 :(得分:1)

解决方案也是使用:

$thisUrl=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];

答案 1 :(得分:0)

这是一个可以帮助你的功能:)

function get_current_url() {
  $pageURL = 'http';
  if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
     $pageURL .= "://";
  if ($_SERVER["SERVER_PORT"] != "80") {
     $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
  } else {
     $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
  }
  return $pageURL;
}

它适用于http和https,它会返回您当前的网址。

答案 2 :(得分:0)

在您的代码中

<input type="hidden" name="current_url" value="<?php echo'http://',$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>"  readonly/>

你试图用逗号(,)代替点(。)

来连接http://字符串

试试这个

<input type="hidden" name="current_url" value="<?php echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; ?>" />

答案 3 :(得分:0)

隐藏输入中的逗号(在&#39; http://&#39;之后)。 必须是:

    <input type="hidden" name="current_url" value="<?php echo 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];?>"  readonly/>

答案 4 :(得分:0)

我想通了,只是使用 window.location.href

<input type='hidden' id='currentId' name="current_url">
<script type="text/javascript">
document.getElementById('currentId').value=window.location.href ; 
</script>
相关问题