【问题标题】:Html form onsubmit edit a text field in the form?html表单onsubmit编辑表单中的文本字段?
【发布时间】:2012-05-17 12:16:50
【问题描述】:

我正在学习 php 和 html。

我试图在按下提交按钮时运行一个函数。然后我希望它使用该函数中的变量编辑表单上的文本字段。无需转到新页面...就像自我刷新一样。

我该怎么做?谢谢。

我想我已经看到了如何使用 JavaScript getelementbyID 做到这一点。但我需要通过 php 来完成。

也许一种简单的解释方法是:按下按钮时,它会自动在文本字段中生成密码。

我正在使用的功能:

<?
function genkey($length){
    $key = '';
    list($usec, $sec) = explode(' ', microtime());
    mt_srand((float) $sec + ((float) $usec * 100000));

    $possibleinputs = array_merge(range('z','a'),range(0,9),range('A','Z'));

    for($i=0; $i<$length; $i++) {
        $key .= $possibleinputs{mt_rand(0,61)}; }
    return $key;
}
?>

【问题讨论】:

  • 看看一些 Ajax 教程——应该可以帮助您入门。

标签: php html forms


【解决方案1】:

像这样:

//    index.php
<?php
function genkey($length){
    $key = '';
list($usec, $sec) = explode(' ', microtime());
mt_srand((float) $sec + ((float) $usec * 100000));

$possibleinputs = array_merge(range('z','a'),range(0,9),range('A','Z'));

for($i=0; $i<$length; $i++) {
    $key .= $possibleinputs{mt_rand(0,61)}; }
return $key;
}

$data = array();
if( !empty($_POST['variable']) ) {
    $data['variable'] = genkey( strlen($_POST['variable']) );
} else {
    $data['variable'] = '';
}
?>
//...HTML...
<form action="" method="POST">
<input name="variable" value="<?=$data['variable']?>">
<input type="submit" value="toPHP">
</form>
//...HTML...

【讨论】:

  • 这行得通!谢谢,与 ajax 替代品相比非常简单。如果未声明 $variable 并且在该字段中出现错误,但它处于新负载状态...解决此问题的最佳方法是什么?
  • 已修复。我关闭了这个警告并添加了一个数组,该数组不会意外到达另一个同名变量。
  • 那不再有效。如果我删除 @ 符号,它会这样做。如果我什至不点击它就不会。
  • 是的,我的错,它在某些设置下无法工作。修复它以声明一个空变量的好方法,但通常只是关闭所有警告。再次编辑。
【解决方案2】:

使用ajax 从 php 文件中获取密码并更新文本字段。

一些使用 jquery 和 ajax 的示例代码:

$.ajax({
url : 'ajax.php',
type : 'post',
success : function(data){
$('#password_field').val(data)        
}
});

【讨论】:

    【解决方案3】:
    $.ajax({
        url: "/your_php_page/function", 
    
        type: "POST",
    
        data: "parameters you want to post to the function",
    
        success: function(data){
            $('#input_field_to_be_updated').val(data);
        }
    
    });
    
    Your php function should be echoed the password which you want to place it in input box.
    

    【讨论】:

      【解决方案4】:

      使用该函数制作一个单独的php文件(keygen.php)。

      <?php
      
      $length=$_GET['klength'];
      
       echo genkey($length);
      
        function genkey($length){
         $key = '';
        list($usec, $sec) = explode(' ', microtime());
        mt_srand((float) $sec + ((float) $usec * 100000));
      
      $possibleinputs = array_merge(range('z','a'),range(0,9),range('A','Z'));
      
      for($i=0; $i<$length; $i++) {
        $key .= $possibleinputs{mt_rand(0,61)}; }
      return $key;
      }
      
      ?>
      

      在你的 html 文件中添加这段代码

      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"    type="text/javascript"></script>
      
      <script type="text/javascript">
       $(document).ready(function() {
      
      $.get('keygen.php?length=32', function(data) {
      alert(data)
       });    
      
      });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-19
        • 1970-01-01
        • 1970-01-01
        • 2019-03-26
        • 2017-12-26
        • 1970-01-01
        • 2014-06-27
        • 1970-01-01
        相关资源
        最近更新 更多