Jump to content

Uploading generated text fields


Schlo_50

Recommended Posts

I have on a page 4 text fields shown for a user to enter an amount in. If they have more than 4 amounts to enter in i have a button which, when clicked adds another text field although the max amount of text fields avaliable is 15. I need my php script to take the values entered by the user and send them into my MS Access database and order them into one field within the table with each value separated my commas.

 

I have included  this post my php so far, and the javascript code which generates the text fields which are unique in name.

 

PHP

<?php
if ($_POST[submit] == "Submit") 
{
$conn = odbc_connect('accessname', 'root', '') or die('Could not Connect to ODBC Database!');

for($i=0,$<15,++$i)if($_POST['field_'.$i]));

$sql = odbc_query("insert into Order1('ProductNotes')values('$_POST['field_$i'])");

$rs = @odbc_exec($conn,$sql);
if (!$rs)
{
echo "An error has occured. Please try again";
}
else
{
echo "The record was successfully inserted.";
}
odbc_close($conn);
}
?>

 

At present, this line is giving my problems:

 

for($i=0,$<15,++$i)if($_POST['field_'.$i]));

 

JAVASCRIPT

 

function addField() {
var tbody = document.getElementById("tblBody");
var ctr = tbody.getElementsByTagName("input").length + 1;
var input;

if ( ctr > 15 ) {
alert ("15 is the maximum amount of orders you are allowed.");
}else{

if (document.all){ //input.name doesn't work in IE
input = document.createElement('<input name="field_'+ctr+'">');
}else{
input = document.createElement('input');
input.name = "field_"+ctr;
}

input.id = input.name;
input.type = "text";
input.value = "";
input.className = "textfield";
var cell = document.createElement('td');
cell.style.height = '30px';
cell.appendChild(document.createTextNode(ctr+". "));
cell.appendChild(input);
var row = document.createElement('tr');
row.appendChild(cell);
tbody.appendChild(row);

window.document.the_form.count.value = ctr;

}
} 

 

If anyone can come up with something to make all values POST into my database that would be extremely helpful.

Thanks

 

Link to comment
Share on other sites

try this

 

<?php
if ($_POST[submit] == "Submit") 
{
$conn = odbc_connect('accessname', 'root', '') or die('Could not Connect to ODBC Database!');

$arrData = array();
for($i=0,$i<15,++$i)
{
if(isset($_POST['field_'.$i]))
{
	array_push($arrData,$_POST['field_'.$i]));
}

}



$sql = odbc_query("insert into Order1 ('ProductNotes') values ('".implode(",",$arrData)."')");

$rs = @odbc_exec($conn,$sql);
if (!$rs)
{
	echo "An error has occured. Please try again";
}
else
{
	echo "The record was successfully inserted.";
}

odbc_close($conn);
}
?>

Link to comment
Share on other sites

As a quick example you don't HAVE to number your text input field boxes like field_1, field_2 etc...

Just name them ALL field[] .

e.g.

<input type="text" name="field[]" />
<input type="text" name="field[]" />
<input type="text" name="field[]" />
<input type="text" name="field[]" />
<input type="text" name="field[]" />

 

This way you don't have to worry about naming them all differently.

Then in your PHP you ALREADY have an array ($_POST['field']), so just do $values = implode(",",$_POST['field']))

And use $values in your SQL statement.

Link to comment
Share on other sites

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.