PHP and Array problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wizzard81
Forum Commoner
Posts: 66
Joined: Wed Jan 15, 2003 6:05 am
Location: Belgium
Contact:

PHP and Array problem

Post by wizzard81 »

Hello,

I have my textfields in array and want to print the value. When i'm doing this i get no result =>

echo $_POST["project_verkoopprijs[0]"];

Can someone tells me what i'm doing wrong?

Best Regards,
Kris Saelen
swdev
Forum Commoner
Posts: 59
Joined: Mon Oct 25, 2004 8:04 am

Post by swdev »

That depends on the type of variable that project_verkoopprijs is.

Have a look at this sample code to see the difference

Code: Select all

// setup some test data
$_POST['variable'] = 'Some Variable';
$_POST['arr'] =  array('Array One', 'Array Two');

// Print the complete contents of the POST array
echo ' print_r POST--'; print_r($_POST); echo '<br />';

// print the contest of the 'variable' element of the POST array
echo ' $_POST[variable]--' . $_POST['variable'] . '<br />';

// print the first element of the 'arr' array within the POST array
echo '$_POST[arr][0]--' . $_POST['arr'][0] . '<br />';
Hope this helps
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

If you are just printing plain text not an array:

echo ($_POST['nameoffield']);

Also make sure your form is posting, if its not posting use $_GET... if you do not know what this means just use $_REQUEST instead of $_GET or $_POST
wizzard81
Forum Commoner
Posts: 66
Joined: Wed Jan 15, 2003 6:05 am
Location: Belgium
Contact:

Post by wizzard81 »

I'm also having an error with this =>

<select name=project[<? echo $counter; ?>] onChange="document.forms['frm_facaanmaken'].elements['project_verkoopprijs[<?echo $counter; ?>]'].value = <? echo "$verkoopprijs[this.options[this.selectedIndex].value]"; ?>;" style="height:18;" height=24>


this is the error => echo "$verkoopprijs[this.options[this.selectedIndex].value]";

But i need this because when i change the select control and choose another item it needs to put the price of that item in the price_field.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

I appears you are trying to mix Javascript and PHP incorrectly.

PHP knows nothing about [this.options[this.selectedIndex].value].

Code: Select all

&lt;select name=project&#1111;&lt;? echo $counter; ?&gt;] onChange="document.forms&#1111;'frm_facaanmaken'].elements&#1111;'project_verkoopprijs&#1111;&lt;?echo $counter; ?&gt;]'].value = &lt;? echo "$verkoopprijs ?&gt;&#1111;this.options&#1111;this.selectedIndex].value]";;" style="height:18;" height=24&gt;
may work but is a total mess for readability. (OK I can't be bothered to read it :wink:)

even better would be something like...

Code: Select all

<?php
$change="document.forms['frm_facaanmaken'].elements['project_verkoopprijs[$counter]'].value = $verkoopprijs.[this.options[this.selectedIndex].value;";
echo("<select name="project[$counter]" onChange="$change" style="height:18;" height=24>");
?>
This makes it far easier to debug.
wizzard81
Forum Commoner
Posts: 66
Joined: Wed Jan 15, 2003 6:05 am
Location: Belgium
Contact:

Post by wizzard81 »

i don't find a working solution. I've tried alot of different things but it's impossible i think?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Can you submit the values and change them into an array after receiveing them? That would be much easier if your project allows for that, if not, and you need them to send as an array.. take a look at your source code of the php document after it is sent to the browser (look at the html source it outputs), is there anything noticeably wrong with it?
wizzard81
Forum Commoner
Posts: 66
Joined: Wed Jan 15, 2003 6:05 am
Location: Belgium
Contact:

Post by wizzard81 »

The problem i have now with the html is it shows nothing.

What do you mean with this?

Can you submit the values and change them into an array after receiveing them?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Yeah...

Code: Select all

//get all your text field's values like this or _post or whatever you used
$var=$_GET['var'];
$thing=$_GET['thing'];
// Make a new array
$arr=array();
// Fill it in
$arr['thing']=$thing;
$arr['var']=$var;
// Test it out
echo "<pre>";
print_r ($arr);
echo "</pre>";
dreamfly
Forum Newbie
Posts: 3
Joined: Wed Nov 10, 2004 9:01 pm
Contact:

Post by dreamfly »

Code: Select all

<?php

# $_POST[var];
# var is a array variable posted by prev page.
echo $_POST[var][0];

?>
Post Reply