duanfei8149 2014-12-09 20:25
浏览 36
已采纳

$ _POST和代码操作

I want to grab the value of a field using $_POST, manipulate it, then pass the value back to the same page to the same field before the PHP code manipulates it.

If I put the PHP code after the field, it manipulates the code, reloads the page but doesn't put the manipulated code back into the field.

    if (!isset($input)) {
        $input = '';
    }   

    echo '<form id="testform" method="post" action="">';
    echo '<input type="text" name="inputText" value="' . $input . '">';
    echo '<button type="submit" name="button"> Button </button>';
    echo '</form>';

    $input = $_POST['inputText'];

    if(isset($_POST['inputText'])) {
        $input = $input . ' manipulated';
    }

    echo $input; //test

If I put the PHP code before the field, it can't find the field to manipulate the value...

    if (!isset($input)) {
        $input = '';
    }

    $input = $_POST['inputText'];

    if(isset($_POST['inputText'])) {
        $input = $input . ' manipulated';
    }

    echo $input; //test

    echo '<form id="testform" method="post" action="">';
    echo '<input type="text" name="inputText" value="' . $input . '">';
    echo '<button type="submit" name="button"> Button </button>';
    echo '</form>';

Obviously the first approach is more correct, but how do I pass the $input variable to the field before the rest of my PHP manipulation code executes?

I tried $_POST['inputText'] = $input as a desperate attempt but nothing..

  • 写回答

2条回答 默认 最新

  • douweiluo0600 2014-12-09 21:06
    关注

    Well, from what I've understood in your explanation, you want to change the input value to something else and show it in he same field. If that's correct, you may want to do this:

    <form id="testform" method="post" action="">
        <input type="text" name="inputText" value="<?php echo ( isset($_POST['inputText']) ) ? sprintf( '%s manipulated', $_POST['inputText'] ) : ''; ?>">
        <button type="submit"> Send </button>
    </form>
    

    Let me know if that's what you wanted. Regards !

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

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常