add table rows to html and send to mysql [js,php,mysql,html]

  • dec
  • Born
  • Born
  • dec
  • Posts: 2

Post 3+ Months Ago

Hi, I want my page to get information from user <input>, if user prefers, add more same rows and then send all those in array on smth to MYSQL.
If I want to send 2 or more rows, PHP picks up and send just the last row of information to MYSQL. And I want to stay on the same page (now redirects to send.php).

Here is what I wrote:

index.PHP
Code: [ Select ]
<style type="text/css">
table,tr,td{border:0px solid black;}
</style>

  <table id="titlebar" cellspacing="0px">
        <tr>
            <td style="width:20px;">&#10003;</td>
            <td style="width:160px;">Show</td>
            <td style="width:62px;">season</td>
            <td style="width:63px;">episode</td>
            <td style="width:100px;">language</td>
            <td style="width:190px;">Link 1</td>
            <td style="width:190px;">Link 2</td>
        </tr>
    </table>
    <form action="send.php" method="POST">
  <table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
    <tr>
      <td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
      <td><INPUT type="text" name="series" style="width:160px;" autocomplete="on" placeholder="Show" required/></td>
            <td><INPUT type="text" name="season" style="width:62px;" autocomplete="on" placeholder="season" required/></td>
            <td><INPUT type="text" name="episode" style="width:63px;" autocomplete="on" placeholder="episode" required/></td>
      <td>
        <SELECT name="language" style="width:100px;">
          <OPTION value="one">one</OPTION>
          <OPTION value="two">two</OPTION>
          <OPTION value="three">three</OPTION>
          <OPTION value="four">four</OPTION>
          <OPTION value="five">five</OPTION>
          <OPTION value="six">six</OPTION>
          <OPTION value="seven">seven</OPTION>
        </SELECT>
      </td>
            <td><INPUT type="text" name="link_1" style="width:190px;" autocomplete="on" placeholder="link 1" required/></td>
            <td><INPUT type="text" name="link_2" style="width:190px;" autocomplete="on" placeholder="link 2" required/></td>
    </tr>
  </table>
    <INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
  <INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />
    <INPUT type="submit" value="Send"/>
    </form>
  1. <style type="text/css">
  2. table,tr,td{border:0px solid black;}
  3. </style>
  4.   <table id="titlebar" cellspacing="0px">
  5.         <tr>
  6.             <td style="width:20px;">&#10003;</td>
  7.             <td style="width:160px;">Show</td>
  8.             <td style="width:62px;">season</td>
  9.             <td style="width:63px;">episode</td>
  10.             <td style="width:100px;">language</td>
  11.             <td style="width:190px;">Link 1</td>
  12.             <td style="width:190px;">Link 2</td>
  13.         </tr>
  14.     </table>
  15.     <form action="send.php" method="POST">
  16.   <table id="dataTable" width="auto" style="margin:-4px 0 0 0;" cellspacing="0px">
  17.     <tr>
  18.       <td style="width:20px;"><INPUT type="checkbox" name="chk" /></td>
  19.       <td><INPUT type="text" name="series" style="width:160px;" autocomplete="on" placeholder="Show" required/></td>
  20.             <td><INPUT type="text" name="season" style="width:62px;" autocomplete="on" placeholder="season" required/></td>
  21.             <td><INPUT type="text" name="episode" style="width:63px;" autocomplete="on" placeholder="episode" required/></td>
  22.       <td>
  23.         <SELECT name="language" style="width:100px;">
  24.           <OPTION value="one">one</OPTION>
  25.           <OPTION value="two">two</OPTION>
  26.           <OPTION value="three">three</OPTION>
  27.           <OPTION value="four">four</OPTION>
  28.           <OPTION value="five">five</OPTION>
  29.           <OPTION value="six">six</OPTION>
  30.           <OPTION value="seven">seven</OPTION>
  31.         </SELECT>
  32.       </td>
  33.             <td><INPUT type="text" name="link_1" style="width:190px;" autocomplete="on" placeholder="link 1" required/></td>
  34.             <td><INPUT type="text" name="link_2" style="width:190px;" autocomplete="on" placeholder="link 2" required/></td>
  35.     </tr>
  36.   </table>
  37.     <INPUT type="button" value="Add row" onclick="addRow('dataTable')" />
  38.   <INPUT type="button" value="Delete row" onclick="deleteRow('dataTable')" />
  39.     <INPUT type="submit" value="Send"/>
  40.     </form>


javascript (to add and delete rows)
Code: [ Select ]
function addRow(tableID) {

    var table = document.getElementById(tableID);

    var rowCount = table.rows.length;
    var row = table.insertRow(rowCount);

    var colCount = table.rows[0].cells.length;

    for(var i=0; i<colCount; i++) {

        var newcell = row.insertCell(i);

        newcell.innerHTML = table.rows[0].cells[i].innerHTML;
        //alert(newcell.childNodes);
        switch(newcell.childNodes[0].type) {
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "text":
                newcell.childNodes[0].value = "";
                break;
            case "checkbox":
                newcell.childNodes[0].checked = false;
                break;
            case "select-one":
                newcell.childNodes[0].selectedIndex = 0;
                break;
        }
    }
}

function deleteRow(tableID) {
    try {
    var table = document.getElementById(tableID);
    var rowCount = table.rows.length;

    for(var i=0; i<rowCount; i++) {
        var row = table.rows[i];
        var chkbox = row.cells[0].childNodes[0];
        if(null != chkbox && true == chkbox.checked) {
            if(rowCount <= 1) {
                alert("Cant delete all rows");
                break;
            }
            table.deleteRow(i);
            rowCount--;
            i--;
        }

    }
    }catch(e) {
        alert(e);
    }
}
  1. function addRow(tableID) {
  2.     var table = document.getElementById(tableID);
  3.     var rowCount = table.rows.length;
  4.     var row = table.insertRow(rowCount);
  5.     var colCount = table.rows[0].cells.length;
  6.     for(var i=0; i<colCount; i++) {
  7.         var newcell = row.insertCell(i);
  8.         newcell.innerHTML = table.rows[0].cells[i].innerHTML;
  9.         //alert(newcell.childNodes);
  10.         switch(newcell.childNodes[0].type) {
  11.             case "text":
  12.                 newcell.childNodes[0].value = "";
  13.                 break;
  14.             case "text":
  15.                 newcell.childNodes[0].value = "";
  16.                 break;
  17.             case "text":
  18.                 newcell.childNodes[0].value = "";
  19.                 break;
  20.             case "text":
  21.                 newcell.childNodes[0].value = "";
  22.                 break;
  23.             case "text":
  24.                 newcell.childNodes[0].value = "";
  25.                 break;
  26.             case "checkbox":
  27.                 newcell.childNodes[0].checked = false;
  28.                 break;
  29.             case "select-one":
  30.                 newcell.childNodes[0].selectedIndex = 0;
  31.                 break;
  32.         }
  33.     }
  34. }
  35. function deleteRow(tableID) {
  36.     try {
  37.     var table = document.getElementById(tableID);
  38.     var rowCount = table.rows.length;
  39.     for(var i=0; i<rowCount; i++) {
  40.         var row = table.rows[i];
  41.         var chkbox = row.cells[0].childNodes[0];
  42.         if(null != chkbox && true == chkbox.checked) {
  43.             if(rowCount <= 1) {
  44.                 alert("Cant delete all rows");
  45.                 break;
  46.             }
  47.             table.deleteRow(i);
  48.             rowCount--;
  49.             i--;
  50.         }
  51.     }
  52.     }catch(e) {
  53.         alert(e);
  54.     }
  55. }


send.php
Code: [ Select ]
<?php

include('mysql.php');

$seriesV = mysql_real_escape_string(stripslashes($_POST['series']));
$seasonV = mysql_real_escape_string(stripslashes($_POST['season']));
$episodeV = mysql_real_escape_string(stripslashes($_POST['episode']));
$languageV = mysql_real_escape_string(stripslashes($_POST['language']));
$link_1V = mysql_real_escape_string(stripslashes($_POST['link_1']));
$link_2V = mysql_real_escape_string(stripslashes($_POST['link_2']));

$sql="INSERT INTO episodes (seriesDB,seasonDB,episodeDB,langDB,link_1DB,link_2DB,dateDB)
 VALUES
 ('$seriesV','$seasonV','$episodeV','$languageV','$link_1V','$link_2V','".time()."')";


if (!mysql_query($sql,$con))
 {
 die('Error: ' . mysql_error());
 }
 echo "1 record added";

mysql_close($con);
?>
  1. <?php
  2. include('mysql.php');
  3. $seriesV = mysql_real_escape_string(stripslashes($_POST['series']));
  4. $seasonV = mysql_real_escape_string(stripslashes($_POST['season']));
  5. $episodeV = mysql_real_escape_string(stripslashes($_POST['episode']));
  6. $languageV = mysql_real_escape_string(stripslashes($_POST['language']));
  7. $link_1V = mysql_real_escape_string(stripslashes($_POST['link_1']));
  8. $link_2V = mysql_real_escape_string(stripslashes($_POST['link_2']));
  9. $sql="INSERT INTO episodes (seriesDB,seasonDB,episodeDB,langDB,link_1DB,link_2DB,dateDB)
  10.  VALUES
  11.  ('$seriesV','$seasonV','$episodeV','$languageV','$link_1V','$link_2V','".time()."')";
  12. if (!mysql_query($sql,$con))
  13.  {
  14.  die('Error: ' . mysql_error());
  15.  }
  16.  echo "1 record added";
  17. mysql_close($con);
  18. ?>


mysql.php (connects to mysql)
Code: [ Select ]
<?php
$con = mysql_connect('localhost', 'user', 'pass') or die ('Error, cant login to DB');
mysql_select_db('tvseries') or die("Unable to select database");

?>
  1. <?php
  2. $con = mysql_connect('localhost', 'user', 'pass') or die ('Error, cant login to DB');
  3. mysql_select_db('tvseries') or die("Unable to select database");
  4. ?>


Attachments:
concept.png

the concept that is now created



sorry my english
  • demonmaestro
  • Gold Member
  • Gold Member
  • User avatar
  • Posts: 679
  • Loc: Conroe, Texas

Post 3+ Months Ago

From what i can gather from what you are asking is that once your form is submited that you want it to go back to your index.php page.
Look at line 24 that i had added. Also welcome to OZZU!

Code: [ Select ]
01.<?php
02.
03.include('mysql.php');
04.
05.$seriesV = mysql_real_escape_string(stripslashes($_POST['series']));
06.$seasonV = mysql_real_escape_string(stripslashes($_POST['season']));
07.$episodeV = mysql_real_escape_string(stripslashes($_POST['episode']));
08.$languageV = mysql_real_escape_string(stripslashes($_POST['language']));
09.$link_1V = mysql_real_escape_string(stripslashes($_POST['link_1']));
10.$link_2V = mysql_real_escape_string(stripslashes($_POST['link_2']));
11.
12.$sql="INSERT INTO episodes (seriesDB,seasonDB,episodeDB,langDB,link_1DB,link_2DB,dateDB)
13. VALUES
14. ('$seriesV','$seasonV','$episodeV','$languageV','$link_1V','$link_2V','".time()."')";
15.
16.
17.if (!mysql_query($sql,$con))
18. {
19. die('Error: ' . mysql_error());
20. }
21. echo "1 record added";
22.
23.mysql_close($con);
24.header('Location:index.php');
25.?>
  1. 01.<?php
  2. 02.
  3. 03.include('mysql.php');
  4. 04.
  5. 05.$seriesV = mysql_real_escape_string(stripslashes($_POST['series']));
  6. 06.$seasonV = mysql_real_escape_string(stripslashes($_POST['season']));
  7. 07.$episodeV = mysql_real_escape_string(stripslashes($_POST['episode']));
  8. 08.$languageV = mysql_real_escape_string(stripslashes($_POST['language']));
  9. 09.$link_1V = mysql_real_escape_string(stripslashes($_POST['link_1']));
  10. 10.$link_2V = mysql_real_escape_string(stripslashes($_POST['link_2']));
  11. 11.
  12. 12.$sql="INSERT INTO episodes (seriesDB,seasonDB,episodeDB,langDB,link_1DB,link_2DB,dateDB)
  13. 13. VALUES
  14. 14. ('$seriesV','$seasonV','$episodeV','$languageV','$link_1V','$link_2V','".time()."')";
  15. 15.
  16. 16.
  17. 17.if (!mysql_query($sql,$con))
  18. 18. {
  19. 19. die('Error: ' . mysql_error());
  20. 20. }
  21. 21. echo "1 record added";
  22. 22.
  23. 23.mysql_close($con);
  24. 24.header('Location:index.php');
  25. 25.?>
  • dec
  • Born
  • Born
  • dec
  • Posts: 2

Post 3+ Months Ago

I figured out code myself, here's the code if someone needs

Code: [ Select ]
<?php
if(isset($_REQUEST['submit'])) {
include('mysql.php');
$count = 0;
$count = count($_POST['lang']) - 1;
for($i = 0; $i <= $count; $i++){
  $seriesV = mysql_real_escape_string($_POST['series'][$i]);
  $seasonV = mysql_real_escape_string($_POST['season'][$i]);
  $episodeV = mysql_real_escape_string($_POST['episode'][$i]);
  $languageV = mysql_real_escape_string($_POST['language'][$i]);
  $link_1V = mysql_real_escape_string($_POST['link_1'][$i]);
  $link_2V = mysql_real_escape_string($_POST['link_2'][$i]);
  $sql=mysql_query("INSERT INTO episodes (seriesDB,seasonDB,episodeDB,langdb,link_1DB,link_2DB,dateDB)
   VALUES
   ('$seriesV','$seasonV','$episodeV','$languageV','$link_1V','$link_2V','".time()."')") or die(mysql_error());
};

mysql_close();
}

header('Location: index.php');
?>
  1. <?php
  2. if(isset($_REQUEST['submit'])) {
  3. include('mysql.php');
  4. $count = 0;
  5. $count = count($_POST['lang']) - 1;
  6. for($i = 0; $i <= $count; $i++){
  7.   $seriesV = mysql_real_escape_string($_POST['series'][$i]);
  8.   $seasonV = mysql_real_escape_string($_POST['season'][$i]);
  9.   $episodeV = mysql_real_escape_string($_POST['episode'][$i]);
  10.   $languageV = mysql_real_escape_string($_POST['language'][$i]);
  11.   $link_1V = mysql_real_escape_string($_POST['link_1'][$i]);
  12.   $link_2V = mysql_real_escape_string($_POST['link_2'][$i]);
  13.   $sql=mysql_query("INSERT INTO episodes (seriesDB,seasonDB,episodeDB,langdb,link_1DB,link_2DB,dateDB)
  14.    VALUES
  15.    ('$seriesV','$seasonV','$episodeV','$languageV','$link_1V','$link_2V','".time()."')") or die(mysql_error());
  16. };
  17. mysql_close();
  18. }
  19. header('Location: index.php');
  20. ?>


And you need to add square brackets for array name="season[]"
  • shumied
  • Born
  • Born
  • shumied
  • Posts: 1

Post 3+ Months Ago

Hello, I'm taking this example to do a job that I need, although I can not succeed to try to rescue all sent the form values, only take the last entered value. Any idea that this bad example?
  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6254
  • Loc: Seattle, WA

Post 3+ Months Ago

Did you try using the most recent version of the code in the post directly above yours?
  • [yonaiker_357+]
  • Born
  • Born
  • [yonaiker_357+]
  • Posts: 1

Post 3+ Months Ago

Hello friends I am new to the community, greetings from venezuela &lt;!-dotdotdot--> very useful this code exactly what I wanted because I was tangling trying to duplicate with php &lt;!-dotdotdot--> its possible that all fields marked are send to the database?

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 95 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.