Forum Moderators: coopster

Message Too Old, No Replies

Javascript Form vs. PHP Processing

Dynamic Form & PHP Processing issue

         

Sherif

10:10 am on Dec 2, 2010 (gmt 0)

10+ Year Member



Hey Guys,

First of all, thanks for the huge support that you provide to the community here.

Currently, I am working on a form, and i wanted it to be dynamic, i.e. extending depending on the number of fileds the user wants to input.

I passed by this page [quirksmode.org ] where they talk about utilizing the Javascript DOM method to generate the dynamic form, and explaing how it can be used to extend the existing form.

I see that the concept is ok, but the question is...
When the form is submitted to the processing page, how can we know what items had been submitted so that we can process and insert them in a database and so on?
I thought of creating a loop and counter, but this will limit the dynamic form not making it fully dynamic in the actual sense that we want.

They use the cgi-method but i am not sure how we can do something like that on PHP as i wanted it to be an efficient code.

Or in other words, how can we check what had been submitted and use that information to perform the required process.


Thank you for your kind support in advance.

Sincerely,
Sherif

JuicyScript

12:46 pm on Dec 3, 2010 (gmt 0)

10+ Year Member



Am having the same problem i know it's possible
You might wanna read this
[codeproject.com...]

Sherif

9:57 pm on Dec 3, 2010 (gmt 0)

10+ Year Member



Hey juicy script, thanks for your response...


But from what i understood from the link that you had provided, it deals with independent rows meaning that each row has its own unique id.

In my case, for example. lets say i generate a form for lets say a user adding their favorite cds as mentioned as an example in the link i entered. The number of cds that a user adds is not known, so how can i know the number of values that the user has already submitted?


Thanks for your support.


Sincerely,
Sherif

rocknbil

1:21 am on Dec 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Guess I don't understand the question. You can tell what's submitted by examining get or post.

Say you do something like this with JS:

for (i=0;i<=10;i++) {
var formname= 'textbox_' . i;
// Then append this to the dom like
//out += '<input type="text" name='+formname+'">';
}

So in your receiving script,

foreach ($_POST as $key => $value) {

will give you a preview of all variables. If you want to order them, get your dynamics out of the way like this:


foreach ($_POST as $key => $value) {
if (preg_match('/^textbox\_\d+$/',$key) and ! empty($value)) {
// we found one of the dynamic fields, do whatever here
}
}


Mileage will vary but there's always a way to examine input in an efficient way, instead of . . .

if ($_POST['obj1']) {
//sigh 1
}
if ($_POST['obj2']) {
//sigh 2
}
if ($_POST['obj3']) {
//sigh 3
}

.... etc . . . instead you could do

for ($i=0;$i<4;$i++) {
$nm = 'obj'.$i;
if ($_POST[$nm]) {
// sigh $i;
}

Sherif

1:44 am on Dec 4, 2010 (gmt 0)

10+ Year Member



Hey Guys,

Thanks rocknbill for the hint... i further worked with the code as a pure php code, but i am not sure if this is considered as a good and efficient approach in the php method.
I treated the $_POST as a normal associative array and filtered its values, and then generated an array containing all of the counter values for the submitted variables: -
<?php
$submitted = array_keys($_POST,"marker");
$counter = array();
foreach($submitted as $value){


$value1 = substr($value,7);

if(!isset($counter[$value1])){
$counter[]= $value1;
}

}
foreach($counter as $counter2){

$numbername = 'value' . $counter2;
echo $numbername . "<br />";
echo $counter2 . " = " . $_POST[$numbername] . ", <br /><br />";

}
echo "<br />" . count($counter);
?>


Once this is complete, i can generate a loop for the mysql insert command with a variable counter $i storing the count($counter) result.


Any suggestions/opinion regards what i had generated in the code above in terms of the methodology of approach or even concept?


Thanks a lot for your kind support.


Sincerely,
Sherif