Page 1 of 1

Self Submitting form variable increment problem

Posted: Fri Oct 17, 2008 1:25 am
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>

Re: Self Submitting form variable increment problem

Posted: Fri Oct 17, 2008 2:04 am
by aceconcepts
You don't "return" $view from your function.

Re: Self Submitting form variable increment problem

Posted: Fri Oct 17, 2008 2:25 am
by sumeetwork
Oh it that so, oh i'll work on it and see

Thanks man

Re: Self Submitting form variable increment problem

Posted: Fri Oct 17, 2008 2:49 am
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 />";
}
}
?>
 

Re: Self Submitting form variable increment problem

Posted: Fri Oct 17, 2008 2:57 am
by sumeetwork
Sorry man every time we submit the form it print the same value "Value = 0"

Re: Self Submitting form variable increment problem

Posted: Fri Oct 17, 2008 3:50 am
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 />";
}
}
?>