【问题标题】:Making Form at Wordpress Template在 Wordpress 模板上制作表格
【发布时间】:2017-04-01 12:43:19
【问题描述】:

我是自学者,所以没有人知道,我不想用 POST 方法制作表单,我用 w3 教程制作。

PHP Form Validation Example

我成功了,我想让它包含在wordpress主题中,所以我把这些代码放在页面模板中,就像这样

<?php
/*
Template Name: Urut Embed
*/
 get_header();
// define variables and set to empty values
$nameErr = $videocodeErr =  $embedurlErr = "";
$name = $videocode = $embed = $embedurl = $embedurut[] = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameErr = "Only letters and white space allowed"; 
        }
    }
    if (empty($_POST["embedurl"])) {
        $embedurl = "";
    } else {
        $embedurl = test_input($_POST["embedurl"]);
        // check if URL address syntax is valid (this regular     expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$embedurl)) {
            $embedurlErr = "Invalid URL"; 
        }
    }
    if (empty($_POST["videocode"])) {
        $videocode = "";
    } else {
        $videocode = test_input($_POST["videocode"]);
        // check if URL address syntax is valid (this regular expression also allows dashes in the URL)
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$videocode)) {
            $videocodeErr = "Invalid URL"; 
        }
    }
    if (empty($_POST["embed"])) {
        $embed = "";
    } else {
        $embed = test_input($_POST["embed"]);
        if($videocode == "sf"){
            $embedurut = str_replace("http://www.savefile.co/","sf;", $embed);
            $embedurut = str_replace(" savefile.co.",".", $embed);
        } else if($videocode == "mp4"){
            $embedurut = str_replace("https://www.mp4upload.com/"," mp4;", $embed);
        } else if($videocode == "yu"){
            $embedurut = str_replace("https://www.yourupload.com/embed/","yu;", $embed);
        } else if($videocode == "gk"){
            $embedurut = str_replace("|"," - gk;", $embed);
        } else {
            $embedurut = $embed;
        }
        $embedurut = str_replace($name,"", $embedurut);
        $embedurut = explode(PHP_EOL, $embedurut);
        sort($embedurut);
    }
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}
?>
<nav class="navbar navbar-default">
    <div class="container">
        <div class="navbar-header">
            <a href="#" class="navbar-brand">Urut Embed Videocode</a>
        </div>
    </div>
</nav>

<div class="container">
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']);?>">
        <div class="input-group input-group-lg">
            <span class="input-group-addon">Remove Strings</span>
            <input type="text" name="name"  class="form-control" placeholder="Masukkan bagian videocode yang ingin dihilangkan" value="<?php echo $name;?>">
        </div>
        <br>
        <div class="alert alert-dismissable alert-success">
            Check sesuai dengan embed video untuk mengubah link menjadi videocode, none jika tidak ingin mengubah.
        </div>

        <label class="radio-inline"><input type="radio" name="videocode" <?php if (isset($videocode) && $videocode=="none"){ echo "checked"; } else if ($videocode==""){ echo "checked"; } ?> value="none">None</label>
        <label class="radio-inline"><input type="radio" name="videocode" <?php if (isset($videocode) && $videocode=="sf") echo "checked";?> value="sf">SaveFile</label>
        <label class="radio-inline"><input type="radio" name="videocode" <?php if (isset($videocode) && $videocode=="mp4") echo "checked";?> value="mp4">MP4Upload</label>
        <label class="radio-inline"><input type="radio" name="videocode" <?php if (isset($videocode) && $videocode=="yu") echo "checked";?> value="yu">YourUpload</label>
        <label class="radio-inline"><input type="radio" name="videocode" <?php if (isset($videocode) && $videocode=="gk") echo "checked";?> value="gk">gk</label>
        <br><br>

        <span class="label label-primary">
            Embeds
        </span>
        <textarea class="form-control" name="embed" rows="5" cols="150"><?php echo $embed;?></textarea>
        <br>

        <input class="btn btn-success" type="submit" name="submit" value="Submit">  
    </form>
    <br>

    <span class="label label-primary">
        Embed Terurut
    </span>
    <textarea class="form-control" name="embed" rows="12" cols="150"><?php
    $alength = count($embedurut);
    for($x = 0; $x < $alength; $x++) {
    echo $embedurut[$x];
    }
    ?></textarea>
</div>

但是当我运行它时,它返回了 404 页面,但是页面 url,而不是 404 url​​。我根本不知道如何使它工作。 你们能帮帮我吗?抱歉英语不好。

问候

【问题讨论】:

    标签: php wordpress forms


    【解决方案1】:

    从 wordpress 管理仪表板创建页面并调用此模板,然后运行该页面。

    【讨论】:

    • 抱歉回复晚了我的意思是,我已经运行了,但是当我点击提交按钮时,它会重定向到自身页面并显示 404。
    • 我从不接触 .htaccess 文件,我需要在那里更改什么?
    • 您将在 settings > permalinks 下的 wordpress 管理仪表板中获得更新后的 .htaccess 文件,只需复制并粘贴代码在您的文件夹中
    【解决方案2】:
    • 转到 wordpress 添加新页面。
    • 给页面一个标题。
    • 并选择“Urut Embed”作为此页面的模板
    • 保存并查看页面。

    【讨论】:

    • 抱歉回复晚了我的意思是,我已经运行了,但是当我点击提交按钮时,它会重定向到自身页面并显示 404。
    • 这是你应该做的。将 textarea 的名称从 embed 更改为 myembed 或其他名称。我在 localhost 中尝试了此代码,它不断将我重定向到带有嵌入的东西,这意味着“嵌入”名称已在其他地方使用。
    猜你喜欢
    • 2014-04-25
    • 2014-12-14
    • 2012-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多