473,394 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Passing values between scripts:

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
14 3101
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
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
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
Could anyone advice?

Thank you
Jul 17 '05 #5
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: go-tard | last post by:
Hello, I'm wondering if there's a way to pass a value from apache to php through httpd.conf. I've noticed that php settings can be changed using php_value. Is there a way to send a value that is...
4
by: Jason Us | last post by:
Does anyone have experience with passing variables from an ASP page to a JSP page. The way it currently works in passing the SSN in the URL. This cannot be good. I thought that storing a...
2
by: Dr. Know | last post by:
When using the Response.Redirect in test1.asp in this manner: Response.Redirect ("test2.asp?err=something&msg=somethingelse") You can pass values calculated in test1.asp to test2.asp. But this...
3
by: equip200 | last post by:
Hello All, I would like some assistance with an issue I have. I will do my best to explain it. I have 2 programs , one is called "AMI" and it could use VBscript to execute procedures . the other...
1
by: Neil Negandhi | last post by:
I've set session cookies in an asp script and need to retrieve the values in an asp.net application (same virtual directory). I've taken the sample code in the httprequest.cookies VS.NET2003 help...
1
by: | last post by:
Hi, 1st, I did a search and could not find any info on this, the Google results were good, but I'm still have issues...So any help will be great. I have a frame page, which contains 3 frames...
2
by: bombardier | last post by:
I just switched ISPs and am in the process of moving all MySQL databases and PHP scripts to the new ISP. I have some simple HTML forms that post to simple PHP scripts that used to work with my old...
0
by: Mario120560 | last post by:
Hello , would you help me, I have two testpartners scripts and I am trying to pass a value, I am making the variable public but the second script doesn't get the value passed. Please help!!! ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.