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

Passing values between scripts:

P: n/a
Hello,

I wondered if anyone could offer some guidance over my php script. I
was hoping the example would allow the user to click the submit
buttons and the item number increment.

And when the user clicks, the my basket icon the items.php scripts
starts and the variables from the current script are passed to this.

I tried to display the variables as the items are incremented, though
I got no value. Can anyone see the logical error?

Thank you

<?php
global $item1, $item2;

if(isset($_GET['item1'])){
$item1++;
}
if(isset($_GET['item2'])){
$item2++;
}
?>

<head>
<title>PHP Example</title>
</head>

<body>
<p>
<table border="0" width="100%">
<tr>
<td>
<a><img src="mybasket.jpg"></a>
</td>
</tr>
</table>
</p>

<form method=post>
<table border="0" width="79%">

<tr>
<td>
<input type="submit" name = "item1" value = "Add item1">
</td>
<td>
<input type="submit" name = "item2" value = "Add item2">
</td>
</tr>

</table>
</form>

</body>
</html>
Jul 17 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On 12 Jul 2004 13:32:01 -0700, An***********@aol.com (Antoni) wrote:
I wondered if anyone could offer some guidance over my php script. I
was hoping the example would allow the user to click the submit
buttons and the item number increment.

And when the user clicks, the my basket icon the items.php scripts
starts and the variables from the current script are passed to this.

I tried to display the variables as the items are incremented, though
I got no value. Can anyone see the logical error?

Thank you

<?php
global $item1, $item2;
If this is the top level of your script, putting 'global' here is without
purpose. What did you want it to do?
if(isset($_GET['item1'])){
$item1++;
$item1 is undefined, incrementing sets it to 1.
}
if(isset($_GET['item2'])){
$item2++;


$item2 is undefined, incrementing sets it to 1.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #2

P: n/a
Antoni wrote:
I wondered if anyone could offer some guidance over my php script. I
was hoping the example would allow the user to click the submit
buttons and the item number increment.

And when the user clicks, the my basket icon the items.php scripts
starts and the variables from the current script are passed to this.

I tried to display the variables as the items are incremented, though
I got no value. Can anyone see the logical error?
try the following mods...
<?php
// removed a line

session_start();
if(isset($_GET['item1'])){
$_SESSION['item1']++;

}
if(isset($_GET['item2'])){
$_SESSION{'item2']++;
}
?>


....skipped some code

<form method="get">

....If you want to use the post method, then all the $_GET references
need to be $_POST...

printf('item1: %d<br>item2: %d',$_SESSION['item1'],$_SESSION['item2']);

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Jul 17 '05 #3

P: n/a
Thank you for replying.

I changed the code, to include the sessions.

When the program runs I get a php error,

PHP Notice: Undefined index: item1 in C:\php\example.php on line 12
PHP Notice: Undefined index: item2 in C:\php\example.php on line 12

In this example, I hoping the user could click the submit buttons
item1 or item2, many times and we keep a counter of each item. Then
pass these values to the next script.

In my orginal code I post, I was trying to achieve the program without
using the sessions. Would it be possible to keep a count of the items,
just using variables?

Many thanks

<?php
session_start();

if(isset($_GET['item1'])){
$_SESSION['item1']++;
}

if(isset($_GET['item2'])){
$_SESSION['item2']++;
}

printf('item1: %d<br>item2:
%d',$_SESSION['item1'],$_SESSION['item2']);
?>

<head>
<title>PHP Example</title>
</head>

<body>
<p>
<table border="0" width="100%">
<tr>
<td>
<a><img src="mybasket.jpg"></a>
</td>
</tr>
</table>
</p>

<form method=post>
<table border="0" width="79%">

<tr>
<td>
<input type="submit" name = "item1" value = "Add item1">
</td>
<td>
<input type="submit" name = "item2" value = "Add item2">
</td>
</tr>

</table>
</form>

</body>
</html>
Jul 17 '05 #4

P: n/a
Could anyone advice?

Thank you
Jul 17 '05 #5

P: n/a
Antoni wrote:
Thank you for replying.

I changed the code, to include the sessions.

When the program runs I get a php error,

PHP Notice: Undefined index: item1 in C:\php\example.php on line 12
PHP Notice: Undefined index: item2 in C:\php\example.php on line 12
That's because you are trying to print the variables before they have
been set on the first request
In this example, I hoping the user could click the submit buttons
item1 or item2, many times and we keep a counter of each item. Then
pass these values to the next script.

In my orginal code I post, I was trying to achieve the program without
using the sessions. Would it be possible to keep a count of the items,
just using variables?
It *can* be done, but sessions are so much easier to do it with.
Otherwise, you'll have to include the vars as a query string in the
form's action:

echo '<form method="post" action="', $_SERVER['PHP_SELF'], '?item1=',
$_POST['item1'], '&item2=',$_POST['item2'], '">

Then when you process the script you'll need to do something like:

if(isset($_POST['item1']) && isset($_GET['item1']))
$_POST['item1']+=$_GET['item1']
}else{
$_POST['item1']=$_GET['item1'];
}

if(isset($_POST['item2']) && isset($_GET['item2']))
$_POST['item2']+=$_GET['item2']
}else{
$_POST['item2']=$_GET['item2'];
}

Realize that the code I am giving you here is *all*untested* but in
theory should work...
Otherwise, see my notes below about using sessions...
<?php
session_start();

if(isset($_GET['item1'])){
$_SESSION['item1']++;
}
Change $_GET to $_POST if you use the post method in the form
if(isset($_GET['item2'])){
$_SESSION['item2']++;
}
same here
<form method=post>


this is the form method I mentioned

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Jul 17 '05 #6

P: n/a
Thank you for replying.

I decided to use sessions and changed my code to include the post
function.

The php message was displayed

PHP Notice: Undefined index: item1 in C:\php\example.php on line 14
PHP Notice: Undefined index: item2 in C:\php\example.php on line 14

Should I not be concerned by this notice?

The script now also displays the session values 0, but when I clicked
the submit buttons these numbers did not increment. And just wondered
if I needed to change some option in the php.ini file?

Many thanks

<?php
session_start();

if(isset($_POST['item1'])){
$_SESSION['item1']++;
}

if(isset($_POST['item2'])){
$_SESSION['item2']++;
}
?>

<?php
printf('item1: %d<br>item2:
%d',$_SESSION['item1'],$_SESSION['item2']);
?>

<head>
<title>PHP Example</title>
</head>

<body>
<p>
<table border="0" width="100%">
<tr>
<td>
<a><img src="mybasket.jpg"></a>
</td>
</tr>
</table>
</p>

<form method=post>
<table border="0" width="79%">

<tr>
<td>
<input type="submit" name = "item1" value = "Add item1">
</td>
<td>
<input type="submit" name = "item2" value = "Add item2">
</td>
</tr>

</table>
</form>

</body>
</html>
Jul 17 '05 #7

P: n/a
In article <93*************************@posting.google.com> , Antoni wrote:
PHP Notice: Undefined index: item1 in C:\php\example.php on line 14
PHP Notice: Undefined index: item2 in C:\php\example.php on line 14


The first time you use $_SESSION['item1'].. thus in $_SESSION['item1']++
$_SESSION['item1'] is not defined. Instead of relying on implicit
variable initialisation, you could do it explicit

if (isset($_POST[['item1']))
{
if (!isset($_SESSION['item1']))
{
$_SESSION['item1'] = 0;
}
$_SESSION['item1']++;
}

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #8

P: n/a
Hello all,

I changed my example to initialise the value to 0. The current error
is:

PHP Notice: Undefined index: item1 in C:\php\example.php on line 22
PHP Notice: Undefined index: item2 in C:\php\example.php on line 22

line 22 is: printf('item1: %d<br>item2:
%d',$_SESSION['item1'],$_SESSION['item2']);

Any thoughts?

Thank you

<?php
session_start();

if (isset($_POST['item1'])){
if(!isset($_SESSION['item1'])){
$_SESSION['item1'] = 0;
$_SESSION['item1']++;
}
}

if (isset($_POST['item2'])){

if(!isset($_SESSION['item2'])){
$_SESSION['item2'] = 0;
$_SESSION['item2']++;
}
}
?>

<?php
printf('item1: %d<br>item2:
%d',$_SESSION['item1'],$_SESSION['item2']);
?>

<head>
<title>PHP Example</title>
</head>

<body>
<p>
<table border="0" width="100%">
<tr>
<td>
<a><img src="mybasket.jpg"></a>
</td>
</tr>
</table>
</p>

<form method=post>
<table border="0" width="79%">

<tr>
<td>
<input type="submit" name = "item1" value = "Add item1">
</td>
<td>
<input type="submit" name = "item2" value = "Add item2">
</td>
</tr>

</table>
</form>

</body>
</html>
Jul 17 '05 #9

P: n/a
In article <93**************************@posting.google.com >, Antoni wrote:
<?php
session_start();

if (isset($_POST['item1'])){
if(!isset($_SESSION['item1'])){
$_SESSION['item1'] = 0;
$_SESSION['item1']++;
}
}
$_SESSION['item1] is initialised only when $_POST['item1'] is set.
And you only add 1 to $_SESSION['item1'] if it's not set.
printf('item1: %d<br>item2:
%d',$_SESSION['item1'],$_SESSION['item2']);
?>


Meaby you need to have a look at the control structures in the manual?
These things are basic.

--
Tim Van Wassenhove <http://home.mysth.be/~timvw>
Jul 17 '05 #10

P: n/a
Hello,

In this version the error/notice messages have disappeared. Though,
when the submit buttons are clicked the item counter display never
increases over 0.

Could anyone advice how to update the item display?

Or perhaps suggest changes to my php.ini file?

Thanks

<?php
session_start();

if(!isset($_SESSION['item1'])) $_SESSION['item1'] = 0;
if(!isset($_SESSION['item2'])) $_SESSION['item2'] = 0;

if(isset($_POST['item1'])){
$_SESSION['item1']++;
}

if(isset($_POST['item2'])){
$_SESSION['item2']++;
}
?>

<?php
printf('item1: %d<br>item2:
%d',$_SESSION['item1'],$_SESSION['item2']);
?>

<head>
<title>PHP Example</title>
</head>

<body>
<p>
<table border="0" width="100%">
<tr>
<td>
<a><img src="mybasket.jpg"></a>
</td>
</tr>
</table>
</p>

<form method=post>
<table border="0" width="79%">

<tr>
<td>
<input type="submit" name = "item1" value = "Add item1">
</td>
<td>
<input type="submit" name = "item2" value = "Add item2">
</td>
</tr>

</table>
</form>

</body>
</html>
Jul 17 '05 #11

P: n/a
Antoni wrote:
Or perhaps suggest changes to my php.ini file?


An exact copy of your script works as expected here.

Check session_save_path in your php.ini

--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #12

P: n/a
Hello,

My session file are being stored in windows temp folder.

Could anyone see a problem in the following php.ini statements?

Thank you

[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
session.save_path = c:\windows\temp

; Whether to use cookies.
session.use_cookies = 1

; This option enables administrators to make their users invulnerable to
; attacks which involve passing session ids in URLs; defaults to 0.
; session.use_only_cookies = 1

; Name of the session (used as cookie name).
session.name = PHPSESSID

; Initialize session on request startup.
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0

; The path for which the cookie is valid.
session.cookie_path = /

; The domain for which the cookie is valid.
session.cookie_domain =

; Handler used to serialize data. php is the standard serializer of PHP.
session.serialize_handler = php

; Define the probability that the 'garbage collection' process is started
; on every session initialization.
; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.

session.gc_probability = 1
session.gc_divisor = 1000

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

; PHP 4.2 and less have an undocumented feature/bug that allows you to
; to initialize a session variable in the global scope, albeit register_globals
; is disabled. PHP 4.3 and later will warn you, if this feature is used.
; You can disable the feature and the warning seperately. At this time,
; the warning is only displayed, if bug_compat_42 is enabled.

session.bug_compat_42 = 0
session.bug_compat_warn = 1

; Check HTTP Referer to invalidate externally stored URLs containing ids.
; HTTP_REFERER has to contain this substring for the session to be
; considered as valid.
session.referer_check =

; How many bytes to read from the file.
session.entropy_length = 0

; Specified here to create the session id.
session.entropy_file =

;session.entropy_length = 16

;session.entropy_file = /dev/urandom

; Set to {nocache,private,public,} to determine HTTP caching aspects.
; or leave this empty to avoid sending anti-caching headers.
session.cache_limiter = private

; Document expires after n minutes.
session.cache_expire = 180

; trans sid support is disabled by default.
; Use of trans sid may risk your users security.
; Use this option with caution.
; - User may send URL contains active session ID
; to other person via. email/irc/etc.
; - URL that contains active session ID may be stored
; in publically accessible computer.
; - User may access your site with the same session ID
; always using URL stored in browser's history or bookmarks.
session.use_trans_sid = 0

; The URL rewriter will look for URLs in a defined set of HTML tags.
; form/fieldset are special; if you include them here, the rewriter will
; add a hidden <input> field with the info which is otherwise appended
; to URLs. If you want XHTML conformity, remove the form entry.
; Note that all valid entries require a "=", even if no value follows.
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeent ry"
Jul 17 '05 #13

P: n/a
Antoni wrote:
Could anyone see a problem in the following php.ini statements? (snip) session.save_path = c:\windows\temp


Try these:
session.save_path = c:/windows/temp
session.save_path = c:\\windows\\temp
--
USENET would be a better place if everybody read: | to email me: use |
http://www.catb.org/~esr/faqs/smart-questions.html | my name in "To:" |
http://www.netmeister.org/news/learn2quote2.html | header, textonly |
http://www.expita.com/nomime.html | no attachments. |
Jul 17 '05 #14

P: n/a
Thank you for replying.

I will try, though when I looked in the windows\temp folder the
session files are being added to the folder.

It just seems, after creating the session file, the same file is is
not being updated with the new item count.

I am using the dzsoft php editor, which comes with an internal server
to preview scripts.

Thanks you
Jul 17 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.