www.webdeveloper.com
Results 1 to 10 of 10

Thread: What is wrong with this script?

  1. #1
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785

    What is wrong with this script?

    OK, it's a baby step for me...

    what is wrong with these scripts?
    I have two files, wish.php and day.php

    wish.php collects the data (day of the week) and day.php should kick a response depending on what you entered. I set it to respond "Get back to work, you lazy bum!", if you enter "Sunday" and, supposedly "You need a vacation..." if you enter something else.

    I am getting a responce "Get back to work, you lazy bum!" no matter what I enter.

    Can someone point an error out?

    here is wish.php
    PHP Code:
    <html>
    <
    head>
    <
    title>Pick-a-Day</title>
    </
    head>
    <
    body>
    <
    form action="day.php" method=post>
      
    I wish today was
      <
    input type="text" name="day">
      [
    enter day of the week
      <
    input type="submit" name="submit" value="Click!">
    </
    form>
    </
    body>
    </
    html
    here is day.php
    PHP Code:
    <?php

    $day 
    "Sunday";
        if (
    $day == "Sunday") { 
            echo (
    "Get back to work, you lazy bum!");
        } 
        else {
            echo (
    "You need a vacation...");

        }

    ?>
    Same thing happens if I use wish2 file, which has checkboxes rather then text input field.
    PHP Code:
    <html>
    <
    head>
    <
    title>Pick-a-Day</title>
    </
    head>
    <
    body>
    <
    form action="day.php" method=post>
      <
    p>I wish today was:</p>
      <
    p
        <
    input type="checkbox" name="Monday" value="checkbox">
        
    Monday</p>
      <
    p
        <
    input type="checkbox" name="Sunday" value="checkbox">
        
    Sunday</p>
      <
    p>
        <
    input type="submit" name="submit" value="Click!">
      </
    p>
      </
    form>
    </
    body>
    </
    html
    which confirms my suspicion: the problem is not with the form, but with the day.php file itself.
    Last edited by Daria; 02-05-2004 at 04:11 PM.
    Absence of a result is a result, unless defined otherwise.

  2. #2
    Join Date
    Aug 2003
    Posts
    1,576
    maybe I'm reading this wrong, but it looks like you're defining $day in day.php
    $day = "Sunday";
    if ($day == "Sunday") {
    which would meen that $day will be equal to Sunday no matter what the form sends day=Sunday
    try removing the line:
    $day = "Sunday";
    and see if it works right

    you also might want to make it ignore case, since a lot of people would type sunday rather than Sunday, however this looks like its just a learning activity, in which case that wouldn't matter

  3. #3
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785
    Thanks.

    Your suggestion worked on the wish.php (the textfield input), but didn't work on wish2.php (checkbox). Interestingly enough, now it's showing the other message, "You need a vacation".

    See wish.php

    See wish2.php

    Edited: just for giggles, I also tried not removing it, but putting
    $day = ""; instead, and I got identical bad results with "you need a vacation". The link above has day.php without that line altogether.

    I'm trying to understand why it works (or should I say, doesn't) this way?...
    Last edited by Daria; 02-05-2004 at 06:23 PM.
    Absence of a result is a result, unless defined otherwise.

  4. #4
    Join Date
    Aug 2003
    Posts
    1,576
    in wish 2, its recieving "checkbox" not a day.
    change your wish2.php as follows:
    PHP Code:
      <p>  
        <
    input type="checkbox" name="day" value="Monday"
        
    Monday</p
      <
    p>  
        <
    input type="checkbox" name="day" value="Sunday"
        
    Sunday</p
      <
    p
        <
    input type="submit" name="submit" value="Click!"
      </
    p
    granted I've never used checkboxes in a form, but this is how I would imagine they work
    EDIT:
    when learning php, its sometimes better to use GET rather than POST, since you can see what arguments the php script is recieving

  5. #5
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785
    GREAT!

    Now I get it!!!!!!!!

    thanks


    big favor:

    could you explain
    when learning php, its sometimes better to use GET rather than POST, since you can see what arguments the php script is recieving
    to me, like I'm 5 year old, ple-ease?
    Last edited by Daria; 02-05-2004 at 06:40 PM.
    Absence of a result is a result, unless defined otherwise.

  6. #6
    Join Date
    Aug 2003
    Posts
    1,576
    as a side note, if both of those checkboxes were checked, it would show as sunday i believe, since its the last thing in your code chronologically, i would suggest using radio buttons for something like that in the future, since you hypothetically wouldn't want someone to wish it was both monday & sunday

  7. #7
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785
    good point!
    Absence of a result is a result, unless defined otherwise.

  8. #8
    Join Date
    Sep 2003
    Location
    Netherlands
    Posts
    353
    With use the GET statement he meant that you should replace POST with GET if you're still learning PHP.
    It makes the variables your sending appear in the address bar.
    For example the user types "Sunday"
    PHP Code:
    <form action="day.php" method=POST
      
    I wish today was:  
      <
    input type="text" name="day"
      [
    enter day of the week]  
      <
    input type="submit" name="submit" value="Click!"
    </
    form
    shows "http://blablabla/day.php" in the Address Bar.
    PHP Code:
    <form action="day.php" method=GET
      
    I wish today was:  
      <
    input type="text" name="day"
      [
    enter day of the week]  
      <
    input type="submit" name="submit" value="Click!"
    </
    form
    Would give "http://blablabla/day.php?day=Sunday"
    This way, you can see the variable and would notice that with the checkboxes it gave day=checkbox instead of day=Sunday. Success with php
    <?php function reply_to_thread($user,$problem=NULL) {
    if($user["nice"] === TRUE) { give_possible_solution($problem);}
    else { exec($user,'code_fail',undefined); }
    return TRUE; } ?>

  9. #9
    Join Date
    Nov 2002
    Location
    NJ, USA
    Posts
    785
    THANK YOU!
    Absence of a result is a result, unless defined otherwise.

  10. #10
    Join Date
    Sep 2003
    Location
    Netherlands
    Posts
    353
    one more thing that you should look out for:
    you code responds with "Get back to work, you lazy bum!"
    If day == "Sunday"
    but if the user types "sUnday" "sunday" "SUNDAY" "sUNDAY" " Sunday" "Sunday " etc.. they will stil get the "You need a vacation..." response. PHP is case- and space-sensitive when it comes to strings and variables.

    To make the variable case-insensitive use this before the if.
    PHP Code:
    $day = (strtolower($day));
    $day = (ucwords($day));
    $day = (trim($day)); 
    This changes the input " sUNDaY " (with spaces) in three steps.
    First step: lowercase everything": " sunday "
    Second step: uppercase 1st letter of every word: " Sunday "
    Third step: remove leading and trailing spaces: "Sunday"
    Tada, your code is now much more friendly (unless the user types Snuday etc.. )
    <?php function reply_to_thread($user,$problem=NULL) {
    if($user["nice"] === TRUE) { give_possible_solution($problem);}
    else { exec($user,'code_fail',undefined); }
    return TRUE; } ?>

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