SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Thread: php - alert box

  1. #1
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question php - alert box

    Hi,

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

    Thanks

  2. #2
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,011
    Mentioned
    56 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eli_goli View Post
    Hi,

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

    Thanks
    script type wasn't specified. <script type="text/javascript"> is the most common.

  3. #3
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here is a quick and simple demo

    index.php

    Code:
     
    <?php
    if(isset($_GET['txtError'])) {
        echo '<script type="text/javascript">alert("'.$_GET['txtError'].'")</script>';
        unset($_GET['txtError']);
    }
    ?>
     
    <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>
        </select>
        <input type="submit" value="submit" />
    </form>
    formProcessor.php

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

  4. #4
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    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?

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    PHP Code:
    $story $_POST["stories"];

    $permitted_stories = array(
      
    'story1.txt'
    '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');
    exit();

    }

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

    $_GET['reason']

    Edit:

    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

  6. #6
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eli_goli View Post
    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?
    who you talking to? :

    the demo I posted works fine in my browser.

  7. #7
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

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

  8. #8
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried Kalon's suggestion. I didn't work. I see couple of question marks again.

  9. #9
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by eli_goli View Post
    I tried Kalon's suggestion. I didn't work. I see couple of question marks again.
    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.

  10. #10
    SitePoint Member
    Join Date
    Nov 2010
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I checked it again. I'd left one line out.
    It works perfectly now.

    Thank you so much.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •