Jump to content

outputting form data into array..


Dragen

Recommended Posts

Hi,

how would I retrieve all values sent by a form with post method and put it in an array? I'm trying to do this because I have a form which has different inputs and textarea etc depending on some other variables. I'm then trying to read the data and pu it into a mysql command.

 

I want to collect all the data from the form (regardless of what it is) and save it as an array which I can use.

Any help?

 

Thanks

Link to comment
Share on other sites

if they are in a POST form and each has a name then the value of that item is passed via the POST system to the POSTed page and can be retrieved on the POSTed to page via the $_POST['name'] variable so there is no need to use an array unless you have a specifi urge to want to use it

<form action="newpage.php" method="POST">
<input type="text" name="text1">
<input type="text" name="text2">
<input type="submit" name="subMe">
</form>

 

would illicit on newpage.php

<?php
if(isset($_POST['subMe']))
{
$text1 = $_POST['text1'];
$text2 = $_POST['text2']; //if this was not filled in on the form it would be an empty string
}
?>

 

Link to comment
Share on other sites

Yeah I know that. What I've got is this mysql statement:

$sql = "INSERT INTO " . $_POST['type'] . " (id,$othercolumns) VALUES ($id,$othercolumns)";

where $othercolumns would be the data from the form.

 

If I put

$sql = "INSERT INTO " . $_POST['type'] . " (id,var1,var2,var3) VALUES ($id,var1,var2,var3)";

then when it tries inserting into the database it will hit problems, because depending on the $_POST['type'] it uses different tables.

Not all the tables have the same columns in them, so if it tries entering data into a column that doesn't exist it wont work. I presume this woulds still happen if the var# is empty and if not, I'll just get lots of extra ',' in the mysql statement, which I presume would give an error..

Link to comment
Share on other sites

I don't know of anything. You could do something bespoke, running through all the POSTed variables, testing to see if they exist, and PUTTING them into an array. In particular, if the IDs of each form item went 1,2,3,4 etc that might make it easier.

 

 

Link to comment
Share on other sites

I'm not great, either, and I don't have any sample code. But I understand the theory. You type in ("fred", "fruit"), ("george", "meat"), etc and when you call up meat, you get george, or fred and you get fruit.

 

Not the best example. i think i'm hungry! But you get the idea.

 

So you could pass in the form element, and its value, to create an array that you could then use in your php. Sorry I don't have any sample code.

 

Best explained here: http://en.wikipedia.org/wiki/Associative_array

 

Toward the bottom there;s a php section. Ain't wikipedia great ;o)

Link to comment
Share on other sites

Why are you trying to put POST data into an array, it already comes as an associative array.

 

An associative array is an array that has an index title that is not the order of the array. For example:

 

<?php
$newArr = array("fruit" => "applies, oranges", "Vegetables" => "broccoli");

print $newArr[0]; // prints applies, oranges

print $newArr["Vegetables"]; //prints broccoli
?>

 

It just gives you another way to access data.

 

Variables that are sent via GET and POST come in an associate array already using the name of the input/textarea.

 

Let's say I have an input box with the name "username" this is how I would access it after the data was posted

<?php
print $_POST['username'];
?>

 

Should print what I put in the username input box.

 

to see all posted data try something like this

 

<?php
print_r($_POST, true);
?>

 

It will print out all the posted data with the indexes etc.

Link to comment
Share on other sites

hmm... thanks. unortunatly it outputs nothing.

I'll probably try using some if statements.

Am I able to put an if statement in a mysql query???

such as:

$sql = "INSERT INTO " . $_POST['type'] . " (id," . if(type == 'portfolio'){
    echo $var1 . "," . var2 . "," . var3 . "," . etc
} . ") VALUES (id," . if(type == 'portfolio'){
    echo $var1 . "," . var2 . "," . var3 . "," . etc
} . ")";

 

probably not...

Link to comment
Share on other sites

oh why am I on about if statements in mysql... ::)

I'll just do is statements like this:

<?php
if($type == portfolio){
$sql = "INSERT INTO " . $_POST['type'] . " (id,var1,var2) VALUES (" . $_POST['id'] . "," . $_POST['var1'] . "," . $_POST['var2'] . ")";
}elseif($type == affiliate){
$sql = "INSERT INTO " . $_POST['type'] . " (id,var1,var3) VALUES (" . $_POST['id'] . "," . $_POST['var1'] . "," . $_POST['var3'] . ")";
}
?>

and so on..

Link to comment
Share on other sites

<?php
$sql = "INSERT INTO " . $_POST['type'] . " (id," . ((type == 'portfolio')?$var1 . "," . var2 . "," . var3 . "," . etc:''). ") VALUES (id," .((type == 'portfolio')?$var1 . "," . var2 . "," . var3 . "," . etc:''). ")";

?>

 

That code may not be right due to syntax issues but you want the ? - ternary operator.

Link to comment
Share on other sites

Funnily enough, I now need to do an assoc array myself!

 

Frost, I think the original question is whether the information can be put into an array and then scrolled through for the SQL (i.e. without specifying the element name, I guess). (Sorry if I have misphrased you, Dragen)

 

For my part, I don't quite know where to start. I have a table, generated from a DB with all the options to buy listed. User can pick several items, and then click Add to cart.

 

I want to store their non-zero options in session variables, e.g. $_SESSION['cart_qty'], $_SESSION['cart_price'].

 

(1) So, does my cart symbol need to be a Submit, too?

(2) Any general syntax suggestions on setting this up? Like, do I do a while...loop after the form, looking for where qty<>0 ? If so, how do I then refer to the other elements of the table?

 

Really confused ???

 

Thanks

Emma

 

	<?	while ($row=mysql_fetch_assoc($result3)) { ?>
	<tr>
	<form name="whatever">
		<td id="normal"><? echo $row['packaging_desc']; ?></td>
		<td id="normal"><? echo $row['packaging_message']; ?></td>
		<td id="normal" align="center"><? echo $row['packaging_quantity']; ?></td>
		<td id="normal" align="center"><? echo $row['packaging_cello']; ?></td>
		<td id="normal" align="center">£<? echo number_format($row['packaging_price'],2,'.',''); ?></td>
		<td id="normal"><input name="qty" type="text" id="qty" size="3" /></td>
		<td id="normal"><a href="/cart.php?cell=1&bulk=<? echo $row['card_id']; $_SESSION['ref'] = "/bulkcards/" . breadcrumb($row['card_category']); ?>"><img src="/images/cart.gif" border="0"></a></td>
	</form>
	</tr>
	<? } ?>

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.