Warrior Forum - The #1 Digital Marketing Forum & Marketplace

Warrior Forum - The #1 Digital Marketing Forum & Marketplace (https://www.warriorforum.com/)
-   Programming (https://www.warriorforum.com/programming/)
-   -   Multiple form fields to one database column (https://www.warriorforum.com/programming/688185-multiple-form-fields-one-database-column.html)

otfromtot 5th October 2012 12:29 PM

Multiple form fields to one database column
 
its a simple enough form, uses method="post"
the issue is that i have several text inputs that need to all submit to "customer_comments" but when i name them all "customer_comments" only the final one ends up being seen in the database.
im trying to do php to handle it but there seems to be issues
<?
$customer_comment = "";
foreach($_POST['customer_comment'] as $value)
{
$customer_comment .= $value . ", ";
}
$customer_comment = substr($customer_comment, 0, -2); //to get rid of the final ", "
echo $customer_comment;
?>
right now, instead of posting customer comment,customer comment, customer comment it's just posting "Array"

MarkLL 5th October 2012 10:42 PM

Re: Multiple form fields to one database column
 
Can you post the code from your initial form?

The input filed names should be something like name="customer_comment[]"

Also, I hope you are checking the return values for html and other injection hacks otherwise you will be in real trouble...

otfromtot 8th October 2012 10:45 AM

Re: Multiple form fields to one database column
 
the issue ended up being in the submission page that the form was linked to. I used this javascript instead.

<div class="fieldContainer">

<p> Beam Size:<input type="text" name="beam" id="beam" onKeyUp="return bar()" /></div>
<p>
<div class="fieldContainer">
<select name="beamm" id="beamm" onChange="return bar()"><option label="Select one">Feet/Meters</option><option label="feet" value="feet">Feet</option><option label="meter">Meters</option></select>
</p>
</p>
</div>
<div class="fieldContainer">
<p> Mast Height:<input type="text" name="mast" id="mast" onKeyUp="return bar()"/></p></div>
<div class="fieldContainer"><p><select name="mastm" id="mastm" onChange="return bar()"><option label="Select one">Feet/Meters</option><option label="feet" value="feet">Feet</option><option label="meter">Meters</option></select>
</p>
</div>
<div class="fieldContainer">
<p> Towable Trailer:<select name="trailer" id="trailer" onChange="return bar()"><option label="Select one">Select one</option><option label="Yes" value="yes">Yes</option><option label="No" value="0">No</option><option label="Not Suitable" value="no">Not Suitable</option></select>
</p>
</div>
<div class="fieldContainer">
<p>Overall Length:<input type="text" name="length" id="length" onKeyUp="return bar()" /></p></div>
<div class="fieldContainer"><select name="lengthm" id="lengthm" onChange="return bar()"><option label="Select one">Feet/Meters</option><option label="feet" value="feet">Feet</option><option label="meter">Meters</option></select>
<br />
</p>
</div>
<div class="fieldContainer">
<p>Overall Height:<input type="text" name="height" id="height" onKeyUp="return bar()" /></p></div>
<div class="fieldContainer"><select name="heightm" id="heightm" onChange="return bar()"><option label="Select one">Feet/Meters</option><option label="feet" value="feet">Feet</option><option label="meter">Meters</option></select>
<br />
</p>
</div>
<div class="fieldContainer">
<p>Category:<select name="category" id="category" onChange="return bar()"><option label="Select one">Select one</option><option label="Power" value="power">Power</option><option label="Sail" value="sail">Sail</option></select>
<br />
<textarea id="customer_comment" name="customer_comment" style="visibility:hidden"></textarea>
</p>
</div>

<script language="javascript" type="text/javascript">

function bar()
{

document.getElementById('customer_comment').value =
" beam: " + document.getElementById('beam').value + "" +
" " + document.getElementById('beamm').value + "|" +
" mast: " + document.getElementById('mast').value + "" +
" " + document.getElementById('mastm').value + "|" +
" trailer: " + document.getElementById('trailer').value + "|" +
" length: " + document.getElementById('length').value + "" +
" " + document.getElementById('lengthm').value + "|" +
" height: " + document.getElementById('height').value + "" +
" " + document.getElementById('heightm').value + "|"
" category: " + document.getElementById('category').value + "|";
}



</script>

SteveSRS 9th October 2012 01:16 AM

Re: Multiple form fields to one database column
 
this would have been the change to make it work probably:

foreach($_POST['customer_comment'] as $value)
to
foreach($_POST['customer_comment'] as $key => $value)

boilingstocks 9th October 2012 10:22 AM

Re: Multiple form fields to one database column
 
Well An easy way to this is, just take all the comments in different variables like :
$comment1 = $_POST['comment1'];
$comment2 = $_POST['comment2'];
$comment3 = $_POST['comment3'];

and then join all of them under one variable
$finalcomment = $comment1.$comment2.$comment3;

All you need to do is post this variable into your table and you're done :)


All times are GMT -6. The time now is 10:13 AM.