Increment variable value on button click

I have a form button that I would like to have increment the value of $capnum by 1 when it is clicked. How can I do this?

I tried:

<?php
$capnum=0;
?>
<input type="button" id="C2" value="test" onclick="<?php $capnum=$capnum+1;?>" />
<?php echo $capnum;?>

That just does the 1 increment and outputs 1 all the time.

Thanks!

PHP isn’t like JavaScript - in fact, it has nothing to do with HTML at all.

That code, on the PHP side, is actually only doing this:

<?php
$capnum = 0;
$capnum++; //quicker way of adding 1
echo $capnum;
?>

It completely ignores HTML output, therefore PHP can’t interact with events that should be javascript.

you have two options:

[list=1]
[]Do it in Javascript, Live
[
]Do it in PHP, requires refresh[/list]

If you want to do it in JavaScript, you’d do this:

<script type="text/javascript">
var capnum = 0;
function add(){
     capnum++;
     document.getElementById('display').innerHTML = capnum;
}
</script>
<button onclick="add()">Add</button>
<div id="display"><script type="text/javascript">document.write(capnum);</script></div>

or in PHP:

<?
session_start();
$_SESSION['capnum'] = ((isset($_SESSION['capnum'])) ? $_SESSION['capnum'] : 0);
if(isset($_GET['add'])){
     $_SESSION['capnum']++;
}
?>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="get">
<input type="submit" name="add" value="add" />
</form>
<?
echo $_SESSION['capnum'];
?>

Hope that helps,
Jake Arkinstall

Jake,

That does help. After reading that, I’m not sure why I was trying that. I knew better.

I can’t refresh, so JS will be the way to go. How can I get a PHP variable set to the JS output?

Perhaps maybe I can’t. After thinking about it, the JS is browser side, and no way for the PHP to grab the value before submitting and being sent to the next page.

What I need to do is count the number of times it is clicked, and pass that value along with form values to the next page for PHP processing.

Got any suggestions?

Sure, I think you can use the javascript to add 1 to the value of a hidden form field:

<script type="text/javascript">
var capnum = 0;
function add(){
     capnum++;
     document.getElementById('count').value = capnum;
}
</script>
<form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post">
<button onclick="add()">Add</button>
<input type="hidden" name="count" id="count" value="0" />
<input type="submit" />
</form>

And it’ll send along with the rest of the data.

Thanks Jake. That did the trick. Perhaps you can help me some with the next step.

First let me give a very quick overview. I have a form where the “customer” is ordering photo shoots. What we did above was count the number of shoots they are ordering. That number gets passed to the email script where I need to email the results of the form.

What I am having trouble with is I need to only email the results, and not have empty data. On the form page, there are just 30 forms hidden in a div, and each time they click the “Add” button, it just shows the next form. So really 30 forms worth of data are sent.

What I am trying to do is tell the email script how many of the fields are filled out. The fields follow the logic of cap number plus field name. So a data set might look like:

1field1
1field2
1field3

2field1
2field2
2field3

3field1
3field2
3field3

and so on.

The problem of course is that this will contain 30 sets, but with a lot of empty ones. So how can I use that number I obtained from your script and only email the ones that have data? I am using the following email code:

<?php
                  
    //Grab values from form
    $var= $_POST['Name']; //repeat for all form fields

//Email values    
if (@mail("email@email.com", "Headwear Photography Request Form", 
"A request form has been submitted with the following information:
        
Data set 1
Data set 2
Date set 3
repeat for all data sets filled out"
        
,"From: email@email.com")
) { 
 echo('Success messge'); 
} else { 
 echo('Fail message'); 
} ;
        
?>

I hope this makes sense. Thanks for your help.

Just to clarify, there arent’ 30 forms. There is one form with 30 data sets repeated. Each is in a hidden div and the next is shown when the button is clicked.

There are multiple ways of going about it. Can I see the names of the fields in each dataset? That way I could build up the code more efficiently.

put name, email, etc… for the names of the fields

<input type="text" name="name[]">

then arrays will be returned with the data and you can use foreach to go through the data and check if it has been filled out

i made a quick example

http://www.trumedia.nu/test.php

EDIT: This way you dont even need javascript to increment the value.

Thanks galen.

That makes sense. I know basic PHP, and don’t use it much. But when shown I can usually run with it.

So how do I get my email to use that array? Wouldn’t I need some sort of while loop? Not sure how to make that work with that email script. I don’t want separate emails for each data set, only 1 email with all data. That is my hang up now.

Jake, here are my fields:

description
ffull
f34left
f34right
f58left
f58right
fsideleft
fsideright
bfull
b34left
b34right
b58left
b58right
bunderstraight
bunderleft
bunderright
cfront
cside
cback
cvsr
background
notes

That data repeats for each shoot.

Also, I am passing the form data to a separate page. I usually do:

<?php $var=$_POST['fieldname'];?>

How do I do it with an array? Would doing it the same way keep the array in tact?

Ok, I see that I cannot do that. Looks like I either need to have the email script on the same page or use sessions to pass the data to another page.

Correct?

Thanks for your help guys.

That data repeats for each shoot.

What identifies the separate datasets?

There are 30 sets of form fields (dynamically generated and named using PHP).

I did have each field individually named, but it may make more sense to make an array for each field.

Click here for the example. You can fill out the first form and click Continue, then you’ll see what I am trying to do.

This is how it is currently setup. There are 30 sets of the fields i posted above, so they would be named like this:

1description
1ffull
1f34left
1f34right
1f58left
1f58right
1fsideleft
1fsideright
1bfull
1b34left
1b34right
1b58left
1b58right
1bunderstraight
1bunderleft
1bunderright
1cfront
1cside
1cback
1cvsr
1background
1notes

2description
2ffull
2f34left
2f34right
2f58left
2f58right
2fsideleft
2fsideright
2bfull
2b34left
2b34right
2b58left
2b58right
2bunderstraight
2bunderleft
2bunderright
2cfront
2cside
2cback
2cvsr
2background
2notes

And so on until 30. But I think this could be done with the arrays.

Yep. Well, saying that the ‘description’ field is mandatory (the script would check if it exists - if it doesn’t it will not bother with the rest of the form. If it has a chance of not being filled in, change it to a mandatory field).:


<?php
$mand_field = "description";
$field_list = array("description","ffull","f34left","f34right","f58left","f58right","fsideleft","fsideright","bfull","b34left","b34right","b58left","b58right","bunderstraight","bunderleft","bunderright","cfront","cside","cback","cvsr","background","notes");
$msgout = "";
for($i = 1; $i <= 30; $i++){
    if(isset($_POST[$i.'description'])){
        $msgout .= "<br /><br /><br />Form ".$i;
        foreach($field_list as $field){
            $msgout .= "<br />".$field.": ".$_POST[$i.$field];
        }
    }
}
if (@mail("email@email.com", "Headwear Photography Request Form",
"A request form has been submitted with the following information:
".$msgout,"From: email@email.com")
){
echo('Success messge');
}else{
echo('Fail message');
}
?>

That should work.

Thanks Jake. I’ll be looking at this today.

Jake,

I have the email sending, but it is not sending any of the data. I am a little confused by…

if(isset($_POST[$i.'description']))

Wouldn’t result in 1description, 2description and so on? Those would not exist. Should it not be [1]description?

I am not a PHP guru so I could be way off. Any help is appreciated.

Looks as though you may have based it off the fields I gave you being individually named. I had changed all the fields to arrays. But I am going to change back and see if it works.