SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2002
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help my codes :)

    Hi how are you doing? Now I am trying to run this program but php edit says "Undefined index: save"

    It says it whenever I try. Could you find my
    mistake?

    My php is 4.2

    Here is code
    save as <ito1.php>
    <?php

    $yourname=$_POST["yourname"];
    $save=$_POST["save"];

    print($yourname."Thank you very much!<br>\n");


    if($save == "on"){

    print("Save your name<br>\n");


    }
    else
    {

    print("Not save your name<br>\n");


    }
    ?>

    2.save as <ito2.php>
    <html>
    <body>
    <form action="ito1.php" method="post">
    Name<input type="text" name="yourname" size="30"><br>
    <input type="checkbox" name="save">Please check your name whether you want to save it or not<br>
    <input type="submit" name="submit" value="submit">
    <input type="reset" name="reset" value="reset">
    </form>
    </html>


    Thank you for helping now

    Bye for now

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    at a first glance I tried your script and then I said "damn it works! where is the problem?".

    I saw it now, and it happens when you do not check the "check box" field of your form.
    If you do not check that field and then you click the submit button then $_POST["save"] will never be set ( ie no post var sent ).
    That is why php warns you with that message.

    So,
    inside ito1.php use:

    PHP Code:
    <?php 

    $yourname
    =$_POST["yourname"]; 

    print(
    $yourname."Thank you very much!<br>\n"); 

    if( isset(
    $_POST["save"]) ){ 
    // the field is checked
    print("Save your name<br>\n"); 



    else 


    print(
    "Not save your name<br>\n"); 



    ?>
    H.T.H.!


    p.s.
    The code I wrote did not check $_POST[ 'submit' ] and other post vars.
    My aim was to show you why you had the "warning" and how to avoid that using your piece of code.
    Last edited by pippo; Jun 5, 2002 at 03:12.
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03


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
  •