forumnz Posted May 3, 2009 Share Posted May 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/ Share on other sites More sharing options...
hchsk Posted May 3, 2009 Share Posted May 3, 2009 this shouldnt be too difficult, set each passed value as a $var, maybe even one array, create a loop testing if($var[$counter]) which just checks for the existence of the variable, then {insert row} i cant really be more specific without seeing more Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824601 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824606 Share on other sites More sharing options...
hchsk Posted May 3, 2009 Share Posted May 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824615 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 Sorry you have it right but it formats it to be inserted into a database - each set of fields (name7, email7) is it's own row. Does what I wrote sound like it would work for this - is there a better way? Thanks Sam Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824622 Share on other sites More sharing options...
hchsk Posted May 3, 2009 Share Posted May 3, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824628 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 Hmm cool. I have tried to write the first part: <?php $num = 1; if(isset($_POST["num" . $num])) { echo "1!<br />"; $num++; } ?> This doesn't echo anything (even when the URL ends in .php?num=1).. what is wrong here? Thanks Sam Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824641 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 Oops just realised how bad that post was. Here is the URL I meant to write: .php?num1=1 Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824643 Share on other sites More sharing options...
hchsk Posted May 3, 2009 Share Posted May 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824645 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 Ok well what about this: <?php $i = 1; while($i < 300) { $post = "name" . $i; if(isset($_POST[$post])) { $arr[$i] = $_POST[$post]; } $i++; } echo $arr; ?> Shouldn't that work (which it doesn't). The URL end in .php?name1=Bob Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824648 Share on other sites More sharing options...
hchsk Posted May 3, 2009 Share Posted May 3, 2009 yeahh thats definitely a step in the right direction. can you explain what $_POST[$post] is? what would var_dump($_POST[$post]); display? Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824652 Share on other sites More sharing options...
sKunKbad Posted May 3, 2009 Share Posted May 3, 2009 I would serialize the data in array form before adding it to the database in 1 field. The serialized data can always be retrieved and unserialized, and this eliminates a lot of extra work. Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824654 Share on other sites More sharing options...
zeeshan_haider000 Posted May 3, 2009 Share Posted May 3, 2009 I don't know if i understood the question well but can't you change the field names to something like: <input type="text" name="name[]" value="" /> <input type="text" name="email[]" value="" /> And then use foreach() to insert data? Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824655 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 @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 Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824657 Share on other sites More sharing options...
hchsk Posted May 3, 2009 Share Posted May 3, 2009 well thats definitely a problem, i dont understand the $_POST functions, so i cant help you with that, but that's the problem Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824658 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824662 Share on other sites More sharing options...
zeeshan_haider000 Posted May 3, 2009 Share Posted May 3, 2009 @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... Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824663 Share on other sites More sharing options...
forumnz Posted May 3, 2009 Author Share Posted May 3, 2009 Thanks but I think I've got it using this method Quote Link to comment https://forums.phpfreaks.com/topic/156605-solved-interesting-javascriptphp-problem/#findComment-824665 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.