Mixture between html and php- UNIDENTIFIED INDEX

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • micky125
    New Member
    • Sep 2008
    • 36

    Mixture between html and php- UNIDENTIFIED INDEX

    Lo all,

    Had me head wrecked the whole week with a really stupid problem that came down to me not reading something but now when im running the php file i have errors switched on and i have bout 15 unidentified index but the problem is that i have used the code
    Code:
    function addActual()
    {
    var myTab = document.getElementById('myTable2');
    var row=myTab.rows.length; var y=myTab.insertRow(row);
    var a=y.insertCell(0);
    var xx= document.createElement('input');
    xx.type="text";
    xx.name="Date[]";
    a.appendChild(xx);
    var b=y.insertCell(1);
    var xx= document.createElement('input');
    xx.type="text";
    xx.name="Staff[]";
    b.appendChild(xx);
    var c=y.insertCell(2);
    var xx= document.createElement('input');
    xx.type="text";
    xx.name="Wage[]";
    c.appendChild(xx); var d=y.insertCell(3);
    var xx= document.createElement('input');
    xx.type="text";
    xx.name="Hours[]";
    }
    to add a row of a table in my input form but i have alot of them and as you can see they start array lists. the code in me php to call these is

    Code:
    $Date = $_POST['Date[]']; 
    $Staff = $_POST['Staff[]'];
    $Wage = $_POST['Wage[]'];
    $Hours = $_POST['Hours[]'];
    I also think this is stopping my mysql select statement as i use

    Code:
    function getProjectDetails()
    {
        global $ProjName;
        $temp = mysql_query("SELECT   Float, Extension, Excusable, NonExcusable, Delays, Value, LiquidatedDamages, totalLiquidatedDamages 
                  FROM     ProjectDetails
                  WHERE    Name= $ProjName")or die('<hr />MySQL Error1: ' .mysql_error(). '<hr />'); 
        $row = mysql_fetch_array($temp);
        return $row;
    }
    // A function to allow overall project variables to be manipulated.
    function makeVariableProjectDetails()
    {
        global $projectFloat, $projectExtension, $projectExcusable, $projectNonExcusable,
            $projectDelays, $projectValue, $liquidateddamages, $totalliquidateddamages;
        $temp = getProjectDetails();
        $projectFloat = $temp['ProjectFloat'];
        $projectExtension = $temp['Extension'];
        $projectExcusable = $temp['Excusable'];
        $projectNonExcusable = $temp['NonExcusable'];
        $projectDelays = $temp['Delays'];
        $projectValue = $temp['Value'];
        $liquidateddamages = $temp['LiquidatedDamages'];
        $totalliquidateddamages = $temp['totalLiquidatedDamages'];
    }
    where the error message

    MySQL Error1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Float, Extension, Excusable, NonExcusable, Delays, Value, Liquid

    comes. So is this connected and any1 ideas... im bout 99% positive me sql query is sound and i had ProjName in input form added so it wasnt that?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if $ProjName is a string, you should enclose it in single quotation marks.

    regards

    Comment

    • micky125
      New Member
      • Sep 2008
      • 36

      #3
      sound one that solved the mysql problem... still have to figure the
      Undefined index: EmailFrom[] in /home/moneill/public_html/mainPHP.php on line 37

      which are me arrays..

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Originally posted by micky125
        sound one that solved the mysql problem... still have to figure the
        Undefined index: EmailFrom[] in /home/moneill/public_html/mainPHP.php on line 37

        which are me arrays..
        Post the code concerning your arrays. I currently can't find them in your given code.

        Comment

        • micky125
          New Member
          • Sep 2008
          • 36

          #5
          lo markus

          just got in from work there the code im using in the html file is basically having

          header 1 h2 h3 h4 h5 etc |BUTTON|

          There is zero input boxes below this so when you first press button javascript kicks in with the code

          Code:
          function addActual() 
          { 
          var myTab = document.getElementById('myTable2'); 
          var row=myTab.rows.length; var y=myTab.insertRow(row); 
          
          var a=y.insertCell(0); 
          var xx= document.createElement('input'); 
          xx.type="text"; 
          xx.name="Date[]"; 
          a.appendChild(xx); 
          
          var b=y.insertCell(1); 
          var xx= document.createElement('input'); 
          xx.type="text"; 
          xx.name="Staff[]"; 
          b.appendChild(xx);
           
          var c=y.insertCell(2); 
          var xx= document.createElement('input'); 
          xx.type="text"; 
          xx.name="Wage[]"; 
          c.appendChild(xx);
          
          var d=y.insertCell(3); 
          var xx= document.createElement('input'); 
          xx.type="text"; 
          xx.name="Hours[]"; 
          }

          I have a couple of these all adding rows to different tables and it updates them ok. The problem i am having is that the inputs that it creates doesnt appear to work in the php program as i get the error;

          unidentified index

          I am using $_POST to pass them from html to php by

          Code:
          $Date = $_POST['Date[]'];  
          $Staff = $_POST['Staff[]']; 
          $Wage = $_POST['Wage[]']; 
          $Hours = $_POST['Hours[]'];
          It is only the variables that i pass to php using this format all the normal inputs are accepted but not these. The problem i am guessing it is is that the javascript is not being passed to php but i cant be sure ... have you any ideas on this.

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            You're receive and undefined error, not unidentified.

            When you pass an array to POST, you access it like an array.

            Code:
            $Date = $_POST['date']

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, Micky.

              When you name an input with '[]' at the end, it creates an array on the PHP side.

              Comment

              • micky125
                New Member
                • Sep 2008
                • 36

                #8
                sound one guys in work now but having to work till 10pm gmt the night so uve helped me loads. its in for next friday an i cant work on it over the weekend.

                THANKS.

                Comment

                • micky125
                  New Member
                  • Sep 2008
                  • 36

                  #9
                  lol hey guys well ive done what u say and fair enuf it works but how do i separate the array i get into smaller parts.
                  I have tried the usual
                  for($i=1; $i<=count("theA rray"); $i++) //"theArray" is generic

                  but this throws an undefined offset which is when the array part im accessing doesnt exist. I have also tried using

                  explode(",", "theArray")

                  but it throws array to string conversion...

                  because of this i get undefined offset within any for loop i use with these arrays.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    For greater efficiency, consider using foreach. Putting count() in the for loop is a bit inefficient, as the array elements are count()'ed with each iteration.

                    Comment

                    • micky125
                      New Member
                      • Sep 2008
                      • 36

                      #11
                      LOL PBMods u hit the nail on the head there... im only outa uni (doing this for dissertation in construction management [yes a major change]) and all i learned was java, cobol, mysql and prolog so what ur saying is 100% but i have looked at foreach code and have not understood it. In ur explanation it is very clear tbh but what is $element, is this the $i or do i go for column headings?

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        This code:
                        [code=php]
                        foreach( $array as $index => $element )
                        {
                        .
                        .
                        .
                        }
                        [/code]

                        is roughly equivalent to this:

                        [code=php]
                        for( $index = 0; $index <= count($array); $index++ )
                        {
                        $element = $array[$index];

                        .
                        .
                        .
                        }
                        [/code]

                        Except that foreach runs through EVERY element in the array, even if it has a non-numeric index.

                        Comment

                        • micky125
                          New Member
                          • Sep 2008
                          • 36

                          #13
                          U learn something new every day... thanks for explaining it to me cus i know uve got the blog and probs explain everyday of the week but sound1. Ill pass this teaching on LOL. :D

                          Comment

                          • micky125
                            New Member
                            • Sep 2008
                            • 36

                            #14
                            Hi guys ive been trying to use these foreach loops and was just looking clarification to find whetehr i am doing it rite.
                            Code:
                            //Email Variables
                            $EmailFro = $_POST['EmailFrom'];
                            $EmailT = $_POST['EmailTo'];
                            $EmailDat = $_POST['EmailDate'];
                            $EmailSubjec = $_POST['EmailSubject'];
                            $EmailMessag = $_POST['EmailMessage'];
                            
                            foreach ($EmailFro as $i => $temp) {
                              $EmailFrom[$i] = $temp;
                            }
                            foreach ($EmailT as $i => $temp) {
                              $EmailTo[$i] = $temp;
                            } 
                            foreach ($EmailDat as $i => $temp) {
                              $EmailDate[$i] = $temp;
                            }
                            foreach ($EmailSubjec as $i => $temp) {
                              $EmailSubject[$i] = $temp;
                            }
                            foreach ($EmailMessag as $i => $temp) {
                              $EmailMessage[$i] = $temp;
                            }
                            
                            // With the remaining foreach loops being
                            
                            foreach ($Dat as $i => $temp) {
                              $Date[$i] = $temp;
                            }
                            foreach ($Staf as $i => $temp) {
                              $Staff[$i] = $temp;
                            }
                            foreach ($Wag as $i => $temp) {
                              $Wage[$i] = $temp;
                            }
                            foreach ($Hour as $i => $temp) {
                              $Hours[$i] = $temp;
                            }
                            foreach ($SubDat as $i => $temp) {
                              $SubDate[$i] = $temp;
                            }
                            foreach ($Sub as $i => $temp) {
                              $Subs[$i] = $temp;
                            }
                            foreach ($Pric as $i => $temp) {
                              $Price[$i] = $temp;
                            }
                            foreach ($Typ as $i => $temp) {
                              $Type[$i] = $temp;
                            }
                            I get errors of

                            Warning: Invalid argument supplied for foreach() in /home/moneill/public_html/mainPHP.php on line 231

                            Notice: Undefined offset: 1 in /home/moneill/public_html/mainPHP.php on line 238

                            and the period of coding that this represents is

                            Code:
                            // A function to allow overall project variables to be manipulated.
                            function makeVariableProjectDetails()
                            {
                                global $ProjName, $projectFloat, $projectExtension, $projectExcusable, $projectNonExcusable,
                                    $projectDelays, $projectValue, $liquidateddamages, $totalliquidateddamages;
                            		
                            	$temp = mysql_query("SELECT Name, ProjectFloat, Extension, Excusable, NonExcusable, Delays, Value, LiquidatedDamages, totalLiquidatedDamages 
                                          FROM     ProjectDetails
                                          WHERE    Name= '$ProjName'")or die('<hr />MySQL Error1: ' .mysql_error(). '<hr />');
                            	$temp = mysql_fetch_array($temp); 
                                $projectFloat = $temp['ProjectFloat'];
                                $projectExtension = $temp['Extension'];
                                $projectExcusable = $temp['Excusable'];
                                $projectNonExcusable = $temp['NonExcusable'];
                                $projectDelays = $temp['Delays'];
                                $projectValue = $temp['Value'];
                                $liquidateddamages = $temp['LiquidatedDamages'];
                                $totalliquidateddamages = $temp['totalLiquidatedDamages'];
                            }
                            Where line 231 is $projectFloat ... and 238 is $liquidatedDama ges?

                            The reason why I want to call a variable by for loop is that the 800 lines of code have a lot of for loops and the program, as well as 20000 words has to be in for wednesddday morning so im a bit caught for time. Any one any ideas of the problem?

                            Comment

                            • pbmods
                              Recognized Expert Expert
                              • Apr 2007
                              • 5821

                              #15
                              Save your server some CPU cycles and just do:
                              [code=php]
                              $EmailFrom = $_POST['EmailFrom'];
                              $EmailTo = $_POST['EmailTo'];
                              $EmailDate = $_POST['EmailDate'];
                              $EmailSubject = $_POST['EmailSubject'];
                              $EmailMessage = $_POST['EmailMessage'];
                              [/code]

                              Its result is identical to that of the code that you posted.

                              Comment

                              Working...