Echo output page if there is no action on input

Discussion in 'PHP' started by web_warrior, Sep 8, 2009.

  1. #1
    I have this very simple code of introducing data from input form into a flat file:

    <?php
       
    if (isset($_POST['submit'])) {
        
    $name1 = $_POST['name1'];
    $name2 = $_POST['name2'];
    
    $fp = fopen("data.txt","w");
        
    if(!$fp) {
    echo 'Error, the file could not be opened or there was an error creating it.';
    exit;
    }
      
    fwrite($fp, "$name1"."<br/><br/>"."$name2"."\n");
     
    fclose($fp);
    }
      
    ?>
    PHP:
    What needs to be introduced in the script above or in the output page script so that if we do not have action (no input made at all), so basically the flat file is not even created yet, a certain output php page to be shown (without outputting the error messages that there is no such data file...)?

    Right now on the output page the data is shown using:

    <html>
    ............
    <?php
    $file = fopen("data.txt", "r");
    $data = fread($file, filesize("data.txt"));
    fclose($file);
      
    echo $data;
    ?>
    ............
    </html>
    PHP:
     
    web_warrior, Sep 8, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    instead of:

    if (isset($_POST['submit'])) {
    PHP:
    use
    if (!empty($_POST['name1']) && !empty($_POST['name2'])) { .. } 
    PHP:
    ?
     
    premiumscripts, Sep 8, 2009 IP
  3. web_warrior

    web_warrior Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It does not work.
    I tried it without changing anything else, and also by making other changes after using this bit of code, but with no result...
     
    web_warrior, Sep 8, 2009 IP
  4. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Err then you're trying to do something else? If you changed the code correctly, it should only save the file if name1 and name2 have both been filled in. That is what you wanted, right?
     
    premiumscripts, Sep 8, 2009 IP
  5. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #5
    May be you missed the concept of the solution given by premium. You have to check whether the user has introduced input in the fields you consider mandatory for creating the file. Your sample script has two fields (name1 and name2). The solution proposed won't create the file if both fields are empty.
    Of course you have to translate that concept to the actual fields in your form. And, not all kind of fields are checked the same way. You have to decide which fields are mandatory and which aren't.
     
    caprichoso, Sep 8, 2009 IP
  6. web_warrior

    web_warrior Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Now this part works, I used this code:

    <?php
    
    if (isset($_POST['submit'])) {
    $name1 = $_POST['name1'];
    $name2 = $_POST['name2'];
    $fp = fopen("some.txt","w");
    
    if(!$fp) {
        echo 'Error, the file could not be opened or there was an error creating it.';
        exit;
    }
    fwrite($fp, "$name1"."<br><br>"."$name2"."\n");
    }
    if(empty($_POST['name1'])) {
    fwrite($fp, "Something that will be output on the outputphp if there is no input on name1"."\n");
    }
    fclose($fp);
    
    ?>
    PHP:
    (As I can have empty the second input field).

    But how I solve that on the output page even if the "some.txt" file does not exist yet, I will have echoed the same "Something that will be output on the outputphp if there is no input on name1"?
     
    web_warrior, Sep 8, 2009 IP
  7. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #7
    You have severe misconceptions about PHP. You have to start practising with less complex code. Then, start working on what you are trying to don now.
     
    caprichoso, Sep 8, 2009 IP
  8. web_warrior

    web_warrior Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Why, what is wrong with the code I just posted?
    It is working, I tried it.
     
    web_warrior, Sep 8, 2009 IP
  9. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Yeah, go and read the sticky in this forum. You do not have enough knowledge of PHP. Ofcourse someone could type a solution here, but I think it's best if you learn this stuff yourself..
     
    premiumscripts, Sep 8, 2009 IP
  10. web_warrior

    web_warrior Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    OK, you guys won't tell me why the code I posted last has severe misconceptions?:eek:
     
    web_warrior, Sep 8, 2009 IP
  11. caprichoso

    caprichoso Well-Known Member

    Messages:
    433
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #11
    Don't take it personally. But you need to learn some basic concepts before trying to do this kind of code.

    You need to learn how PHP manages HTML output.
    You have misused the if control structure. It doesn't work as you want it to work because misplaced it. Check how if control construct works.
    You will need to know how HTML forms values are handle on submission.

    PHP documentation is translated to several other languages too. The official documentation root is at: http://www.php.net/docs.php
     
    caprichoso, Sep 8, 2009 IP