0

After discovering why my site doesn't redirect by header command, I found another problem. The problem is that I want put some variables into url redirecting, that is in javascript. That's part of my code:

<?php
    $cost = 200
    $name = 'Bill'
    $url_endpoint = 'https://example.com/item1='.$cost.'&n='.$name.'.html';
    if( !headers_sent() ) {
        header("Location: $url_endpoint");
    }
    else {
?>

    <script type="text/javascript">
        document.location.href="$url_endpoint";   //That's just example what I want
    </script>
    Redirecting to <a href="$url_endpoint">site.</a> //Same here

<?php
    }
    die();
    exit;
   } // ?
?>

Any ideas/tips for beginner?

1
  • You will want to echo the php variable into the javascript. document.location.href='<?php echo $url_endpoint;?>'; Commented Nov 11, 2015 at 11:08

3 Answers 3

1

Your script just requires a slight amendment on the output of the variable into the Javascript.

You just need to remember that when you close off PHP using ?>, PHP variables are no longer accessible until you open PHP again with <?

You also had one too many closing brackets which could end up causing a PHP error.

<?php
    $cost = 200
    $name = 'Bill'
    $url_endpoint = 'https://example.com/item1='.$cost.'&n='.$name.'.html';
    if( !headers_sent() ){
        header("Location: $url_endpoint");
    }else{
?>
    <script type="text/javascript">
        document.location.href="<?php echo $url_endpoint;?>";
    </script>
    Redirecting to <a href="<?php echo $url_endpoint;?>">site.</a>
<?php
    die();
    exit;
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

It's just a part of code, that I forgot to correct. Anyway, thanks for help in this stupid question.
Its fine :). We all start somewhere
0

Use something like this

<script type="text/javascript">
   document.location.href="<?php echo $url_endpoint; ?>";
</script>

when you want to echo variable to html or in your case javascript. But you could find this on the first page of google search, without asking a question... Search a lot before asking.

Comments

0

Use as

<script type="text/javascript">
  document.location.href="<?php echo $url_endpoint"; ?>;   //That's just example what I want
  </script>
  Redirecting to <a href="<?php echo $url_endpoint"; ?>">site.</a>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.