Undefined index?

Discussion in 'PHP' started by le007, Oct 17, 2011.

  1. #1
    The file is saving but I'm getting this error too?

    Notice: Undefined index: saving in C:\wamp\www\appendfile\test.php on line 162

    Any suggestions? I can't see what's wrong...

    This is where line 162 is:
    Thanks


    <td style="color:white;">

    <?php
    $saving = $_REQUEST['saving'];
    if ($saving == 1) {
    $data = $_POST['data'];
    $file = "test.txt";

    $fp = fopen($file, "w") or die("Couldn't open $file for writing!");
    fwrite($fp, $data) or die("Couldn't write values to file!");

    fclose($fp);
    echo "Saved to $file successfully!";
    }
    ?>

    <form name="form1" method="post" action="test.php?saving=1">
    <textarea name="data" cols="82" rows="20">
    <?php
    $file = "test.txt";
    if (!empty($file)) {
    $file = file_get_contents("$file");
    echo $file;
    }
    ?>
    </textarea>
    <br>
    <center><input type="submit" value="Save Testimonials"></center>
    </form>
    </td>
     
    le007, Oct 17, 2011 IP
  2. mrmuggles

    mrmuggles Greenhorn

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #2
    if this is where the error happens:
    $_REQUEST['saving'];

    Then it means that the "index" named "saving" doesn't exists in the array REQUEST.
    $_REQUEST will look in the URL to see a parameter named "saving", something like this :
    http://www.mysite.com/page.php?saving=123

    if it's possible that the parameter is not there in your URL sometimes, just look if it exists :
     
    mrmuggles, Oct 17, 2011 IP
  3. Divided

    Divided Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    0
    #3
    Another way is to use the isset() function:

    $saving = "";
     if (isset($_REQUEST['saving'])) {
     $saving = $_REQUEST['saving'];
     }
    PHP:
    isset() is about 3 times faster than array_key_exists(). They do virtually the same thing but optimisation is never a bad thing.
     
    Divided, Oct 18, 2011 IP
  4. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Thanks for your replies. Which will work, do you think? I just need rid of the error message :/
     
    le007, Oct 18, 2011 IP
  5. Divided

    Divided Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    3
    Trophy Points:
    0
    #5
    Either will work, they both achieve the same solution using different methods.

    It's down to personal preference which one you adopt.
     
    Divided, Oct 18, 2011 IP
  6. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #6
    Use empty... ?xxx= is set but is still empty and may produce an error
     
    Einheijar, Oct 18, 2011 IP
  7. le007

    le007 Well-Known Member

    Messages:
    481
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #7
    Got it working when I put in the full address rather than test.php

    Thank you all for the replies.
     
    le007, Oct 23, 2011 IP