LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-10-2005, 07:55 AM   #1
Erhnam
Member
 
Registered: Jan 2005
Posts: 54

Rep: Reputation: 15
[php] Posting correct value when using foreach


I wrote a php script that uses the foreach function. The result (a group of system users) will get through the netstat commando and if the status is null, a button will be displayed. This all seems to work.

PHP Code:
foreach ($result as $inhoud) {
exec ("netstat -ea | grep $inhoud"$userstatus);

echo 
$inhoud;
if ( 
$userstatus == NULL )
{
echo 
"<font color=\"red\"> Server status offline!</font><br>";
echo 
"<form action='start.php' method='post'>";
echo 
"<input type='hidden' value='$inhoud' name='inhoud'>";
echo 
"<input type='submit' value='Start'>";
echo 
"</form><br>";
$_SESSION[inhoud] = $inhoud;
unset(
$userstatus);
//unset($inhoud);

http://www.strengholt-online.nl/linux/serverstatus.png

The next thing is I want to post (session) the value of the user after pressing the button. Without using unset the value is always the latest value of result. Using unset I get the first value of unset. Both are wrong. In this example, when I press on Start for csuser05. The next page displays csuser01 or csuser11 (last in the row). What do I have to do to get the right value of the user posted?

Next screen:

PHP Code:
<? session_start();

echo 
$_SESSION[user]; // is admin
echo "<br>";
echo 
$_SESSION[inhoud];
echo 
"<br>";

Last edited by Erhnam; 02-10-2005 at 08:40 AM.
 
Old 02-10-2005, 03:12 PM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Couple things:
First, your global variables need quotes in them ie: $_SESSION['user']

Second, I don't think the value of $_SESSION[inhoud] is making it to your second page (start.php) as you are only passing the POST variable, not the session variable.

Try this:
Code:
foreach ($result as $inhoud) { 
exec ("netstat -ea | grep $inhoud", $userstatus); 
 
echo $inhoud; 
if ( $userstatus == NULL ) 
{ 
echo "<font color=\"red\"> Server status offline!</font><br>"; 
echo "<form action='start.php' method='post'>"; 
echo "<input type='hidden' value='$inhoud' name='inhoud'>"; 
echo "<input type='submit' value='Start'>"; 
echo "</form><br>"; 
}
then:
Code:
<? session_start();
// give $_SESSION['inhoud'] a value here, as it is not being 
// passed along with your POST data
$_SESSION['inhoud'] = $_POST['inhoud'];

echo $_SESSION['user']; // is admin 
echo "<br>"; 

echo $_SESSION['inhoud']; 
echo "<br>";
HTH
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use foreach on filenames with spaces? BrianK Linux - General 3 08-09-2005 12:15 PM
PHP posting value to session using foreach Erhnam Programming 15 02-16-2005 08:31 AM
PHP Mail and Foreach function Gerardoj Programming 6 10-08-2004 05:24 PM
foreach loops chunky Linux - General 2 07-02-2004 11:49 AM
perl:foreach katana Programming 3 07-24-2001 01:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration