Page 1 of 1

PHP and Array problem

Posted: Wed Nov 10, 2004 4:23 am
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

Posted: Wed Nov 10, 2004 4:52 am
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

Posted: Wed Nov 10, 2004 5:53 am
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

Posted: Wed Nov 10, 2004 6:15 am
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.

Posted: Wed Nov 10, 2004 6:42 am
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.

Posted: Wed Nov 10, 2004 8:52 am
by wizzard81
i don't find a working solution. I've tried alot of different things but it's impossible i think?

Posted: Wed Nov 10, 2004 12:33 pm
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?

Posted: Wed Nov 10, 2004 1:35 pm
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?

Posted: Wed Nov 10, 2004 2:13 pm
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>";

Posted: Wed Nov 10, 2004 9:07 pm
by dreamfly

Code: Select all

<?php

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

?>