0

Hello i am writing a calculator in PHP and php is working fine, Now what i want is to get value from users.

Im not sure what im missing or i have to use any JS file to do it. I have inbuilt value set $v = 1000000; but i want user to set value as per there need. Also want inbuild remain for defualt user. I think im not using correct way for form.

html

<form action="index.php" method="Get">
Value: <input type="text" name="v" />
<input type="submit" />
</form>

php

<?php
$i = 0;
$v = 1000000;
do {
    $i++;
    $pay  = round($v + $i);
    echo "<tr><td><b>Value</b> $" . number_format($v) . "</td><tr>";
    $v = $v + $bid;
} while ($i <= 5);
?>

I have modified and hide my other personal codes :)

fixed: $v = isset($_GET['v']) ? (int)$_GET['v'] : 1000000;

Thanks in advance live : http://ffsng.deewayz.in/

3 Answers 3

3

As your form uses $_GET you have to assign that to your $v variable, but with condition, if isset.

Change:

$v = 1000000;

to:

$v = isset($_GET['v']) ? (int)$_GET['v'] : 1000000;
Sign up to request clarification or add additional context in comments.

Comments

3

You need to get the value back from the form on the reload like this:

$v = $_GET['v'];

Otherwise the GET value is simply not used in your code.

Mihai correctly points out that if a form value isn't set, you will get an error if your code tries to use is, so run with the test first.

2 Comments

@DeepakKumar They are really the same thing, except Mihai introduced a test to see if it is passed, if not, it assigns a default value, otherwise it uses the value sent. Use his code (it is a more thorough code sample than mine - which really only addressed the fact that you were not doing anything with the GET data).
Yes i understood thanks so much for solving out my question. very thankfull to you guys :)
1

Just do this

    <?php
$i = 0;
$v =$_GET['v'];
do {
    $i++;
    $pay  = round($v + $i);
    echo "<tr><td><b>Value</b> $" . number_format($v) . "</td><tr>";
    $v = $v + $bid;
} while ($i <= 5);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.