如何在php中添加字符串到URL?

时间:2017-03-01 11:25:24

标签: php jquery

我有一个随机字符串,使用我在这里找到的php函数

<?php function generateRandomString($length = 12) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    $randomString;
}

$randomString = generateRandomString(); ?>

我有一张表格

<form method="post" id="form" action="<?php bloginfo('url'); ?>/results/">
    <input type="text" name="height" placeholder="Enter height">
    <input type="text" name="weight" placeholder="Enter weight">
    <input type="submit" id="export">
</form>

当您点击提交时,它会转到.com / results /页面,但我正在尝试将/ results /更改为generateRandomString函数中的随机字符串,但仍会显示已显示的内容结果页面

到目前为止,我已尝试在

形式下添加此jQuery
$("#export").click(function(){
    url = $("#form").attr("action");
    url = url.replace("/results/",<?php echo $randomString ?>);
    $("#form").attr("action", url).submit();
});

但这仍然无效。有谁知道我怎么能这样做呢?

谢谢

下面的php文件 -

    <script src="jquery-3.1.1.min.js"></script>
<?php function generateRandomString($length = 12) {
    $characters =     '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
$randomString = generateRandomString();
?>

<form method="post" id="form" action="<?php bloginfo('url'); ?>/results/">
    <input type="text" name="height" placeholder="Enter height">
    <input type="text" name="weight" placeholder="Enter weight">
    <input type="submit" id="export">
</form>

<script>
$("#export").click(function(){
  url = $("#form").attr("action");
  url = url.replace("/results/",<?php echo $randomString ?>);
  $('#form').attr('action', url).submit();
});
</script>

2 个答案:

答案 0 :(得分:2)

您的函数不返回值,请使用return语句:

 function generateRandomString($length = 12) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
         $randomString .= $characters[rand(0, $charactersLength - 1)];
    }

    return $randomString;
}

答案 1 :(得分:1)

  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>

<?php
 function generateRandomString($length = 12) {
    $characters =     '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}
$randomString = generateRandomString();
?>
  <form method="post" id="form" action="google.com/results/">
    <input type="text" name="height" placeholder="Enter height">
    <input type="text" name="weight" placeholder="Enter weight">

</form>
 <input type="submit" id="export">


<script>
$("#export").click(function(event){
  url = $("#form").attr("action");
  url = url.replace("results/","<?php echo $randomString ?>");
  console.log(url);
 //event.preventDefault();
  $('#form').attr('action', url).submit();
});
</script>