www.webdeveloper.com
Results 1 to 15 of 15

Thread: World outside the (isset($_POST['Submit']) work why?

  1. #1
    Join Date
    Mar 2014
    Posts
    70

    World outside the (isset($_POST['Submit']) work why?

    $name = "";
    $email= "";
    $errMsg = array(
    "name"=>"",
    "email"=>"",
    "pass"=>"",
    "id"=>"",
    "phone"=>""
    );
    $err = false;


    if i write the above code outside the if(isset($_POST['Submit'])) then i am able to get values on the page.

    $errMsg["name"] = "Name is required";
    <?php echo htmlspecialchars($name);?>

    why not i have directly access to these errors,,,, as form is on the same page

    if(isset($_POST['Submit'])){

    if (empty($_POST["name"])){
    $errMsg["name"] = "Name is required";
    $err= True;
    }
    // check e-mail */

    if (empty($_POST["email"])){
    $errMsg["email"] = "Email is required";
    $err = True;
    }
    }


    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <label for="name">Your name:</label>
    <input type="text" name="name" class="form-control" id="userName" value="<?php echo htmlspecialchars($name);?>" />
    <span class="error"><?php echo $errMsg["name"];?></span>

    <label for="email">Your email:</label>
    <input type="text" name ="email" id="userEmail" class="form-control" value="<?php echo htmlspecialchars($email);?>" />
    <span class="error"><?php echo $errMsg["email"];?></span>

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    My question would be, do you have an input on your form named 'Submit' that has a value in it? Just having a submit button is not the same thing and so when you check $_POST['Submit'] there won't be a value and that entire block will be skipped. A better way to check if a form has been submitted would be to use

    PHP Code:
    if(isset($_POST)) {
      
    // Do stuff here


  3. #3
    Join Date
    Mar 2014
    Posts
    70
    <input type ="submit" value ="Send" id="userSubmit" class="btn btn-success"/>

    so? what to do? should i write also $name = "";
    $email= "";
    $errMsg = array(
    "name"=>"",
    "email"=>"",
    "pass"=>"",
    "id"=>"",
    "phone"=>""
    );
    $err = false;

    with

    if(isset($_POST)) {
    // Do stuff here
    }

  4. #4
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    I'm not really understanding what you are trying to ask at this point... In your original code you claimed you couldn't access those variables while they were inside of your if statement (if(isset($_POST['Submit'])){}). You don't have a field with a name 'Submit' that will get posted to PHP so I presented the correct way to check if a form had been submitted, which would replace your if(isset($_POST['Submit'])){ line of code.

    So based on what you originally stated your code would look like this:
    PHP Code:
    if(isset($_POST)) {
      
    $name "";
      
    $email"";
      
    $errMsg = array(
        
    "name"=>"",
        
    "email"=>"",
        
    "pass"=>"",
        
    "id"=>"",
        
    "phone"=>""
      
    );
      
    $err false;

      if (empty(
    $_POST["name"])) {
        
    $errMsg["name"] = "Name is required";
        
    $errTrue;
      }

      
    // check e-mail
      
    if(empty($_POST["email"])) {
        
    $errMsg["email"] = "Email is required";
        
    $err True;
      }


  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    You don't have a name value for your submit button, so there is no "Submit" element in the form data.
    Code:
    <input type="submit" name="Submit" value="Send" id="userSubmit" class="btn btn-success"/>
    "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

  6. #6
    Join Date
    Mar 2014
    Posts
    70
    why u are initializing like this?
    $name = "";
    $email= "";
    i am doing
    $name = $_POST["name"];
    $email = $_POST["email"];
    i have some other fields also like age,gender,, its not code duplication.. first time initialize with empty value then put the value in it.. why ????

  7. #7
    Join Date
    Mar 2014
    Posts
    70
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
    <label for="name">Your name:</label>
    <input type="text" name="name" class="form-control" id="userName" value="<?php echo htmlspecialchars($name);?>" />


    <label for="email">Your email:</label>
    <input type="text" name ="email" id="userEmail" class="form-control" value="<?php echo htmlspecialchars($email);?>" />

    if(isset($_POST)){
    $name = "";
    $email= "";
    $pass = "";
    $id = "";
    $phone = "";
    $Success ="";
    $err = False;
    $errMsg = array(
    "name"=>"",
    "email"=>"",
    "pass"=>"",
    "id"=>"",
    "phone"=>""
    );

    if (empty($_POST["name"])){
    $errMsg["name"] = "Name is required";
    $err= True;
    }
    else
    {
    $name = $_POST["name"] ;
    }

    if (empty($_POST["email"])){
    $errMsg["email"] = "Email is required";
    $err= True;
    }
    else
    {
    $email = $_POST["email"] ;
    }

    }


    but i am getting
    Undefined index: name in ........ on line 77
    Undefined index: email in............ on line 78 why ?

  8. #8
    Join Date
    Mar 2014
    Posts
    70
    final code

    if(isset($_POST)){
    $name = "";
    $email= "";
    $pass = "";
    $id = "";
    $phone = "";
    $Success ="";
    $err = False;
    $errMsg = array(
    "name"=>"",
    "email"=>"",
    "pass"=>"",
    "id"=>"",
    "phone"=>""
    );

    $name = $Object->check($_POST["name"]); ........ ref1
    $email = $Object->check($_POST["email"]); ......... ref2

    }


    function check($Data){
    // sql injection
    $Data = trim($Data);
    $Data = stripslashes($Data);
    $Data = htmlspecialchars($Data);
    return $Data;
    }

    after cleaning then i am making array of $name,...,..,,... and then validating....
    please help me what is wrong...


    ref1...... Undefined index: name in ........ on line 77
    ref2...... Undefined index: email in............ on line 78

    why ?

  9. #9
    Join Date
    Mar 2014
    Posts
    70
    any one help me... still getting the above error...

  10. #10
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    1,033
    As much as I'd like to solve your problem, your code really is all over the place. It's hard to tell you what's wrong because I'm not seeing everything, only pieces of your code and frankly they don't seem to be in any order.

    For instance, 2 post up (post #7), you mention the error on line 77 and 78. Based on the code you provided in that post it doesn't appear possible that you could get those 2 errors on consecutive lines. In your next post your code seems to have changed, but you have the same error. While you seem to point to the actual lines the error is on, I wonder how much of your code I'm not seeing.

    On a separate but related note, generally when setting values from POST variables you should always use some sort of conditional assignment to prevent error from missing/empty values. An example:
    PHP Code:
    $name = (isset($_POST['name'])) ? $_POST['name'] : ""
    This prevents any issue in the event that name is not submitted in the POST. But again, I have no idea if this will help you or not as I honestly have no idea what your code looks like. I just keep getting little snippets that seem to keep changing.

  11. #11
    Join Date
    Mar 2014
    Posts
    70
    should i do this... $name = (isset($_POST['name'])) ? $_POST['name'] : "";

    for all inputs as i have very big form..

  12. #12
    Join Date
    Feb 2014
    Location
    Canada
    Posts
    155
    The 2 errors stated there were undefined indices on 2 lines, however, no where in your code snippets would those errors occur, so we would need to see larger, more complete code snippets.

    In regards to checking whether $_POST variables are set, you should always do this but it's up to you and what you want to happen as to whether you want to use a condensed way of checking if the $_POST variable are set or explicitly write if-else blocks.

  13. #13
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,634
    Quote Originally Posted by sulman8s View Post
    should i do this... $name = (isset($_POST['name'])) ? $_POST['name'] : "";

    for all inputs as i have very big form..
    Let PHP do some of the work:
    PHP Code:
    $fields = array('name''email''subject''text');
    foreach(
    $fields as $fieldName) {
        ${
    $fieldName} = (isset($_POST[$fieldName])) ? $_POST[$fieldName] : '';

    Mind you, I'm not a big advocate of assigning everything in $_POST to scalar variables, as you are just duplicating what is already in $_POST. E.g., in a simple example:
    PHP Code:
    // why do this:
    $foo $_POST['foo'];
    echo 
    $foo;

    // when you could just do this:
    echo $_POST['foo']; 
    Plus, at any point in your code where you use $_POST['foo'], you automatically know it's from posted form data, so you don't have to trace a variable back to its source to find out where/how it was set. If you take that approach, then you would just change the above to:
    PHP Code:
    $fields = array('name''email''subject''text');
    foreach(
    $fields as $fieldName) {
        if(!isset(
    $_POST[$fieldName])) {
        
    $_POST[$fieldName] = '';

    "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

  14. #14
    Join Date
    Mar 2014
    Posts
    70
    one thing more .. how to write function for this approach and display errors... as i want to use as oop...

  15. #15
    Join Date
    Mar 2014
    Posts
    70
    how to show errors and make function... more keep the form values save after reload!

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