Self Submitting form variable increment problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sumeetwork
Forum Newbie
Posts: 3
Joined: Fri Oct 17, 2008 1:19 am

Self Submitting form variable increment problem

Post by sumeetwork »

Hi guys,

this is my first post :) here, i am facing problem in incrementing a variable in a self submitting form. I need to validate a condition - that if a user submits for the first time then a certain div doesn't show up.

My Code: just for the variable increment is:

<body>
<?
static $view=0;

function oneplus($view)
{
$view++;
echo "views = $view<br />";
}
?>
<?php
if(isset($_POST["submit"]))
{
echo "views = $view<br />";
$value=$_POST["value"];
$value++;
oneplus($view);
$name=$_POST["name"];
if(trim($name)==""){
echo"please fill in name.<br />";
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="hidden" name="value" value="<? echo"$value";?>" />
<input type="text" name="name" />
<input type="submit" name="submit" id="submit" />
</form>
</body>
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Self Submitting form variable increment problem

Post by aceconcepts »

You don't "return" $view from your function.
sumeetwork
Forum Newbie
Posts: 3
Joined: Fri Oct 17, 2008 1:19 am

Re: Self Submitting form variable increment problem

Post by sumeetwork »

Oh it that so, oh i'll work on it and see

Thanks man
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Self Submitting form variable increment problem

Post by aceconcepts »

Code: Select all

 
<?
static $view=0;
 
function oneplus($view_count)
{
return $view_count++;
}
?>
<?php
if(isset($_POST["submit"]))
{
echo "views = $view<br />";
$value=$_POST["value"];
$value++;
$view=oneplus($view);
echo "views = $view<br />";
$name=$_POST["name"];
if(trim($name)==""){
echo"please fill in name.<br />";
}
}
?>
 
sumeetwork
Forum Newbie
Posts: 3
Joined: Fri Oct 17, 2008 1:19 am

Re: Self Submitting form variable increment problem

Post by sumeetwork »

Sorry man every time we submit the form it print the same value "Value = 0"
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Self Submitting form variable increment problem

Post by aceconcepts »

To be honest you dont really need the "oneplus" function.

You couls just do it like this:

Code: Select all

 
<?php
session_start();
 
if(isset($_POST["submit"]))
{
if(!isset($_SESSION['view_count'])){ $_SESSION['view_count']=1; } else { $_SESSION['view_count']++; }
echo "views = $_SESSION['view_count']<br />";
$value=$_POST["value"];
$value++;
$name=$_POST["name"];
if(trim($name)==""){
echo"please fill in name.<br />";
}
}
?>
 
Post Reply