Send Dynamic form Values to PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ak1dnar
    Recognized Expert Top Contributor
    • Jan 2007
    • 1584

    Send Dynamic form Values to PHP

    Hi, I have created a web application that enables to Delete Items in a product table using check boxes.
    I Need to Display each and every product in <TR> and with the product there is a corresponding check box to delete it, use can select the check box and then he will press the Update Button in the Form to submit for processing.

    [PHP]
    //Here I will get the Values from MySQL Table
    //This Area is Inside a HTML form with Update button and On Submit it will pass //to PHP page for precessing.

    while($row = mysql_fetch_ass oc($result))
    {
    echo '<tr>
    <td>'.$row['p_name'].'</td><td>
    <input type="checkbox" name="pid" value="'.$row['p_id'].'" /><td>
    </tr>';
    }
    [/PHP]

    But the problem is here in my coding as the checkbox name I am giving the name as pid.
    Since i have put it inside while loop for 5 products there will be 5 check boxes.
    Then name will create duplicates.

    So then I cant get this Value from PHP side. Only one of the check box value will come to PHP page.
    So How do i send all the selected check box values when user Submit the Form.
  • gauravgmbhr
    New Member
    • Feb 2007
    • 107

    #2
    Originally posted by ajaxrand
    Hi, I have created a web application that enables to Delete Items in a product table using check boxes.
    I Need to Display each and every product in <TR> and with the product there is a corresponding check box to delete it, use can select the check box and then he will press the Update Button in the Form to submit for processing.

    [PHP]
    //Here I will get the Values from MySQL Table
    //This Area is Inside a HTML form with Update button and On Submit it will pass //to PHP page for precessing.

    while($row = mysql_fetch_ass oc($result))
    {
    echo '<tr>
    <td>'.$row['p_name'].'</td><td>
    <input type="checkbox" name="pid" value="'.$row['p_id'].'" /><td>
    </tr>';
    }
    [/PHP]

    But the problem is here in my coding as the checkbox name I am giving the name as pid.
    Since i have put it inside while loop for 5 products there will be 5 check boxes.
    Then name will create duplicates.

    So then I cant get this Value from PHP side. Only one of the check box value will come to PHP page.
    So How do i send all the selected check box values when user Submit the Form.

    i had the very same problem

    the solution is very simple
    dont use the name="pid"

    but use
    Code:
     name="pid[]"

    now when u recieve the value in PHP
    then
    var_dump($_GET[pid]) will b an array that will contain all ur selected values

    Comment

    • gauravgmbhr
      New Member
      • Feb 2007
      • 107

      #3
      Originally posted by gauravgmbhr
      i had the very same problem

      the solution is very simple
      dont use the name="pid"

      but use
      Code:
       name="pid[]"

      now when u recieve the value in PHP
      then
      var_dump($_GET[pid]) will b an array that will contain all ur selected values



      the other solition is u can have a $i varible
      and name="pid{$i}";
      $i++;


      so each check box will have names as
      pid1
      pid2..........


      but that will make ur destination script complicated
      so try using the first idea
      which is more reliable
      and employed and tested by me

      Comment

      • ak1dnar
        Recognized Expert Top Contributor
        • Jan 2007
        • 1584

        #4
        Thanks I made it but now there is a small bug.
        to submit the Form I am using a image and onclick of the image i will call for the JS function . then JS will Submit the form.


        [HTML]
        <!--This is in side the form, it will make multiple check boxes according to the result -->

        <input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />
        <!--More Comes here -->

        <!-- My Submit image -->
        <a href="javascrip t:update()"><im g src="images_in/b_update.jpg" alt="Update" width="64" height="27" border="0" /></a>[/HTML]

        JS function
        Code:
        function update(){
        	document.inq_form.action="update.php";
        	document.inq_form.submit();
        	}
        Problem is if i am submitting form to update.php with out selecting a checkbox from the list Error will come.

        Code:
        Warning: implode() [function.implode]: Bad arguments. in D:\....\update.php on line 4
        update.php
        [PHP]
        require 'dbcon.php';//MySQL connectivity
        $frm_chk_delete = $_REQUEST['frm_chk_delete '];
        $list_of_ids = implode(",", $frm_chk_delete );
        mysql_query("DE LETE FROM cart WHERE p_id IN ($list_of_ids)" );
        header('Locatio n:previouspage. php');
        [/PHP]

        How to Avoid This error...for that I have to check whether user has selected a check box or Not.

        Comment

        • gauravgmbhr
          New Member
          • Feb 2007
          • 107

          #5
          Originally posted by ajaxrand
          Thanks I made it but now there is a small bug.
          to submit the Form I am using a image and onclick of the image i will call for the JS function . then JS will Submit the form.


          [HTML]
          <!--This is in side the form, it will make multiple check boxes according to the result -->

          <input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />
          <!--More Comes here -->

          <!-- My Submit image -->
          <a href="javascrip t:update()"><im g src="images_in/b_update.jpg" alt="Update" width="64" height="27" border="0" /></a>[/HTML]

          JS function
          Code:
          function update(){
          	document.inq_form.action="update.php";
          	document.inq_form.submit();
          	}
          Problem is if i am submitting form to update.php with out selecting a checkbox from the list Error will come.

          Code:
          Warning: implode() [function.implode]: Bad arguments. in D:\....\update.php on line 4
          update.php
          [PHP]
          require 'dbcon.php';//MySQL connectivity
          $frm_chk_delete = $_REQUEST['frm_chk_delete '];
          $list_of_ids = implode(",", $frm_chk_delete );
          mysql_query("DE LETE FROM cart WHERE p_id IN ($list_of_ids)" );
          header('Locatio n:previouspage. php');
          [/PHP]

          How to Avoid This error...for that I have to check whether user has selected a check box or Not.


          Try Using Var_dump on ur $_REQUEST['frm_chk_delete '] In order to check whether its an array or Not


          i Guess it will be an array with all values = '.$id.' yes '.$id.' not the values u want to send

          so when u implode it then $id will be treated as a php variable wich is blank
          thatz y the warning is comin

          so dont use
          <input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />


          But use

          Code:
          <input type="checkbox" name="frm_chk_delete[]" value="<?php echo $id ?>" />

          the reason for using echo $id is that outside the PHP island $id is a string ='$id' not the value it contain


          PHP send plain HTML code to the browser
          so PHP is used to write the HTML code
          USE the <?php echo $id?> instead od '$id' directly.
          M sure ur script will work

          Comment

          • gauravgmbhr
            New Member
            • Feb 2007
            • 107

            #6
            And One more thing
            Try handling the errors so that the warning does not appear when user do not select any of the checkboxes

            TRY

            if(sizeof($frm_ chk_delete)==0) {
            echo "atleast one of the check boxes";
            header('locatio n: previospage.php ');
            }
            else
            {
            implode(bla bla bla);
            }


            errors should be handle because implode will produce warnind not an error
            so the script wont stop

            and the SQL statement will be executed
            and produce a database error

            u dont wanna tell anyone bout ue database schema


            if u wanna study how to handle SQL statement in PHP
            read this

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Originally posted by gauravgmbhr
              Try Using Var_dump on ur $_REQUEST['frm_chk_delete '] In order to check whether its an array or Not


              i Guess it will be an array with all values = '.$id.' yes '.$id.' not the values u want to send

              so when u implode it then $id will be treated as a php variable wich is blank
              thatz y the warning is comin

              so dont use
              <input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />


              But use

              Code:
              <input type="checkbox" name="frm_chk_delete[]" value="<?php echo $id ?>" />

              the reason for using echo $id is that outside the PHP island $id is a string ='$id' not the value it contain


              PHP send plain HTML code to the browser
              so PHP is used to write the HTML code
              USE the <?php echo $id?> instead od '$id' directly.
              M sure ur script will work
              My HTML coding will Print dynamically from PHP. You have misunderstood my Problem. this $pid will always Give the value to the check box (ex: 1001,1002) if there is a checkbox and that i can pass to the PHP page with values successfully.It will Never print as $pid in value attribute.

              [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]

              The problem is here with out selecting a check box if i submit this form that implode function will get this as a Bad Argument. because Here at the array NO values.
              Because i did not select a check box.

              Comment

              • gauravgmbhr
                New Member
                • Feb 2007
                • 107

                #8
                Originally posted by ajaxrand
                My HTML coding will Print dynamically from PHP. You have misunderstood my Problem. this $pid will always Give the value to the check box (ex: 1001,1002) if there is a checkbox and that i can pass to the PHP page with values successfully.It will Never print as $pid in value attribute.

                [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]

                The problem is here with out selecting a check box if i submit this form that implode function will get this as a Bad Argument. because Here at the array NO values.
                Because i did not select a check box.

                C Now ur are Putting all the <input......... ............... ....... in the PHP echo
                u didnt do it in first time

                If u Put the statement Inside PHP echo LIke u did now
                [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]

                this will work for sure

                Comment

                • gauravgmbhr
                  New Member
                  • Feb 2007
                  • 107

                  #9
                  still U have an error In ur HTML script


                  [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]



                  u r using value=" ' .$id. ' " but u should use

                  value = ' " .$id. " '

                  c the diff in quotes


                  READ THE FINAL HTML SCRIPT AFTER THE PAGE LOAD
                  BY USING view source option
                  Then U will C that U r making mistake In HTML, so the values Are Not being passed correcly


                  Or Use var_dump($_REQU EST['frm_chk_delete ']) U will C that its not recieving ur desired values

                  Comment

                  • ak1dnar
                    Recognized Expert Top Contributor
                    • Jan 2007
                    • 1584

                    #10
                    Originally posted by gauravgmbhr
                    still U have an error In ur HTML script
                    u r using value=" ' .$id. ' " but u should use

                    value = ' " .$id. " '

                    c the diff in quotes


                    READ THE FINAL HTML SCRIPT AFTER THE PAGE LOAD
                    BY USING view source option
                    Then U will C that U r making mistake In HTML, so the values Are Not being passed correcly


                    Or Use var_dump($_REQU EST['frm_chk_delete ']) U will C that its not recieving ur desired values

                    Read this http://www.thescripts.com/forum/thread1707.html
                    I made my Script to work, based on this POST.

                    To delete the values i can use this PHP script.
                    [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]

                    And No need to Pass Like this as you say :)
                    [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value = ' " .$id. " ' />';[/PHP]

                    and Any one can understand why peoples are using value="'.$id.'"
                    with their coding except newbies. If some one can Get a Value to PHP variable again he can pass it in this way :
                    [PHP]
                    $id = "The value Goes Here";
                    echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]

                    Thanks :)

                    Comment

                    • gauravgmbhr
                      New Member
                      • Feb 2007
                      • 107

                      #11
                      Originally posted by ajaxrand
                      Read this http://www.thescripts.com/forum/thread1707.html
                      I made my Script to work, based on this POST.

                      To delete the values i can use this PHP script.
                      [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]

                      And No need to Pass Like this as you say :)
                      [PHP]echo '<input type="checkbox" name="frm_chk_d elete[]" value = ' " .$id. " ' />';[/PHP]

                      and Any one can understand why peoples are using value="'.$id.'"
                      with their coding except newbies. If some one can Get a Value to PHP variable again he can pass it in this way :
                      [PHP]
                      $id = "The value Goes Here";
                      echo '<input type="checkbox" name="frm_chk_d elete[]" value="'.$id.'" />';[/PHP]

                      Thanks :)


                      SORRY MY MISTAKE
                      I DIDNT NOTICE THA U R USING SINGLE QUOTES TO PRINT THE VALUE
                      AND TOLD U TO USE value=' " .$id. "' coz i thought u r using double qotes to print ur value and sigle quotes to quote the id values

                      ur method is correct

                      Comment

                      • gauravgmbhr
                        New Member
                        • Feb 2007
                        • 107

                        #12
                        and i put spaces between the double and single quotes so that it will be distinguishable here On the site for diplay, not that its used that way

                        Comment

                        • gauravgmbhr
                          New Member
                          • Feb 2007
                          • 107

                          #13
                          i personally use double quotes for echoing HTML statement con then i dont need to exit and re-enter the quotes like value=" ' .$id. ' "

                          if u use double quotes the u can directly use


                          echo "<input type=\"checkbox \" name=\"whatever \" value=\"$id\">"
                          and so on

                          Comment

                          • ak1dnar
                            Recognized Expert Top Contributor
                            • Jan 2007
                            • 1584

                            #14
                            Thanks for that bro. I combined your Error trapping system to my Script.Now the Script is Ready to Fly.

                            [PHP]require 'dbcon.php';
                            $frm_chk_delete = $_REQUEST['frm_chk_delete '];
                            if(sizeof($frm_ chk_delete) == 0){
                            echo 'Please Select check boxes';
                            /*
                            I'll Do something Better Here to send the Error Back to
                            the_page_I_came _from.php
                            */
                            }else{
                            $list_of_ids = implode(",", $frm_chk_delete );
                            mysql_query("DE LETE FROM MyTable WHERE p_id IN ($list_of_ids)" );
                            header('Locatio n: the_page_I_came _from.php');
                            }[/PHP]

                            Wish to c u again :)

                            Comment

                            • gauravgmbhr
                              New Member
                              • Feb 2007
                              • 107

                              #15
                              Any time Brother
                              not a problem

                              Comment

                              Working...