PHP表单- post当前URL作为隐藏值


PHP form - post current URL as hidden value

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

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

这是我的表单代码:
<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,但是我不能通过调用$ 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.";
}
?>

我哪里错了?

一个解决方案是:

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

这里有一个函数可以帮到你:)

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,它会返回你当前的url

在你的代码中有

<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']; ?>" />

隐藏输入中的逗号(在'http://') "之后)。必须:

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

我明白了,只是使用window.location.href

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