Jump to content

Submitting data from dynamically created form


Agony

Recommended Posts

So the form fields are dynamically created based off the database results with a for loop.

 

3 text inputs per row - with a value from the database.

 

Now the problem is that submitting the form this way wouldn't work - the name fields would be same and only last one would be submitted.

Turning the name filed into an array - so each is different - doesn't work ether. It will submit everything in the whole form without any way to identify which "row" was edited.

 

Multiple forms and each row with its on submit button is too bulky and ugly.

for ($i = 0; $i < $csize; $i++){    $out .= "<tr>";    $out .= "<td  style='width: 230px; padding: 7px;'>";    $out .= "<input type='text' name='category"' size='40' value='".$category[$i]['category']."'/></td>";    $out .= "<td  style='width: 230px; padding: 7px;'>";    $out .= "<input type='text' name='id' size='40' value='".$category[$i]['id']."'/></td>";    $out .= "<td  style='width: 230px; padding: 7px;'>";    $out .= "<input type='text' name='permission' size='40' value='".$category[$i]['permission']."'/></td>";    $out .= "<input type='hidden' name='org_id' value='".$category[$i]['id']."'/></td>";    $out .= "<td><a class='icon_link ".BASE_COLOR." 'href='".$_SERVER['REQUEST_URI']."&page=delete&id=".$category[$i]['id']."'><i class='icon-trash icon-large'     title='Delete Category'></i></a></td>";                          $out .= "</tr>";}

Would there be any alternative to this kind of setup? Surely the modern web has something that doesn't require you to have to submit after every row if u have some data like that to update.

 

Link to comment
Share on other sites

One option would be to use checkboxes to indicate which rows you want to update. You could use some Javascript to automatically check the box if the fields change. You'll need to give each element a unique name though. One option for that would be to make the checkboxes an array of the IDs that are being updated, and then append the ID to each form element. Get the IDs from the checkboxes, and use the IDs to get the values they entered.

Link to comment
Share on other sites

Multiple forms and each row with its on submit button is too bulky and ugly.

[...]

Would there be any alternative to this kind of setup? Surely the modern web has something that doesn't require you to have to submit after every row if u have some data like that to update.

 

I don't understand. Why would you need to tell the Php code what fields had been changed? The Php code can compare all the fields are determine which ones have changed.

Link to comment
Share on other sites

i don't need to tell php what changed - i only want to post the ones that changed.

But if the name is as an array it would still post the whole form with all the generated "rows".

The changed results would then be used to update the database.

 

To compare it all i would ether have to pull the whole table from db and compare each result with the array i get from $_POST - all of which requires extra loops and after that i can use the new compared array to loop through and update DB for each changed value. That's a lot of extra bandwidth and delay and cpu usage for a simple task.

 

Posting only rows that did get changed would save from the first half of comparing everything to the database.

Link to comment
Share on other sites

Use for loop value to append to attribute name values

name='category0'

name='category1'

name='category2'

 

store total in hidden input

 

<input type="hidden" name="total_group_inputs" value="<?php echo $csize; ?>">

 

when form submitted retrieve 'total_group_inputs' value and create for loop to retrieve values.

Link to comment
Share on other sites

i don't need to tell php what changed - i only want to post the ones that changed.

But if the name is as an array it would still post the whole form with all the generated "rows".

The changed results would then be used to update the database.

 

To compare it all i would ether have to pull the whole table from db and compare each result with the array i get from $_POST - all of which requires extra loops and after that i can use the new compared array to loop through and update DB for each changed value. That's a lot of extra bandwidth and delay and cpu usage for a simple task.

 

I still don't understand. What simple task? Don't you need to display the entire form to the user? I'm guessing yes. So you retrieve the entire form from the database and put it on the screen -- or if you're really worried about bandwidth then send it to the client as an array via Ajax. Then Javascript builds the table and puts the form on the screen. The user makes any needed changes and clicks submit. Javascript then processes the fields and compares them to the original array and sends only the fields that have changed back to the Php code.

Link to comment
Share on other sites

If you want to minimize bandwidth then you may want to use Ajax, but of course there is a tradeoff because the code to support Ajax may be larger than your form.

Link to comment
Share on other sites

so i submit it over ajax.

$.ajax({   url: 'xx.php?blahblah',   type: 'post',   data: {'category': category, 'id': id},});

But how would i in that case only submit the changed values and refresh the table in return with new ones?my row 3 input names would all be arrays.

<input type='text' name='category[]' size='40' value='".$category[$i]['category']."'/>

And thee can be any unspecified number of those in the whole final table.I have very little experience with jquery/javascript so im not sure where to start with all of this.

 

EDIT:I did find that you can detect change of a textbox by using:

$('input[name="Value"]').on('input',function(e){});

But that only helps partially - i don't have static input names. They can be id1, id2, id3 id 400 etc - since its declared as id[] - and since the number of results varies i cannot predict what it will be.

Edited by Agony
Link to comment
Share on other sites

login/session management is completely separate, the whole project is modular.

 

The form can be from just 10 rows in the beginning to 20+ if the user needs more.this far iv got to this:

        var results = $('[name^=id]').map(function(){               var $this = $(this);            var $defaultid = this.defaultValue;                        $($this).on('focusout',function(e){                var $id = $($this).val();                if ($id != $defaultid){                    console.log($id);                    console.log($defaultid);                    $($this).css('background-color', '#FFA07A');                                 $.ajax                ({                    url: 'index.php?action=ticket_ajax',                    type: 'post',                    data: {'id': $id, 'defaultid': $defaultid, 'page' : 'update'},                    success : function(data) {                    console.log(data);                    }                                  });                                };            });          });

one of those for each "name" field - so 3 in total.But im having issue with ajax - when i return the json it seem to have sent the default value instead.

1 999 {"id":"999","defaultid":"999"} 

1 is the new value 999 is default(random testing values).If i use console.log($id); before and it shows the $id has new value - but the same variable with ajax has default value. whats up with that?

Link to comment
Share on other sites

But the user might fill out the form and then decide they need ten more rows whose current values need to be retrieved from the database?

 

I am not a jQuery person so I can't intelligently comment on your code example above.

Link to comment
Share on other sites

it displays all the rows currently in the database - when user adds new ones, they have default values.

 

Tho the jscript above - why would ajax get a diff value fir $id then the console.log shows just before it?

Edited by Agony
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...