开发者

PHP output in the textfield/text

i have a little 开发者_如何学运维problem about PHP there is two input for num1 and num2 and another input answer,,can the output in the php be putted in the input text answer??

<input type="text" name ="num1">
<input type="text" name ="num2">
<input type="text" name ="answer">


Try something like this:

<?php
     if(!empty($_POST['num1'])){
        $num1 = $_POST['num1'];
        $num2 = $_POST['num2']
        $answer = $num1 + $num2;
     }
?>

<form action="yourpage.php" method="post">
    <input type="text" name ="num1" value="<?=@$num1?>">
    <input type="text" name ="num2" value="<?=@$num2?>">
    <input type="text" name ="answer" value="<?=@$answer?>"> 
    <br/>
    <input type="submit" id="btnSubmit" value="POST ME" />
</form>

Not tested btw...


<?php
//get the values and calc..
$answer = $_GET['num1'] + $_GET['num2']; 
//more secure, $answer = floor($_GET['num1']) + ...
?>
<html><body>
<form method="get" action=".">
<input type="text" name ="num1">
<input type="text" name ="num2">
<input type="text" name ="answer" value="<?php echo $answer;?>">
<input type="submit">
 </form>
...


The jQuery answer that was suggested by riky has shown up on SO before. Try:

How to update the value in one text box based on the value entered in another text box?

As per the OP's request for an fleshed out version of the jQuery answer, modifying the linked to answer gives something like:

$('#num1').change(function() {
  var txtAmtval = $('#num1').val() + $('#num2').val();
  $('#answer').val(txtAmtval);
});

$('#num2').change(function() {
  var txtAmtval = $('#num1').val() + $('#num2').val();
  $('#answer').val(txtAmtval);
});

Is that what you had in mind?

Obviously you'd need to include jQuery too.

Please see code in action here: http://jsfiddle.net/9KEsY/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜