如何将输入文本更改为 url link

how to change input text into url link

我有四个输入文本和一个提交按钮 enter image description here

我想要的是将输入文本的结果转换为 link,我将其用作像这样的 whatsapp 消息。

https://wa.me/628123456789?text=Hai%20My%20Name%20Andi

我该怎么办? 这是我的代码

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
  <!-- https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4 -->
    <form action="https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
  <input type="text" name="textInput1" id="textInput1" ><br><br>
  <input type="text" name="textInput2" id="textInput2" ><br><br>
  <input type="text" name="textInput3" id="textInput3" ><br><br>
  <input type="text" name="textInput4" id="textInput4" ><br><br>

  <input type="submit" name="submit" value="Submit">
</form>

  <?php 
      //target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
  $url = null;
  if(isset($_POST['submit']))
  {
    $textInput1 = $_POST['textInput1'];
    $textInput2 = $_POST['textInput2'];
    $textInput3 = $_POST['textInput3'];
    $textInput4 = $_POST['textInput4'];
    $url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
  }
   ?>

</body>
</html>

这是我的代码var_dump结果

enter image description here

只需将表单设置为方法 post,请参阅下面的代码。将名称属性添加到您的输入字段和提交按钮,以便您可以通过 http post 检索它们的值。然后检查提交按钮是否已使用 isset($_POST['submit']) 编辑 post。如果已设置,我们会将您输入字段的值分配给变量,以便重新创建 urls post key/value 对。

重要说明:我不会介绍您输入字段的清理,请务必阅读有关正确清理输入的内容,具体取决于什么您允许 back-end 代码处理以重新创建您的 url.

<form action="/https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4" method="post">
  <input type="text" name="textInput1" id="textInput1" ><br><br>
  <input type="text" name="textInput2" id="textInput2" ><br><br>
  <input type="text" name="textInput3" id="textInput3" ><br><br>
  <input type="text" name="textInput4" id="textInput4" ><br><br>

  <input type="submit" name="submit" value="Submit">
</form>

PHP:

//target url --> https://wa.me/628123456789?text=textInput1%20textInput2%20textInput3%20textInput4
$url = null;
if(isset($_POST['submit'])){
  $textInput1 = $_POST['textInput1'];
  $textInput2 = $_POST['textInput2'];
  $textInput3 = $_POST['textInput3'];
  $textInput4 = $_POST['textInput4'];
  $url = "https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4;
}

示例输出。在 html 页面中简单地输出 url 例如:

在您的 html 中简单地回显变量 $url。它只会在实际设置时显示在您的代码中。 <?=$url?><?php echo $url;?>

注意: 如果您试图在提交页面之前将 link 放入表单的 action 属性中,以便在输入字段中设置值是表单操作的一部分,您需要在使用 JS 或 JQuery 提交页面之前获取值,并获取更改输入的值或类似性质的值,然后构建 url 在 js/jquery 然后使用 JS/Jquery.

设置你的表单属性 action

编辑: 我希望输入文本的结果是一条 whatsapp 消息,所以它会被放在 urllink。我更新了我的 post

好的,要在填写表单并清理输入后重定向用户,请在 header() 函数中设置 url 并将用户重定向到所需的 url.

*确保您从您的表单中删除action属性,因为您将使用phpheader() 函数代替。

if(isset($_POST['submit'])){
    // I am using filter_var(FILTER_SANITIZE_STRING) in this example. 
    $textInput1 = filter_var ( $_POST['textInput1'], FILTER_SANITIZE_STRING);
    $textInput2 = filter_var ( $_POST['textInput2'], FILTER_SANITIZE_STRING);
    $textInput3 = filter_var ( $_POST['textInput3'], FILTER_SANITIZE_STRING);
    $textInput4 = filter_var ( $_POST['textInput4'], FILTER_SANITIZE_STRING);
    // make sure to test this url by echoing it out before you run the header redirect. 
    $url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);
    header("Location: $url");
    exit();
}

使用带有 header 的 foreach 循环的条件从 post 值构造 url:

if(isset($_POST['submit'])){
  $url = "https://wa.me/628123456789?text="; // declare the core of your url without the post values  
  $i = 1; // increment 
  $k = 0; // key value for $inputs
  $userinput = ''; // empty variable to hold user inputs for encoding
  $num = count($_POST) - 1;  // count the number of items in the array to properly format spaces in url string subtract one for submit button 
  foreach($_POST as $value){ // run a foreach loop on the $_POST
    if($value !== "Submit"){ // we remove the submit post value from our array by omitting it using does not equal    
      $inputs[] = filter_var ( $value, FILTER_SANITIZE_STRING); // create a new array and push values into it
      if($i < $num){ // all but last iterations will produce the space
        $url .= $input[$k]." ";
      }else{ // last iteration will not have a space
        $url .= $input[$k]; 
      }
    }    
  }  
  $url .= urlencode($userinput);
  echo $url; // for testing purposes to make sure the string is populating the input values as you have entered them delete this line after testing. 
  //header("Location: $url"); <-- Uncomment this line to redirect
  //exit(); <-- uncomment exit() if you uncomment header() to close after redirect to make sure code stops on this page. 
}

只需要在内容前添加函数urlencode():

 $url = urlencode("https://wa.me/628123456789?text=".$textInput1." ".$textInput2." ".$textInput3." ".$textInput4);