Jump to content

Recommended Posts

I have two files - one which is a form and the other which is the PHP script to insert the data into a database.

 

In form.php, I use Javascript to allow the user to dynamically create fields. For example.. the user wants to insert 5 sets of data (each set is a name and email).

 

The Javascript would add a new set of text fields as the user needs them and would call them something along the lines of:

name1 email1

name2 email2

name3 email3

name4 email4

name5 email5

 

Suddenly the user decides that 1 and 4 are not to be used, so they click delete on those sets and submit the form. They form is submitted with data like:

name2 email2

name3 email3

name5 email5

 

Now the insert.php script needs to be able to insert 3 rows (as there are three sets of data).

 

How would it work so that the PHP script cycles through the numbers and only inserts rows where the data exists (so not row 1, 4, 6, 7, 8...etc)

 

I'm not sure if I wrote this well but hopefully you can understand and help :)

Thanks heaps!

Sam :)

Here is what I cam up with (feel free to tell me it's useless):

There is a hidden field within each set of data, so when the form is sent it send something like

name1 email1 1

name2 email2 2

 

And then I create an array using the hidden fields (which are the solo numbers - 1, 2 etc)

Then I create $name(number) = $_GET['name(number)']; and $email(number) = $_GET['email(number)']; where number is each number from the array.

 

Then I insert into the database all the values....

I am so confused now... I think I am making this more complicated for myself than it needs to be. What do you all think?

 

Sam :)

youve got to explain more about what exactly youre doing. the javascript adds and subtracts fields, and when the user clicks submit it sends it to the php script, which formats it for an email? i think what you wrote should work, but it will need to be put into a for($counter = 0; counter <= $counter(array), $counter++) {} loop and generate the $name(number) with $name = 'name' . $counter within the loop

sounds good, i just think it needs to be in a loop like i described so itl work for any number of rows. as for a better way, iunno, generally honestly, as long as it works, it works for me, but i'm not at the stage of development where i need to be terribly concerned about performance, though i do like small and tidy scripts. as long as you indent your scripts nicely for ease of use and keep everything clear, its all good.

so basically for this example its checking

 

if(isset($_POST[1num])) {}

 

i've havent yet in the project i've worked on needed to customize or write any scripts that worked in depth with POST and GET functions, so i don't full understand what $_POST[] is, so i can't help if the problem is there, but i will say that what you posted is not a loop, it just adds to $num once and moves on. but i suppose it should still be printing the first time? can you explain what $_POST[] is? but the biggest thing is that that is not a loop

@hchsk

$_POST[$post] is meant to be each different variable (like name1, name2 etc).

That var_dump returns NULL.

Why is that?? :) Shouldn't it return 'Bob'?

 

@sKunKbad

I will - just getting the first things out of the way - thanks :)

 

@zeeshan_haider000

Not sure what you mean :o

Yeah.. I figured out a solution to the NULL - I should have been using GET not POST.

 

Here is what I have:

<?php

$i = 1;
$arr = array();
while($i <= 300)
{
$post = "name" . $i;
if(isset($_GET[$post]))
{
$arr[$i] = $_GET[$post];
}

$i++;
}
echo $arr;
?>

 

The output is just: Array

What could be the problem here?

 

Thanks

Sam :)

@forumz

The Javascript would add a new set of text fields as the user needs them and would call them something along the lines of: name1, name2...

 

I meant to say that you can use an array like name[] and email[] to save your data: <input type="" name="name[]"... /> and just retrieve the date from the arrays to insert into the db...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.