Selecting Table Rows for update, edit or delete

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prosad
    New Member
    • Jul 2007
    • 27

    Selecting Table Rows for update, edit or delete

    Hi!

    Have been hitting my head on the wall for two weeks now. am a php newbie. have a form that on submitting to a MySQL DB instantly generates a HTML table on the same page. what i need now is way to be able to select rows from this table. specific selected row info sent back to text-box in my multi-button form for further processing depending on which button i click. have been trying to study javascript onclcik event handlers to see if this could be the solution. my scripts for multi-button form and generated table below:

    1. Form:
    [code=php]<table id = "MAINFORM">
    <form name = "complaint_sett ings" method = "POST" action = "php_complaint_ mysql.php">

    <td>
    <!--<div align = "left"></div> -->
    <table border = "3" bordercolor = "gold" bgcolor = "00008B">
    <tr><td>

    <p style = "color:gold">Co mplaint: <br>
    <input type = "text" size = "50" maxlength = "50" name = "complaint" ></td></tr><br>
    <tr><td>
    <input type = "submit" name = "submit_c" value = "submit"> <br>
    <input type = "submit" name = "submit_c" value = "update"> <br>
    <input type = "submit" name = "submit_c" value = "delete" onClick = "return confirm('Are you sure you want to delete record?')">

    </td></tr>

    2. Generated Table based on submission from above form:

    //initializing extraction of complaint parameters from mysql
    $complaintTable = mysql_query("SE LECT * FROM complaint") or die (mysql_error()) ;

    //store complaint records into array $complaint_row &&
    //create table of mysql complaint array
    echo "<table>";
    echo "<td><div>" ;"</div></td>";
    echo "<table id = \"table_c\" border = \"4\" bordercolor = 00008B>";
    echo "<tr><th>Compla int</th>";
    while ($comp_rec= mysql_fetch_arr ay($complaintTa ble))
    {echo "<tr><td>"; echo $comp_rec['complaint']; echo "</td>";}
    echo "</tr></table>";[/code]

    [Please use CODE tags when posting source code. Thanks! --pbmods]

    THANKS IN ADVANCE.
  • epots9
    Recognized Expert Top Contributor
    • May 2007
    • 1352

    #2
    do u know AJAX? Tutorial

    that way using your javascript u can call php files. (different php files for delete, update, etc.)

    Comment

    • prosad
      New Member
      • Jul 2007
      • 27

      #3
      Originally posted by epots9
      do u know AJAX? Tutorial

      that way using your javascript u can call php files. (different php files for delete, update, etc.)
      sorry, dont know AJAX yet.
      just starting out with PHP, MySQL, Firebird and it seems am now being forced to learn javascritp just for this purpose on recovering text item from my HTML table.

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Give these buttons different names then you can test which one was clicked to submit the form
        Code:
        <input type = "submit" name = "select" value = "select"> <br>
        <input type = "submit" name = "update" value = "update"> <br>
        <input type = "submit" name = "delete" value = "delete"
        AJAX is for wimps. Then in the script called by "action" [PHP]//php_complaint_m ysql.php
        if(isset($_POST['select']))
        //do lots of select stuff
        if(isset($_POST['delete']))
        //do lots of delete stuff
        if(isset($_POST['update']))
        //do lots of update stuff [/PHP] Bit more tidying up to do but this is the idea

        Comment

        • prosad
          New Member
          • Jul 2007
          • 27

          #5
          Originally posted by code green
          Give these buttons different names then you can test which one was clicked to submit the form
          Code:
          <input type = "submit" name = "select" value = "select"> <br>
          <input type = "submit" name = "update" value = "update"> <br>
          <input type = "submit" name = "delete" value = "delete"
          AJAX is for wimps. Then in the script called by "action" [PHP]//php_complaint_m ysql.php
          if(isset($_POST['select']))
          //do lots of select stuff
          if(isset($_POST['delete']))
          //do lots of delete stuff
          if(isset($_POST['update']))
          //do lots of update stuff [/PHP] Bit more tidying up to do but this is the idea
          thanks for the tip. my different buttons work fine.
          the BIG Question though i have is that how do i retrieve exactly already submitted (posted) data that is being generated and displayed in my html table immediately after posting. the rows on my table are not clickable. i wish to click on any row on my table and data/item on that row will be returned to the form (text field) it was originally submitted from.
          html form and php, mysql generated table all shown in my source code. i wish to use form for submit, update, delete functions on my MySQL database through this single form and table. table to be used for just reselcting already posted data. form to be used for processing data (retrieved from my table).

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            i wish to click on any row on my table and data/item on that row will be returned to the form (text field) it was originally submitted from
            PHP can handle this very well. You need to wrap your data display line in a hyperlink, with the link returning the same page and your data being passed as a URL variable.
            I assume this is your data line
            [PHP]{echo "<tr><td>"; echo $comp_rec['complaint']; echo [/PHP]
            Please check my syntax because this is of the top of my head.
            Please note that I only use single quotes, because PHP parses them much faster than double.
            And I have inserted extra spaces for clarity only. Remove them.
            [PHP]{echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp _rec['complaint'].' " '.comp_rec['complaint']. '</a>';[/PHP]

            Comment

            • prosad
              New Member
              • Jul 2007
              • 27

              #7
              Originally posted by code green
              PHP can handle this very well. You need to wrap your data display line in a hyperlink, with the link returning the same page and your data being passed as a URL variable.
              I assume this is your data line
              [PHP]{echo "<tr><td>"; echo $comp_rec['complaint']; echo [/PHP]
              Please check my syntax because this is of the top of my head.
              Please note that I only use single quotes, because PHP parses them much faster than double.
              And I have inserted extra spaces for clarity only. Remove them.
              [PHP]{echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp _rec['complaint'].' " '.comp_rec['complaint']. '</a>';[/PHP]
              ur script:
              [PHP]{echo '<tr><td><a href="'.$_SERVE R['PHP_SELF'].'?comp='.$comp _rec['complaint'].' "'.$comp_re c['complaint'].'</a>';}[/PHP]

              thanks for the effort. though does not work cos i submit to a script that validates then inserts into a MySQL DB. as soon as database insert is carried out the data row is generated with the new data item just inserted into DB. using your script it does not return any data to my "complaint" table for display on page even though it has succesfully inserted data item in MySQL DB. my Database submit script is given below and as u can see it returns thro header to my submit page so user stays on same page. after submitting thro form - given above (form name: "complaint_sett ings") - Now the submit page (csu_settings.p hp) will have a single difference. HTML table displayed below form includes newly submited item fetched from database as new row. THE QUESTION??? HOW DO I SELECT THESE ITEMS ANY OF MY TABLE ROWS SUCH THAT ON CLICKING ANY CELL DATA IT RETURNS INTO MY FORM (INPUT TYPE = TEXT) THAT IS DISPLAYED ABOVE THE TABLE FOR OTHER ACTION SUCH AS UPDATE, DELETE . DIFFERENT BUTTONS ON FORM DETERMINES APPROPRIATE SCRIPT TO SEND DATA ITEM FOR PROCESSING UPDATE, DELETE ETC.

              [PHP]//inserting record in complaint table
              mysql_select_db ("CSU", $con);
              $sql="INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
              if (!mysql_query($ sql,$con)) {die('Error: ' . mysql_error()); }

              mysql_close($co n)

              ;};}

              header('locatio n:http://localhost/CSU_Project/csu_settings.ph p'); [/PHP]

              [PHP]//initializing extraction of complaint parameters from mysql
              $complaintTable = mysql_query("SE LECT * FROM complaint") or die (mysql_error()) ;

              //store complaint records into array $complaint_row &&
              //create table of mysql complaint array that is displayed under submit form on csu_settings.ph p page.

              echo "<table>";
              echo "<td><div>" ;"</div></td>";
              echo "<table id = \"table_c\" border = \"4\" bordercolor = 00008B>";
              echo "<tr><th>Compla int</th>";
              while ($comp_rec= mysql_fetch_arr ay($complaintTa ble))[/PHP]

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                I suggesting passing the variables as URL variables
                Code:
                echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';
                The following does not pass anything anywhere
                Code:
                (header 'location:http://localhost/CSU_Project/csu_settings.php');
                using your script it does not return any data to my "complaint" table
                You must read the URL variables using GET and not POST
                Code:
                INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";

                Comment

                • prosad
                  New Member
                  • Jul 2007
                  • 27

                  #9
                  Originally posted by code green
                  I suggesting passing the variables as URL variables
                  Code:
                  echo '<tr><td><a href=" '.$_SERVER['PHP_SELF'].'?comp='.$comp_rec['complaint'].' " '.comp_rec['complaint']. '</a>';
                  The following does not pass anything anywhere
                  Code:
                  (header 'location:http://localhost/CSU_Project/csu_settings.php');
                  You must read the URL variables using GET and not POST
                  Code:
                  INSERT INTO complaint (complaint) VALUES ('$_POST[complaint]')";
                  thanks code green,
                  solved my problem using Javascript onclick, can click on any cell in a dynamic table and it will pass the innerText object value to my form text field. parts of code given below:

                  Code:
                  <script type="text/javascript">
                  function select_cell(obj)
                  {
                      document.getElementById('trial').value = obj.innerText; 
                      document.getElementById('complaint').value = obj.innerText;
                      document.getElementById('selected_form');
                  }
                  [PHP]echo "<tr><th>Compla int</th>";
                  while ($comp_rec= mysql_fetch_arr ay($complaintTa ble))
                  {echo "<tr><td id = 'complaint' onclick='select _cell(this);'>" ; echo $comp_rec['complaint'];
                  echo "</td>";[/PHP]

                  [HTML]<table id = "MAINFORM">
                  <form id = "selected_f orm" name = "complaint_sett ings" method = "POST" action = "php_complaint_ mysql.php">
                  <td>
                  <table border = "3" bordercolor = "gold" bgcolor = "00008B">
                  <tr><td>
                  <p style = "color:gold">Co mplaint: <br>
                  <input type = "text" size = "50" maxlength = "50" name = "complaint" >
                  </td></tr><br>
                  <tr><td>
                  <input type = "submit" name = "submit_c" value = "submit"> <br>
                  <input type = "submit" name = "submit_c" value = "update"> <br>
                  <input type = "submit" name = "submit_c" value = "delete" onClick = "return confirm('Are you sure you want to delete record?')">[/HTML]

                  my problem now is how to pass along my primary key which is the unique id of each $comp_rec to my form as a hidden variable so i can pass $_POST['complaint'] to php_complaint_m ysql.php for update or delete. As it is now the table i use onclick only shows a single column(complain t) and even if i add another cell (id) the two cells are not carried and placed in separate fields on the same form. Any idea on populating form fields by clicking a row record with each attribute being passed to appropriate field? Attribute ID not presently on table. wish to keep hidden. But need to pass ID so i can use to update mysql. let me go wander around javascript forum.
                  thanks.

                  Comment

                  Working...