www.webdeveloper.com
Results 1 to 6 of 6

Thread: How to display a message on another page

  1. #1
    Join Date
    Nov 2013
    Posts
    72

    How to display a message on another page

    I am working on a form for a survey. It is very simple as it only has three radio buttons. After the form is submitted, I have another PHP page handling the form. I am using isset to check to see if each one was ticked. If one was not ticked, I want to display a message asking the user to answer each one. On the other hand, I also want to display a message how can I pass a message once the form is successfully completed. I know how to easily do this in JavaScript but no in PHP.

    Anyone care to help me out? Below is the code I have:

    This is where I want to display the message. The page that contains this is only a PHP page and not an HTML page. Thanks to anyone that takes a look at this.

    <span name="message">&nbsp;</span>

    PHP Code:

    session_start(); 
    <?php



    if(!isset($_POST['whereBuyBooks']) ||
       !isset(
    $_POST['rent']) ||
       !isset(
    $_POST['usefulness'])){
           
         
    //How do I get this on the survey page??
        
    $message 'Please answer each question';
      }


    //Grabs data from form
    $whereBuyBooks $_POST['whereBuyBooks'];
    $rent $_POST['rent'];
    $usefulness $_POST['usefulness'];

    //Grabs current time
    $curtime date("Y-m-d H:i:s",time()); 

    // Formats data before writing to file
    $data =  'New survery taken on: ' .$curtime."\n";
    $data .= 'Where students buy most books: ' .$whereBuyBooks "\n";
    $data .= 'Do you rent texbooks: ' .$rent"\n";
    $data .= 'How useful do you find our site: ' .$usefulness"\n\n\n\n";
                  
            
    $filename 'survey.txt';

    file_put_contents($filename$dataFILE_APPEND);
           
    //How do I get this on the survey page??
    $message 'Thank you for completing our survey';
            
     
    header("Location: index.php"); 
            
            
    ?>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    Easiest(?) way is to use a single page for everything, so that the form page submits to itself. Then you add logic up front to determine whether or not this is a form submission (usually by checking for some $_POST or $_GET element), in which you case you process it, else if not, then you display the form. If the form submission fails, then you can display the form along with a message, plus the values are already there in $_GET or $_POST if you want to use them to prefill the form with what the user had entered.

    You may want to make the form page itself a separate file that you can require() as needed, and you could do the same with the "success" page.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Nov 2013
    Posts
    72
    Quote Originally Posted by NogDog View Post
    Easiest(?) way is to use a single page for everything, so that the form page submits to itself. Then you add logic up front to determine whether or not this is a form submission (usually by checking for some $_POST or $_GET element), in which you case you process it, else if not, then you display the form. If the form submission fails, then you can display the form along with a message, plus the values are already there in $_GET or $_POST if you want to use them to prefill the form with what the user had entered.

    You may want to make the form page itself a separate file that you can require() as needed, and you could do the same with the "success" page.
    I thought about doing that to start with but was not sure if I could. This page I am working on with the survey form will be the header page for each page of my site, which will also have the log in form.

    Will I run into any problems having two forms that need to be handled on the same page by the same page? I suppose I could put each one in a function. Would that work?

    Thanks for the fast response!

  4. #4
    Join Date
    Nov 2013
    Posts
    72
    Just wanted to say that I figured it out. I just put all the code in an if isset on the submit button. Then I said if not set show error message else grab the inputs, write to file, and display thank you message.

    Thanks again!

  5. #5
    Join Date
    Sep 2013
    Posts
    221
    Below is the example code you can try this:

    process.php

    header('location: /thank-you.php?mess=1');
    exit();
    thank-you.php

    $mess = isset($_REQUEST['mess']) ? $_REQUEST['mess'] : null;
    if($mess == 1) {
    echo 'Thank you. Please come again'; }

    This is not optimal but should work for simple scenarios.Hope this helps.

  6. #6
    Join Date
    Nov 2013
    Posts
    3
    Such A Nice Information thanks For the Share This

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles