dongzhenshen7435 2013-12-27 11:58
浏览 48
已采纳

将输入值输入php变量

Is there any method to take value of the input to a php variable.

<input type="text" name="s_amount" style='width:20%;' required>

I want value of this s_amount to take into a php variable $s_amount. This code is already inside a form. I want this conversion happen before submitting the form.

  • 写回答

5条回答 默认 最新

  • dps123456789 2013-12-27 12:09
    关注

    You can't convert a javascript variable into a php variable without communication with the server. So if you would like to turn a client side variable (your input value) into a php variable BEFORE submitting a form you'll need to post/get this variable to the server in a separate request. The best way to do this is by an asynchronous ajax call. If you are using jquery this can be very simple:

    $.ajax({
        url: '/path/to/file.php',
        type: 'POST',
        dataType: 'text',
        data: {param1: $("input[type='text'][name='s_amount']").val()},
    })
    .done(function(response) {
        console.log("response");
        //do something with the response
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });
    

    On the server you can receive this value by getting the posted data like so

    $myphpvariable= $_POST['param1'];
    //here you can manipulate/validate your variable and echo something back to the client
    echo 'this is my php variable: '.$myphpvariable;
    

    Now your ajax call will receive "this is my php variable: [x]" in its .done() callback.

    EDIT: don't forget you have a javascript console in your browser (for example firebug). this can be very handy to see what's going on.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题