Question

my code is:

$movie_id = $_GET['like'];
$temp = "www.hifff.se/details.php?id=" . $movie_id";
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Du får bara lajka en gång per film och dygn. Lajka gärna en annan film eller se filmen igen och återkom imorgon!')
window.location.href='" . $temp . "';
</SCRIPT>");

im assuming my setting of $temp is wrong. and therefor not working. but i dont understand what im doing wrong when setting it. i also heard that this wont work in chrome? and that this:

 setTimeout(function(){document.location.href = "page.html;"},500);

should. i dont know how i should apply it to my code either. can someone be kind enough to help me?

Was it helpful?

Solution 2

$temp = "http://www.hifff.se/details.php?id=" . $movie_id"";

You have to add http to your URL in order to perform a correct redirection

OTHER TIPS

Let's analyze your code together. First line:

$movie_id = $_GET['like'];

As you are coding, you learn different design patterns and best practices when you progress. My advice for you is to program defensively. This piece of code might work in the start, but can break easily. Use the isset function to check whether the GET variable exists (http://www.php.net/isset).

$temp = "www.hifff.se/details.php?id=" . $movie_id";

You are using PHP's magic quotes. Magic quotes allow variables inside them and other special characters. PHP will automatically parse them for you, so there's no need to use a concatenation (and the codeblock is even wrong, see quote on the end of the sentence):

$temp = "www.hifff.se/details.php?id=$movie_id"; // this works.

NOTE: Just found out that magic quotes are deprecated since PHP 5.4. Don't use them if you think you don't need it.

echo ("<SCRIPT LANGUAGE='JavaScript'>

The language attribute has been deprecated since long.. use type="text/javascript" instead. This will work, because most new browser use javascript anyway in their <script> tags.

Also, it is a general practice to use " double quotes for tag attributes. If anyone looks in the source code, then you have single quotes and double quotes everywhere in your HTML. Details, my friend..This will be the result:

echo ('<script type="text/javascript">

In the following line, you are calling the window object. This object is already in the global scope, so unless you have overwritten this, you don't need to write window. anymore. You are also missing a ; on the end of the alert.

alert('Du får bara lajka en gång per film och dygn. Lajka gärna en annan film eller se filmen igen och återkom imorgon!');

Finally, we are at the end. Again, window is already this, thus it is unnecessary to write the whole thing again. Less is more. You also forget to append http:// to your temp variable. Notice that you use the full URL in this situation; it's a better practice to write the location within the website.

You also don't need all those quotes, because the echo uses magic quotes, thus exiting the quotes and concatenating the variable is not necessary in this case.

location.href='$temp';

Last comment: you are echoing everything in one line.. Does it look organized/neat to you? I think not, it's better to split everything in tiny nice bits, instead of just dumping everything in one line. It will look better and more structured. Any errors you get will be much easier to find. When your code is used or seen by others, it is best to maintain a programming style: http://en.wikipedia.org/wiki/Programming_style.


I would write the code like this:

if (isset($_GET['like'])) {
    $temp = "www.hifff.se/details.php?id=" . $_GET['like'];
    $text = 'Du får bara lajka en gång per film och dygn. Lajka gärna en annan film eller se filmen igen och återkom imorgon!';

    echo    '<script type="text/javascript">'
          .     "alert($text); location.href='$temp';"
          . '</script>'
          ;
}

Replace this line

$temp = "www.hifff.se/details.php?id=" . $movie_id";

with

$temp = "www.hifff.se/details.php?id=" . $movie_id;

Remove doublequotes at the end of the line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top