help with some problem

I’m trying to code a PHP page with a textfield, a button and one link, every time I click in the button the value is added(sum) to a variable and when I click the link I can see the value of this variable somewhere in the page.

what i did

[php]

<?php if (isset($_POST['submit'])) { $formvalue = $_POST['input_value']; } ?>
    </body>
[/php] [php] <?php if (isset($_POST['submit'])) {
            $value = $_POST['value_enter'];
            if ($value== 1){
            @$a = $a +1;      
            }
            echo @$a;

        } 
        ?>[/php] ...but doesn't work the number is not adding

sybn,

Assuming that both of these are on a server somewhere and you are opening the first html code,
you need a basic tutorial on forms and PHP in general.
First, PHP is run SERVER-SIDE. Forms are handle by HTML on the CLIENT-SIDE. Since you did not give the names of the two files you show, I will assume the first one is the actual “index.html” file that you are opening to start with.
If this is true, the PHP code in this page is executed once only. (When you load the page.)
When you press the submit button, it goes to the index.php file and stops. Therefore would not work.

Let’s start with what PHP is. Pages stored on the server can contain PHP code. This code is executed BEFORE the HTML code is sent back to the CLIENT-SIDE browser where it is displayed. This means that you can not call a PHP file and expect it to do anything as far as displaying inside the browser.

If you call a PHP file on the server, it MUST have HTML inside it for you to be able to see the results.
Also, variables can not be passed to other pages just by calling the page. You either have to pass them as POSTED info or by PHP $_SESSION variables. Your code was partially set up to send it as POSTED info.

To handle a post, PHP code would have to include data to display the result. This would be HTML code.
Using your sample, you can change your server-side file (index.php) to something like the following:
[php]

<?php if (isset($_POST['submit'])) { $value = $_POST['value_enter']; if ($value== 1){ @$a = $a +1; } echo @$a; } ?> [/php]

But, note, this will not allow you to keep pressing a button. The button would send you to this HTML/PHP file which show results. Instead, you would have to create a page to call itself. I would use the PHP $_SESSION variables. It is fairly simple and the variable set will stay alive until you log off.

Google PHP session sample for more on that.

Lastly, remember that PHP only runs on a server and can not run on your desktop unless you have set up a server. (This can be done quickly with xampp http://www.apachefriends.org/en/xampp.html )
Hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service