1

I'm having troubles in getting the current URL from which the filled form was send.

I want to use the same form for more sites.

This is my form code:

<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>

And PHP that sends emails. I need its $message to show URL, but I couldnt get it to work by calling $url

<?php

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

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

    $to = "[email protected]";
    $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.";
}

?>

Where am I going wrong?

5 Answers 5

1

a solution would be to also use:

$thisUrl=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['SERVER_NAME'].$_SERVER['SCRIPT_NAME'];
Sign up to request clarification or add additional context in comments.

2 Comments

for a more robust solution, you could also add the port $thisUrl = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . (empty($_SERVER['SERVER_PORT']) ? '' : ':' . $_SERVER['SERVER_PORT']) . $_SERVER['SCRIPT_NAME'];
Thanks, unfortunately this is useless since it passes url of my php not the site form was send from.
0

Here is a function that might help you :)

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;
}

It works with http and https and it will return you your current url.

2 Comments

Sorry, but how do I make an output to my email with this?
Just call this function for the value of your hiden field and then get it from the post array
0

In your code you have

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

You're trying to concat the http:// string with a comma (,) instead a dot (.)

Try this

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

Comments

0

Comma in your hidden input (after 'http://'). Must be:

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

Comments

0

I figured it out and simply used window.location.href

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.