duanhuau567787 2016-01-08 12:51
浏览 39
已采纳

用php计算html文本字段的输入

At the moment, I try to create a survey in a webpage. At the end of the survey, users are able to fill two text fields with values. With these values, my plan is to calculate an output, displayed for them at the same page. So:

Input: a

Input: b

Result: ab+b-ab (do not concentrate this please, its just an example)

My plan is that the user is able to fill the two input fields and by a buttonclick, a php function is calculating the result field (by my own algorithm depending on input - this is already working) and fills this field. Do i have to link to another webpage for this purpose? And how is it possible to grab the two input values and give it to my php function? And as last thing, how is it possible to start a php function either embedded in html or in an own file?

I tried your solution and some others as well (fetching inputA and inputB from the DOM with document.getElementById does not work. Below is my code

<form>
    InputA:<br>
    <input type="text" id="inputA"/></br>
    InputB:<br>
    <input type="text" id="inputB"/></br>
    Output:<br>
    <input type="text" id="output"/>
</form> 

<input name="go" type="button" value="Calculate" id="calculate" >

<script type="text/javascript" src="http://code.jquery.com/jquery-2.1.4.min.js" ></script>
<script type="text/javascript">
$("#calculate").click(function(){
        $.get( "submit.php", { value1: $("#inputA").val(), value2: $("#inputB").val() } )
        .done(function( data ) {
        $("#output").val(data);
    });

});

</script>

submit.php:

<?php 
        $value1 = $_POST['value1'];
        $value2 = $_POST['value2'];

        $output = $value1 + $value2;
        echo($output);

    }
?>

When I check with firebug the error, i get a: no element found exception in both (html and php) files. Seems like the problem is, that with: value1: $("#inputA").val(); no value is givent to the server or it can not be handled there.

If i grab the value from the DOM, I can "bring" the value inside the .click function but there is still a "no element found exception" by calling the submit.php.

I have no idea what i am doing wrong, any suggestions? Do i need to install/bind anything in for using JQuery?


After some additional changes, it finally worked (one thing was the header line in the submit.php file):

<form>
    WorkerID:<br>
    <input type="text" id="workerId"/></br>
    CampaignId:<br>
    <input type="text" id="campaignId"/></br>
    Payment Code:<br>
    <input type="text" id="payCode"/>
</form> 

<input name="go" type="button" value="Calculate" id="calculate" >

<script type="text/javascript">
$("#calculate").click(function(){
    $.get( 'submit.php', { wId: $('#workerId').val(), cId: $('#campaignId').val()} )
    .done(function( data ) {
        $('#payCode').val(data.payCode);
    });     
});

and submit.php:

<?php 
    header('Content-Type: text/json');

    $workerId = $_GET['wId'];
    $campaignId = $_GET['cId'];

    $payCode = $campaignId . $workerId;

    $result = array("status" => "success",
                    "payCode" => $payCode);
    echo json_encode($result);
?>
  • 写回答

1条回答 默认 最新

  • doujia7779 2016-01-08 13:14
    关注

    To simplify, i am using jQuery, doing this in vanilla JS is a real pain in the a** in my opinion.

    You can use .get(), which is the GET shorthand for .ajax().

    With that code, you bind a handler on your submit button and make a AJAX request to your PHP and fill the result your PHP gives into your result field asynchronously.

    $("#calculate").click(function(){
    
      $.get( "path/to/your_php.php", { value1: $("#inputA").val(), value2: $("#inputB").val() } )
      .done(function( data ) {
        $("#output").val(data);
      });
    
    });
    

    Also change your submit to something like this:

    <input name="go" type="button" value="Calculate" id="calculate" >
    

    Like that, your button won't submit a form and therefore synchronously load your PHP.

    Since you seem new to JavaScript and you had this comment

    my button, but here i got redirected to submit, no idea how i can go back to page before with filled textfield

    in your question, i'll tell you, JavaScript works while the DOM (Document Object Model) is loaded, means you can access your elements when already loaded and alter them.

    Getting the value of a input is as easy as that in jQuery:

    $("#inputA").val();
    

    With the AJAX you get what your php will return in data.

    // the { value1: $("#inputA").val(), value2: $("#inputB").val() } object
    // is what you send to your PHP and process it
    $.get( "path/to/your_php.php", { value1: $("#inputA").val(), value2: $("#inputB").val() } )
    .done(function( data ) {
         // data is what your php function returned
    });
    

    Using JS you can now change your elements as just said, effectively meaning to change the value of your output here:

    $("#output").val(data);
    

    "Working" Example: JSFiddle (There is no PHP to access to, so it will not do anything actively)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器