Question

My site has 6 main pages. Five of those pages are files that end in .php (e.g., home.php) and are accessed by simply clicking on a menu bar's links ( a href ), however one of the pages (video.php) can be different based on user choices and is accessed using a form and two unique id's (please forgive all the escaping characters...this was how I was taught). User images in a WHILE-generated gallery are clicked to access this page:

<?php 
//Lots of code then
echo "<form action=\"video.php?id1=".$row['id1']."&id2=".$row['id2']."\"     
method=\"post\">
<input type=\"image\" src=\"".$row['image']."\"></form>";
?>


<body>
<!--Lots of lines then every page has this form-->
<form action="#" method="post" id="MyForm"><input type="hidden" name="count" 
value="somenumber"><button type="submit" name="action">Click</button></form>
<!--More lines-->
</body>

For each page on the site, for page reloads, the user (after POST-ing some input and this input is processed) returns to the current page like this:

$CurrentPage = 'anypage.php';

echo "<SCRIPT>
alert('This is a message');
location = '$CurrentPage';
</SCRIPT>";

For one of my site's features, video.php and other pages must be distinguished...the following code works to distinguish between video.php and the other pages and returns the user correctly to whichever location of the site he/she was on after the reload occurs:

$id1 = $_GET['id1'];//only on video.php page
$id2 = $_GET['id2'];//only on video.php page
$CurrentPage = 'anypage.php';

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

$result = $_POST['count'];
//lots of code then
$Message = ($result > 1) ? 'Here is a message!' : ''; 

if (!$id2) {
    echo "<SCRIPT>
    alert('".$Message." Here is the rest of the message');
    location='$CurrentPage';
    </SCRIPT>";
}
else {
    echo "<SCRIPT>
    alert('".$Message." Here is the rest of the message');
    location='video.php?id1=$id1&id2=$id2';
    </SCRIPT>";
}
}//Endif

On pages that $id2 does not exist, the user is returned to $CurrentPage, but where it does exist (on the video.php page) the user is returned there with the correct id1 and id2 to properly generate the page.

However, I thought I would do one better and use a ternary operator with location as follows (none of my other pages has the word "video" in the file name, hence the utility of the strpos function here):

$id1 = $_GET['id1'];//only on video.php page
$id2 = $_GET['id2'];//only on video.php page
$CurrentPage = 'anypage.php';

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

$result = $_POST['count'];
//lots of code then
$Message = ($result > 1) ? 'Here is a message!' : ''; 

$Loc = (strpos($CurrentPage, 'video') === false) ? $CurrentPage : 'video.php?
id1=$id1&id2=$id2';

    echo "<SCRIPT>
    alert('".$Message." Here is the rest of the message');
    location='$Loc';
    </SCRIPT>";
}//Endif

This seemingly equivalent code returns the user to each page correctly EXCEPT video.php, as it cannot find the appended id1 and id2 using this method (the user is returned to the shell of video.php...everything is there except what is generated by id1 and id2).

Why is the ternary operator failing here?

Was it helpful?

Solution

How about this?

$id1 = $_GET['id1'];//only on video.php page
$id2 = $_GET['id2'];//only on video.php page
$CurrentPage = 'anypage.php';

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

$result = $_POST['count'];
//lots of code then
$Message = ($result > 1) ? 'Here is a message!' : ''; 

$Loc = (strpos($CurrentPage, 'video') >= 0) ? 'video.php?id1=$id1&id2=$id2' : $CurrentPage;

    echo "<SCRIPT>
    alert('$Message. Here is the rest of the message');
    location='$Loc';
    </SCRIPT>";
}//Endif

Otherwise, use this:

$id1 = $_GET['id1'];//only on video.php page
$id2 = $_GET['id2'];//only on video.php page
$CurrentPage = 'anypage.php';

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

$result = $_POST['count'];
//lots of code then
$Message = ($result > 1) ? 'Here is a message!' : ''; 

$Loc = ($CurrentPage === 'video.php') ? 'video.php?id1=$id1&id2=$id2' : $CurrentPage;

    echo "<SCRIPT>
    alert('$Message. Here is the rest of the message');
    location='$Loc';
    </SCRIPT>";
}//Endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top