Jump to content

Recommended Posts

I am using a form to get data from a user, currently I am only getting one value and placing it in a database as shown below.

 

-------------------------------------------------------------

<form action="file01.php" method="post">

<br>Enter a color<br>

<p>color: <input type="text" name="color" /><input type="submit" /></p>

</form>

 

<?php $colorvar = $_POST['color'];?>

 

(then I place into database)

-------------------------------------------------------------

 

Id like to take this to the next level and let the user keep entering in colors until they are done, and store each of those colors in the database in a unique position.

 

What is the best way to go about this? Should I use a Do or While loop and loop the form?

 

Id like to set it up so the user has 1 form box and 2 buttons (submit and add)

where submit would end the loop and add would allow them to add another color.

or

just the submit button, and they can use submit to enter all their colors and when they enter '0' into the box and hit submit it ends.

firstly make your input an array by adding '[]' to the input name like so...

 

<form action="file01.php" method="post">
<br>Enter a color<br>
<p>color: <input type="text" name="color[]" />
<br>Enter a color<br>
<p>color: <input type="text" name="color[]" />
<br>Enter a color<br>
<p>color: <input type="text" name="color[]" />
..... // use the button to add these (you only need add the SAME mark up each time its clicked)
<br>Enter a color<br>
<p>color: <input type="text" name="color[]" />
<input type="submit" /></p>
</form>

 

Then you can be cleaver in your query to insert them into the database...

 


$values = implode("'),('" , $_POST['color']);
$insert ="INSERT INTO `colours_table` (`colour`) VALUES ('".$values."')";
// run query etc etc.

 

not the best to achieve this in terms of php code but its quick and nasty i'm giving you here...

 

 

firstly make your input an array by adding '[]' to the input name like so...

 


..... // use the button to add these (you only need add the SAME mark up each time its clicked)

 

Thanks for the help,...although I am slighty unclear with what you mean by the statement above.  But, yeah I would like to add one color to the array at a time.  I am a little lost though as to how I build the loop to build the array. The forms do not seem to like being inclosed in php tags.

 

thanks

If you are trying to have them add the colors real time(add color while on page) you are going to have to use javascript to hold the data and then you can post it. However, If you are looking for a solution with a fixed number of inputs you can do as toonmariner suggests with creating an array but I am not sure what he was doing in the post but you can do this:

 

<?php
if($_POST){
     $color_one = $_POST['color'][0];
     $color_two = $_POST['color'][1];
     $query = "INSERT INTO table (color_one,color_two) VALUES('$color_one','$color_two')";
}

yes, I am trying to add them in real time since I do not know if the user will enter 1 input, or 20. 

so php cannot do this without javascript? hmm I was afraid of that.

Any idea on what I should search for?  I have been searching terms such as 'iterative forms' , 'dynamic forms', 'variable length forms' etc... but haven't turned up much.  I guess I'll start looking into a javascript solution  :)

 

 

If you are trying to have them add the colors real time(add color while on page) you are going to have to use javascript to hold the data and then you can post it. However, If you are looking for a solution with a fixed number of inputs you can do as toonmariner suggests with creating an array but I am not sure what he was doing in the post but you can do this:

 

<?php
if($_POST){
     $color_one = $_POST['color'][0];
     $color_two = $_POST['color'][1];
     $query = "INSERT INTO table (color_one,color_two) VALUES('$color_one','$color_two')";
}

Do you know anything about the HTML DOM that Javascript has? Check out this tutorial.

 

Pay special attention to getElementById(), and the .innerHTML attribute of tags. If you need extra help your best bet would be to post in the javascript section

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.