Php - alert box


I’ve looking for this on the web and haven’t found a clear explanation for my question.

I have 2 pages, user select a story on the first page. Second page (using post method) receives the story value(there are only 3 options) and does something.

If a user opens the second page without choosing a story, I want to pop an alert box saying user needs to choose a story first and redirect user to the first page.

here is my php code

$story = $_POST["stories"];
if (($story!= "story1.txt") && ($story!="story2.txt") && ( $story!="story3.txt")){
 echo'<script>alert("You need to choose a story first, please go to Story Selection");window.location.href= "StorySelection.php";</script>'; 

this is what i figured out from other posts
This code does not work, I just see bunch of question marks.

Is it my if statement or the script tag?

any ideas?


script type wasn’t specified. <script type=“text/javascript”> is the most common.

here is a quick and simple demo


if(isset($_GET['txtError'])) {
    echo '<script type="text/javascript">alert("'.$_GET['txtError'].'")</script>';
<form action="formProcessor.php" method="post" >
    <select name="selStory">
        <option value="0">Select a story</option>
        <option value="1">Story 1</option>
        <option value="2">Story 2</option>
        <option value="3">Story 3</option>
    <input type="submit" value="submit" />


if(!isset($_POST['selStory']) || empty($_POST['selStory']) || $_POST['selStory'] == '0' ) {
    $errMsg = urlencode("You haven't selected a story");
    header('Location: index.php?txtError='.$errMsg);
echo '<p>you selected  story '.$_POST['selStory'].' </p>';

I fixed the type.

But when I open the page, a box pops up asking to open the same page and when i do that (just to see what happens) it says this is a script file, are you sure you want to open it?

my alert box doesn’t pop up, and why it wants to open itself again?

I think most people would simply tend to redirect the user back to page one, as they should not be able to navigate directly to this page, from your description.

$story = $_POST["stories"];

$permitted_stories = array(
, 'story2.txt'
, 'story3.txt'

// check if the story submitted is permitted
if( !in_array($story, $permitted_stories) ){

// send to the start page
header('Location: page_one.php');


// All ok - get on with the page

If you wanted to be nice and explain on page one why they had been redirected you could replace “page_one.php” with “page_one.php?reason=badchoice” on the redirect line.

Then on page_one you could detect that by checking for



Ah, bit slow there you could do what Kalon suggests too, and detect the absence of POST[‘stories’] and do a similar redirect with a different $reason

who you talking to? :confused2:

the demo I posted works fine in my browser.

Thank you both.

I tried Cups suggestion (seems easier).

I did the changes to page_two, but i didn’t get what you said about explaining the reason why user is redirected. (I am a beginner)

could you explain that what am i suppose to do on page_one exactly? (if i want a alert box, should i use Kalon’s solution?)

I tried Kalon’s suggestion. I didn’t work. I see couple of question marks again. :frowning:

if you copy and paste the code I posted into separate files, for demo purposes, it should work. It works in my IE8 and FF3.6.

if your code isn’t working then you need to post all your updated code and we can try to help you fix it.

I checked it again. I’d left one line out.
It works perfectly now.

Thank you so much.