undefined index....

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • selvialagar
    New Member
    • Apr 2008
    • 57

    undefined index....

    [code=php]
    <?php
    require_once('d atabase_conn.ph p');
    session_start() ;
    if(isset($_SESS ION['username']))
    {
    if(isset($_GET['action']) && $_GET['action']=="submit")
    {
    $cust_id=$_POST['cname'];
    echo $cust_id;
    }
    else
    {
    ?>
    <html>
    <head>
    <script type="text/javascript">
    function cust_display()
    {
    if(document.get ElementById('cn ame').value==0)
    {
    alert("Please Select Customer Name");
    }
    else
    {
    document.getEle mentById('cust_ id').value=docu ment.getElement ById('cname').v alue;
    alert(document. getElementById( 'cust_id').valu e);
    window.location ="bill_index.ph p?action=submit ";
    }
    }
    </script>
    </head>
    <body>
    <?php
    include('adminl ogin.php');
    ?>
    <form action="#" method="post" name="cust">
    <br />
    <center>
    Select Customer Name :
    <select name="cname" id="cname" class="bg2" onChange="cust_ display()">
    <option value="0">--Select--</option>
    <?php
    $query=mysql_qu ery("select cust_name,cust_ id from pms_customer");
    while($r=mysql_ fetch_object($q uery))
    {
    ?>
    <option value="<?php echo $r->cust_id;?>"><? php echo $r->cust_name;?> </option>
    <?php
    }

    ?>
    </select>

    </form>
    <input type="hidden" name="cust_id" id="cust_id" />
    <?php
    }
    ?>
    </html>
    <?php
    }
    else
    {
    header("locatio n:login_index.p hp");
    }
    ?>
    [/code]
    when i execute this program....unde fined index ...error appers....how to solve thi s problem....
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    I cannot see where you submit the form, is submitted at all.

    Secondly, you mix $_GET and $_POST, so what is it?[php]if(isset($_GET['action']) && $_GET['action']=="submit")
    {
    $cust_id=$_POST['cname'];
    echo $cust_id;
    }[/php]Ronald

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      That particular mix of GET and POST data looks kind of strange to me to.

      I don't see a submit button for the form. The only thing I see there is a JavaScript redirect, which includes the "?action=submit " GET string.

      If that is indeed what you are using to submit your form, you must realize that none of the form's <input> elements are being sent. The form is not actually being submitted, the browser is simply being redirected.

      Which would cause the POST array to be empty, leading to the undefined index notice.

      To solve this... set the <form> action to "?action=submit " and add a <input type=submit> element to submit the form.

      Or have JavaScript submit the form via the: "form.submi t()" function.

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        .... :o

        I think I posted in this thread.... but I can't see my post...

        Comment

        • ronverdonk
          Recognized Expert Specialist
          • Jul 2006
          • 4259

          #5
          Originally posted by hsriat
          .... :o

          I think I posted in this thread.... but I can't see my post...
          Sorry about that hsriat!

          You posted in the duplicate thread. This mixup of threads is one of the reasons why duplicate threads are not allowed.

          Your post can still be seen in that thread HERE

          Ronald

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            Originally posted by ronverdonk
            Sorry about that hsriat!

            You posted in the duplicate thread. This mixup of threads is one of the reasons why duplicate threads are not allowed.

            Your post can still be seen in that thread HERE

            Ronald
            oh its cool!!. No need to apologize... :)

            I was actually confused about whether I pressed Submit Reply or not. :D


            Regards,
            Harpreet

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              I suggest that you just post your reply again. I could do that. but that would be an infringement of your copyright :-)

              Ronald

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Originally posted by ronverdonk
                I suggest that you just post your reply again. I could do that. but that would be an infringement of your copyright :-)

                Ronald
                Well, after reading the code again, I think the main problem is not what I thought earlier. But its at line 27, in the JavaScript code.

                @selvialagar

                Remove line 27 and write the following lines instead.[code=javascript]
                document.forms[0].action = "bill_index.php ?action=submit" ;
                document.forms[0].submit();[/code]
                If problem still persists, replace && in line 6 with & (single ampersand).


                Harpreet

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by hsriat
                  ...
                  If problem still persists, replace && in line 6 with & (single ampersand).


                  Harpreet
                  Or you could just write "and".

                  PHP understands all of them...
                  (& == && == and) if that makes any sense ;)

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Originally posted by Atli
                    Or you could just write "and".

                    PHP understands all of them...
                    (& == && == and) if that makes any sense ;)
                    Sorry about that, but it has to be either && or and, but NOT &.

                    I got confused between & and &&.

                    & would actually give error here. & usually gives same results as && does but not always.

                    In case of &&, if first condition is false, it will return false, without caring to check the second one.
                    But & will always check both conditions before returning anything.

                    I tested it with this example:[php]<?php
                    echo "With <b>&amp;&amp; :</b><br>";
                    if (A(false) && B(true))
                    {
                    echo "0";
                    }
                    echo "<br>------------<br>With <b>&amp;:</b><br>";
                    if (A(false) & B(true))
                    {
                    echo "0";
                    }

                    function A($c)
                    {
                    echo "Function A executed<br>";
                    return $c;
                    }
                    function B($c)
                    {
                    echo "Function B executed<br>";
                    return $c;
                    }
                    ?>
                    [/php]


                    Regards,
                    Harpreet

                    Comment

                    • Atli
                      Recognized Expert Expert
                      • Nov 2006
                      • 5062

                      #11
                      Originally posted by hsriat
                      Sorry about that, but it has to be either && or and, but NOT &.

                      I got confused between & and &&.

                      & would actually give error here. & usually gives same results as && does but not always.

                      In case of &&, if first condition is false, it will return false, without caring to check the second one.
                      But & will always check both conditions before returning anything.

                      I tested it with this example:
                      ...

                      Regards,
                      Harpreet
                      Ahh, I see.
                      Nice catch, I never thought to check that.

                      I think this would actually qualify as a bug, as checking the extra parameters does nothing but degrade performance and cause possible errors. I can't think of any real reason why you would want the extra parameters checked if the others fail.

                      And like you say, in this case, using the single & would cause the undefined index notice if the first check fails.

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        To contradict my previous post :P...

                        After digging a little deeper, I find that the single & is in fact NOT a logical operator, which "and" and "&&" are...
                        It is in fact a bitwise operator, which explains why it evaluates everything before returning.

                        It is meant to compare all the parameters and "set" all bits that are identical (which in the case of boolean values, will always be 0 (false) unless all parameters are true).

                        Also...
                        There are slight differences between "&&" and "and".

                        They will work identically when used inside an if statement, but when used on variables, they differ slightly.

                        Consider this:
                        [code=php]
                        $a = true && false; // evaluates into: false
                        $b = true and false; // evaluates into: true
                        [/code]
                        The difference...
                        "&&" has higher precedence than "=", so "true && false" is evaluated and then stored in $a
                        "and" has lower precedence than "=", so "$b = false" is evaluated first, which is then evaluated against "and false", which changes nothing and $b remains true.

                        Lastly... we seem to have wandered slightly of topic.
                        My apologies for that :)

                        Comment

                        • Markus
                          Recognized Expert Expert
                          • Jun 2007
                          • 6092

                          #13
                          Originally posted by Atli
                          To contradict my previous post :P...

                          After digging a little deeper, I find that the single & is in fact NOT a logical operator, which "and" and "&&" are...
                          It is in fact a bitwise operator, which explains why it evaluates everything before returning.

                          It is meant to compare all the parameters and "set" all bits that are identical (which in the case of boolean values, will always be 0 (false) unless all parameters are true).

                          Also...
                          There are slight differences between "&&" and "and".

                          They will work identically when used inside an if statement, but when used on variables, they differ slightly.

                          Consider this:
                          [code=php]
                          $a = true && false; // evaluates into: false
                          $b = true and false; // evaluates into: true
                          [/code]
                          The difference...
                          "&&" has higher precedence than "=", so "true && false" is evaluated and then stored in $a
                          "and" has lower precedence than "=", so "$b = false" is evaluated first, which is then evaluated against "and false", which changes nothing and $b remains true.

                          Lastly... we seem to have wandered slightly of topic.
                          My apologies for that :)
                          Wow!

                          Seems like i'm thanking you alot, recently!

                          Haha.

                          Cheers(again).

                          Comment

                          • selvialagar
                            New Member
                            • Apr 2008
                            • 57

                            #14
                            thank u very much for your guidence....... ...

                            Comment

                            • coolsti
                              Contributor
                              • Mar 2008
                              • 310

                              #15
                              Here is where a bit of discipline in script writing can go a long way. Writing clear and understandable code helps not only yourself in debugging it but also others in helping you.

                              I have two hopefully helpful comments in this regard:

                              You have a line something like "$query = mysql_query(" etc. ")" and it really threw me off when I looked at your code, that you used the variable name $query here. The query is actually the argument of this function, not its return value. I use the variable $result instead. This maybe seems like nitpicking, but using an appropriate variable name helps make the code clearer.

                              Another point is that you should try to separate a bit more clearly the code that produces HTML output to the requester's browser, and the code that does other tasks, such as database queries.

                              Comment

                              Working...