-1

I have an issue. I want my webpage to allow multiple entries in a table. However some of the column fields POST to search a database and return value which affects the following fields. Currently one entry can be done. However I want to make it into a table where I have an ADD ROW button which I have added javascript to do.

<button id="add_row" >Add Row</button>
<button id="delete_row">Delete Row</button>

<script type="text/javascript">
    $(document).ready(function(){
        var i=1;
        $("#add_row").click(function(){
            $('#addr'+i).html($('#addr0').html());

            $('#timeTable').append('<tr id="addr'+(i+1)+'"></tr>');
            i++;
        });
        $("#delete_row").click(function(){
            if(i>1){
                $("#addr"+(i-1)).html('');
                i--;
            }
        });

   });

Well this works until two things occur. One if I fill in the first field it POSTs and then refreshes the page which won't save the table. I have a working multiple POSTs site which is this example.

$task1 = $_POST["tasks1"];
$task2 = $_POST["tasks2"];
$task3 = $_POST["tasks3"];
$task4 = $_POST["tasks4"];
$task5 = $_POST["tasks5"];
$task6 = $_POST["tasks6"];
$task7 = $_POST["tasks7"];

Now I put this in place for all other aspects of the table, it works. It keeps the data even if I update another box after a refresh. Now I'm wondering if this can be made into an array so it becomes.

$task[x] = $_POST["task[x]"];

The reason I want the _POST to change is because it's a which if they all equal the same then my data is going to go into those, so I do want it to be an array because each of them needs to allow different values.

Basically I want to increment a variable through the javascript and associate it to each added row. I've looked and found "similar" issues but not exactly what I want. This is pretty specific. Finally, I have an even/odd table meaning a format change for every other row in the table. However when I use the Add Row button, it only does the first two rows then after that it no longer keeps the format. Suggestions?

<script type="text/javascript">
    $(document).ready(function(){
        $("tr:even").css("background-color", "#A71B95");
        $("tr:odd").css("background-color", "#F8D3F3");
    });
</script>

So after the 2nd row it no longer changes the background color.

Edit 08/08/2018

I've tried to use javascript. Here is what I have currently in the php portion. $text = $_POST["text1"]; $project1 = $_POST["projects1"];

    (Mysql) which populates $textA / $textB   for selection and $projectsA 
     and B

    <td align='center'>

                        <?php if(isset($_POST["text1"])){?>
              <select id = "text" name="text1" onchange="this.form.submit()">
                          <option value=''></option>
                          <? echo $textA; echo $textB;} 
                          else{?>
              <select id = "text" name="text1" onchange="this.form.submit()">
                              <option value=' '> </option>
                              <? echo $textB;}?>

                        </select>


                   </td>

                       <td align='center'>
                        <?php if(isset($_POST["submitForm"])){?>
                      <select name="projects1" onchange="this.form.submit()">
                          <option value=' '> </option>
                          <? echo $projectsA;?>
                          <? echo $projectsB;} 
                            elseif (isset($_POST["text1"])){?>
                      <select name="projects1" onchange="this.form.submit()">
                            <option value=' '> </option>
                            <? echo $projectsA;?>
                            <? echo $projectsB;} 
                                else{?>
                            <select name="projects1">
                                <option value=' '> </option>
                                <? }?>
                        </select>
                       </td>

Here is java

<button id="add_row" >Add Row</button>
<button id="delete_row">Delete Row</button>

<script>
$(document).ready(function(){
  var i=1;
  $("#add_row").click(function(){
  $('#addr'+i).html($('#addr0').html());


  $('#timeTable').append('<tr id="addr'+(i+1)+'"></tr>');

  i++;
    $("tr:even").css("background-color", "#A71B95");
  $("tr:odd").css("background-color", "#F8D3F3");
  });
  $("#delete_row").click(function(){
  if(i>1){
  $("#addr"+(i-1)).html('');
  i--;
  }
 });



$('#text').change(function(){



var text1 = $('#text').val();


$.post("website.php",
{text1: text},

function(text1)
{


    document.getElementsByName('text1').value= text1;
    }

    );
   });

});

</script>

So I'm trying to add a new row, then post the info so that it reads the first column change, then update the selection of the 2nd column based on that change. However I run into problems when I add lines, it refreshes the page and clears everything. Same with after I add rows. I want to add rows that have the same functionality. Hope this helps give a better picture of why I want the post but no refresh. And I want it to come into $_POST['text1'] so it can run the query and update for the next column. and again for every row entered.

dcary
  • 69
  • 6
  • _“I've looked and found "similar" issues but not exactly what I want. This is pretty specific.”_ - increasing some number/index in HTML element attributes when dynamically adding elements, isn’t very specific. What do you mean by “not exactly what I want” then? Did not work via pure copy&paste, or …? – CBroe Aug 03 '18 at 14:12
  • _“So after the 2nd row it no longer changes the background color.”_ - of course it doesn’t, because you only set inline style for the rows that were already there when the page was loaded. The solution to this is simple - don’t do this with JS in the first place, CSS can handle that just fine on its own. https://stackoverflow.com/a/5080787/1427878 – CBroe Aug 03 '18 at 14:12
  • @CBroe, I fixed the background color issue. But what I mean is many situations I've found all typically utilize once. $_POST['task1'] I want it in an array. I want the table to start with one blank line, if the user wants to add to it. They add, but I want the info from the first line to not change at all. Then when the user submits the info, it's going to read all lines and place into a database table. – dcary Aug 03 '18 at 14:45
  • Well then what's stopping you from typing something trivial like "php form fields as array" into Google and finding out that you will need to use square brackets in the field names for that? – CBroe Aug 03 '18 at 15:36
  • Nothing is stopping me @CBroe , I have done that and have not found any examples using it like that, it's usually only $_POST['insert'] not an array. – dcary Aug 06 '18 at 12:46
  • https://stackoverflow.com/questions/3314567/how-to-get-form-input-array-into-php-array ... using `[]` in the field names is the important thing here. – CBroe Aug 06 '18 at 12:54

1 Answers1

0

When you fill in the first field, fire your post using ajax. This will stop your page to refresh. You can then fire the normal post after adding values for the whole row.

Simranjit Singh
  • 384
  • 1
  • 6
  • To Colour rows even odd you can use css `tr { background: #f2f3f6 } tr:nth-child(2n+1) { background: #ccc }` – Simranjit Singh Aug 03 '18 at 14:18
  • It's for multiple rows though. Users would be filling out one row which has multiple posts, then the next line would be a new row of multiple posts. I'm trying to create something that looks like a spreadsheet, retrieves data from my database tables. Each row can be different or even have very similar information. – dcary Aug 03 '18 at 14:18
  • Right,so you can get data using ajax on every field in a row as required and do a normal post for each row. You can also post the row using ajax, page won't refresh. However I suggest to get data using ajax and for each row fire a normal post request. – Simranjit Singh Aug 03 '18 at 14:22
  • It has to be for the whole table, the next row is then going to post. The posting is just for information retrieval. There will be a submit button at the bottom that takes all information and runs a query that INSERTs it to a table. Maybe I'm misunderstanding. But what I have run into is when I select the first field on the second row, it clears out the first row information. And unless I change the name of the – dcary Aug 03 '18 at 14:56
  • I also don't know anything about AJAX. – dcary Aug 03 '18 at 14:57
  • Use Jquery for ajax http://api.jquery.com/jquery.ajax/. I find it simpler than vanilla js version. – Simranjit Singh Aug 03 '18 at 15:36
  • I've a small example to get data using Jquery ajax with instructions to use post: https://codepen.io/simranz/pen/KBemWb – Simranjit Singh Aug 03 '18 at 15:51
  • You'll easily crack this, jQuery makes a lot of things simpler. I've found it very helpful with ajax. – Simranjit Singh Aug 03 '18 at 16:06
  • I edited original question to give more info. Working with jQuery and Ajax has proven difficult. – dcary Aug 08 '18 at 19:16
  • From what I understand is you would need to post the add row request with ajax too. If you want to work with database, form posts and not want page to refresh then it has to be Ajax – Simranjit Singh Aug 09 '18 at 02:41