By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,574 Members | 1,205 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,574 IT Pros & Developers. It's quick & easy.

help needed - passing values between two loads of the same page

P: n/a
Hi,
How do I pass a string from one call to a php block to another call in
the
same page but from another form?

Here's my full php code:

I'm using two forms in the same page. A hidden field 'f' with values
'1' and
'2' are used to distinguish calls from one form from those from the
other.

<html>
<head><title>Database Access</title></head>
<body>

<form name="QueryForm" action="db2.php" method="POST" />

<input type="hidden" name="f" value="1" />

<input type="text" width="100" name="txtQuery"
value="select * from license_master;" /> <br/>

<input type="submit" name="Query" value="Query" /> <br/>
</form>

<?php
$s = ""; $nl = "<br/>";
switch ($_POST['f']){
case '1':
$conn=odbc_connect('mysqldsnone','root','katipatan g');
$sql = $_POST['txtQuery'];
$rs=odbc_exec($conn,$sql);
echo "<table width=\"60%\">\n";
while (odbc_fetch_row($rs)){
$abbr=odbc_result($rs,"abbr");
$fullname=odbc_result($rs,"fullname");
$link=odbc_result($rs,"link");
echo "<tr>
<td colspan=\"3\" background=\"blue.png\">
<p class=\"white-text\">".$abbr."</p></td></tr>\n";

echo "<tr><td>".$abbr."</td><td>".$fullname
."</td><td><a
href=\"".$link."\">".$link."</a></td></tr>\n";

$s .= $abbr.",".$fullname.",".$link."\n";
//making the comma separated string here
}

//************************************************** ***********************
/*
PROBLEM: somehow pass $s, which now contains the data in csv form
to the next call of the same page from the second form which is below
tried cookies but they do not work because you can only set a cookie
right
at the start of the page
*/
//************************************************** ***********************

echo "</table>\n";
odbc_close($conn);
break;
case '2':
$of = $_POST['outputfile'];
$of ="C:/Program Files/Apache Group/Apache2/htdocs/php/lm.csv";
$handle = fopen($of,"w+");
if (!$handle){echo "Cannot open file $of for writing".$nl;}
if (!fwrite($handle,$s)){echo "Cannot write to file $of".$nl;}
fclose($handle);
break;
default:
;
}
?>
<form name="ExportForm" action="db2.php" method="POST" />

<input type="hidden" name="f" value="2" /><br/>

<input type="text" name="outputfile" value="lm.csv" /> <br/>
<input type="submit" name="SubmitExport" value="Export" />
<br/>
</form>
</body>
</html>

How do I get the value I stored into $s in case '1', when execution
moves to case '2'?

Any help is greatly appreciated.
Thanks in advance.
Joseph S.

Sep 20 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
I never used ob_start() and ob_end_flush() , but you can check the
manual page :

http://mx2.php.net/manual/en/function.setcookie.php

it says :

" As of PHP 4, you can use output buffering to send output prior to the
call of this function, with the overhead of all of your output to the
browser being buffered in the server until you send it. You can do this
by calling ob_start() and ob_end_flush() in your script, or setting the
output_buffering configuration directive on in your php.ini or server
configuration files."

Another "make it yourself" solution would be to define a javascript
function that you call on the "onload" event of the page. as you have
to put it on the "body" balise, you make it calling the "submit" event
of an hidden form that call another javascript function with your
string as a param (string that you know at the end of your php script".
This last function accessing a cookie... But as you don't want your
page to be sent, you have to make this function to return false (abort
the sending process).
Yes, A little bit tricky...

<body onload=document.forms[3].submit()>
<form 1>
....
<form 2>
....
<form 3>
<input type=submit onsubmit="set_cookie(ccvs_string)">

<javascript
function set_cookie(param)
{
....
return false;
}


ok, that is a first jet... Sure there is simpler to do it, check php
manual...
Regards,
JR.

Sep 20 '05 #2

P: n/a
Joseph S. wrote:
Hi,
How do I pass a string from one call to a php block to another call in
the
same page but from another form?

Here's my full php code:

<code snipped>

How do I get the value I stored into $s in case '1', when execution
moves to case '2'?

Any help is greatly appreciated.
Thanks in advance.
Joseph S.


How about placing it in the $_SESSION array? Start the session at the
beginning of the page and store $s in $_SESSION['s']. Then pull it out
on the second call. (Be sure to clear $_SESSION['s'] on first call to
the page).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 20 '05 #3

P: n/a
Joseph S. wrote:
Hi,
How do I pass a string from one call to a php block to another call in
the
same page but from another form?


Use a hidden field.

Sep 20 '05 #4

P: n/a
Hi,
How about placing it in the $_SESSION array? Start the session at the
beginning of the page and store $s in $_SESSION['s']. Then pull it out
on the second call. (Be sure to clear $_SESSION['s'] on first call to
the page).


I did that, the value of $s is getting stored properly in
$_SESSION['s'], but in the second load of the page, $_SESSION['s'] is
not showing in the case '2' code.

Either script does not hav e access to $_SESSION or values held in
$_SESSION are removed. Does PHP give an invalid index error in the case
that I access $_SESSION['s'] and $_SESSION does not have an 's'
element?
I am asking this because, it is giving no such error, probably meaning
that the $_SESSION array has 's' but the value is getting cleaned.

also, in the case '2' code,
print_r($_SESSION);
outputs nothing at all!

Also, I have tried -
session_name("samplesession");
session_start();
in the case '1' code at the start.

But in the case '2' code,
a session_is_registered("samplesession"); gives a FALSE.

I have register_globals=On in my php.ini.
Any other function needs to be used?
Joseph S.

Sep 21 '05 #5

P: n/a

Found a good place for tutorials, which also cleared some of the
problems of sessions that I had in this thread:

http://www.tutorialized.com/

http://www.tutorialized.com/tutorials/PHP/1

http://www.tutorialized.com/tutorial...and-Sessions/1

Obviously, there may be others better, but for now, this place looks
good.

Regards,
Joseph S.

Sep 22 '05 #6

P: n/a
Joseph S. wrote:
Hi,
How about placing it in the $_SESSION array? Start the session at the
beginning of the page and store $s in $_SESSION['s']. Then pull it out
on the second call. (Be sure to clear $_SESSION['s'] on first call to
the page).

I did that, the value of $s is getting stored properly in
$_SESSION['s'], but in the second load of the page, $_SESSION['s'] is
not showing in the case '2' code.

Either script does not hav e access to $_SESSION or values held in
$_SESSION are removed. Does PHP give an invalid index error in the case
that I access $_SESSION['s'] and $_SESSION does not have an 's'
element?
I am asking this because, it is giving no such error, probably meaning
that the $_SESSION array has 's' but the value is getting cleaned.

also, in the case '2' code,
print_r($_SESSION);
outputs nothing at all!

Also, I have tried -
session_name("samplesession");
session_start();
in the case '1' code at the start.

But in the case '2' code,
a session_is_registered("samplesession"); gives a FALSE.

I have register_globals=On in my php.ini.
Any other function needs to be used?
Joseph S.


First of all, you should never have register_globals set to on. It's a
huge security risk.

As to the session variables not working. They work fine for me.
However, without seeing your entire code, it's almost impossible to
determine why sessions are failing.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 23 '05 #7

P: n/a
Jerry Stuckle wrote:
Joseph S. wrote:
Hi,
How about placing it in the $_SESSION array? Start the session at the
beginning of the page and store $s in $_SESSION['s']. Then pull it out
on the second call. (Be sure to clear $_SESSION['s'] on first call to
the page).


I did that, the value of $s is getting stored properly in
$_SESSION['s'], but in the second load of the page, $_SESSION['s'] is
not showing in the case '2' code.

Either script does not hav e access to $_SESSION or values held in
$_SESSION are removed. Does PHP give an invalid index error in the case
that I access $_SESSION['s'] and $_SESSION does not have an 's'
element?
I am asking this because, it is giving no such error, probably meaning
that the $_SESSION array has 's' but the value is getting cleaned.

also, in the case '2' code,
print_r($_SESSION);
outputs nothing at all!

Also, I have tried -
session_name("samplesession");
session_start();
in the case '1' code at the start.

But in the case '2' code,
a session_is_registered("samplesession"); gives a FALSE.

I have register_globals=On in my php.ini.
Any other function needs to be used?
Joseph S.


First of all, you should never have register_globals set to on. It's a
huge security risk.

As to the session variables not working. They work fine for me.
However, without seeing your entire code, it's almost impossible to
determine why sessions are failing.


The two major causes of session failure:
1. The two pages don't reside on the same domain
2. One of the two pages does not call session_start() - this needs to be
done on every single page where a session var is used

:)
Sep 24 '05 #8

P: n/a
littlefire wrote:

The two major causes of session failure:
1. The two pages don't reside on the same domain
2. One of the two pages does not call session_start() - this needs to be
done on every single page where a session var is used

:)


Those are two major causes. I wouldn't say they are THE two major causes.

For instance - calling session_start() after some output has been
written. Not having write access to the temporary directory. Incorrect
session values in the php.ini file. And several more common errors.

The problem is - which is it?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 24 '05 #9

P: n/a
> littlefire wrote:

The two major causes of session failure:
1. The two pages don't reside on the same domain
2. One of the two pages does not call session_start() - this needs to be
done on every single page where a session var is used
Those are two major causes. I wouldn't say they are THE two major causes.

For instance - calling session_start() after some output has been
written. Not having write access to the temporary directory. Incorrect
session values in the php.ini file. And several more common errors.

The problem is - which is it?


In my case it was no.2 above, sesion_start() absent.

Also, I discovered that the PHP _interpreter_ goes line by line in
displaying as well as processing. So, if you want to see whether a call
to the file is the first ever call or a subsequent refresh (or form
submittal for that matter), one way to do that is to create a dummy
session variable $_SESSION['a']=1 as the last line of the php file, and
in the lines above, before your actual processing begins, check whether
$_SESSION['a'] is set. If it the first ever call to the page, it will
not be set depending on which you can take some actions(keeping fields
empty etc.). If it is a second call via a refresh or a form submittal,
you will have $_SESSION['a'] set.

Little tricks often are helpful.
(a.php)
<?php
/* Keeping the look of the output HTML the same between
multiple calls to the same page */
session_start();
?>
<html>
<head>
<title>Title</title>
</head>
<body>
<form name="taxform" action="a.php" method="POST" >
<center>
Enter Gross<br/>
<input type="text"
name="gross"
value="<?php if(isset($_SESSION['a'])){echo $_POST['gross'];}?>"

/>
<br/>
<input type="submit"
name="Calcbtn"
value="Calculate Tax"
/>
<br/>

<?php
/* doing the calculation here */
$gross = (float)$_POST["gross"];
$tax = 0;
$rate = 0.3;
$tax = $gross*$rate;
?>
<br />
Tax<br/>
<?php echo $_SESSION['a'] ?><br/>
<input type="text"
name="tax"
value="<?php if(isset($_SESSION['a'])){echo $tax;} ?>"
/>
<br/>
</center>
</form>
</body>
</html>
<?php $_SESSION['a'] = 1; ?>

Regards,
Joseph S.

Sep 24 '05 #10

P: n/a
Hi,
I got a nice workaround for the original problem of multiple forms I
had:
In many cases, you want multiple submit buttons and not multiple forms
really.
So here is a sample code that works fine for the muliple submits case:
(multisubmits.php)
<?php
session_start();
?>
<html>
<head>
<title>Title</title>
</head>
<body>
<center>
<form name="taxform" action="multisubmits.php" method="POST" >
Enter Gross<br/>
<input type="text"
name="gross"
value="<?php if(isset($_SESSION['a'])){echo $_POST['gross'];}?>"

/>
<br/>
<table>
<tr>
<td><input type="submit" name="Tax30" value="30pcTax" /></td>
<td><input type="submit" name="Tax40" value="40pcTax" /></td>
<td><input type="submit" name="Tax50" value="50pcTax" /></td>
</tr>
</table>
</form>
<?php
/* doing the calculation here */
$gross = (float)$_POST["gross"];
$tax = 0;
if (isset($_POST['Tax30'])){
$tax = $gross * 0.3;
$country = "USA";
}else if(isset($_POST['Tax40'])){
$tax = $gross * 0.4;
$country = "United Kingdom";
}else if(isset($_POST['Tax50'])){
$tax = $gross * 0.5;
$country = "France";
}
?>
<br />
Tax<br/>
<?php echo $_SESSION['a'] ?><br/>
<?php if(isset($_SESSION['a'])){echo $tax;} ?><br/>
Must be <?php echo $country; ?>!<br/>
</center>
</body>
</html>
<?php $_SESSION['a'] = 1; ?>

That takes care of a lot of things - you can make forms with any
arbitrary multiple number of submit buttons and you just have to check
which submit was pressed and act accordingly!

Also, my original problem of multiple forms is also solved by session
variables. Anyways, now I deal with multiple forms like this:

<?php
session_start();
?>
<html>
<head>
<title>Title</title>
</head>
<body>

<center>
<form name="taxform" action="multiforms.php" method="GET" >
Enter Gross<br/>
<input type="text"
name="gross1"
value="<?php if(isset($_SESSION['a'])){echo $_GET['gross1'];}?>"

/><br/>
<input type="submit" name="Tax30" value="30pcTax" />
</form>

<form name="taxform" action="multiforms.php" method="GET" >
Enter Gross<br/>
<input type="text"
name="gross2"
value="<?php if(isset($_SESSION['a'])){echo $_GET['gross2'];}?>"

/><br/>
<input type="submit" name="Tax40" value="40pcTax" />
</form>

<form name="taxform" action="multiforms.php" method="GET" >
Enter Gross<br/>
<input type="text"
name="gross3"
value="<?php if(isset($_SESSION['a'])){echo $_GET['gross3'];}?>"

/><br/>
<input type="submit" name="Tax50" value="50pcTax" />
</form>
</center>

<?php
/* doing the calculation here */
$tax = 0;
if (isset($_GET['Tax30'])){
$gross = (float)$_GET["gross1"];
$tax = $gross * 0.3;
$country = "USA";
}else if(isset($_GET['Tax40'])){
$gross = (float)$_GET["gross2"];
$tax = $gross * 0.4;
$country = "United Kingdom";
}else if(isset($_GET['Tax50'])){
$gross = (float)$_GET["gross3"];
$tax = $gross * 0.5;
$country = "France";
}
?>
<br />
Tax<br/>
<?php echo $_SESSION['a'] ?><br/>
<?php if(isset($_SESSION['a'])){echo $tax;} ?><br/>
Must be <?php echo $country; ?>!<br/>
</body>
</html>
<?php $_SESSION['a'] = 1; ?>

Thanks to all,
Regards,
Joseph S.

Sep 24 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.