douqian2524
2014-11-30 23:21
浏览 333
已采纳

WordPress自定义页面提交表单

I'm trying to write a simple script to write some data to mysql then read it. My code is working without a problem alone but I'm trying to use it inside a WordPress page, this is the point problem starts.

I have created a template file for WordPress, and using this template to create the page. Page shows up without a problem but whenever I try to submit the form inside it (my custom php form) it forwards me to index.php .

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<span>Enter Your Code : </span><br/>
<input type="text" name="sha256"><br/>
<p align = right><input type="submit" name="shaSubmit" value="Submit" /></p>
</form>

this is my form (inside custom php), and as you can see it posts the data to itself. At the the start of my custom php code I have

if(isset($_POST['Submit']))

But it doesn't matter, as soon as I click on button, it forward me to domain.com/index.php

Btw, this custom php is on a page with such url domain.com/custompage/

How can make this form work ?

ps. Code above is for reading from mysql.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试编写一个简单的脚本来将一些数据写入mysql然后读取它。 我的代码单独工作没有问题,但我正在尝试在WordPress页面中使用它,这是问题的开始。

我为WordPress创建了一个模板文件,并使用 这个模板来创建页面。 页面显示没有问题,但每当我尝试在其中提交表单(我的自定义php表单)时,它会将我转发到index.php。

 &lt; form action =“  &lt;?php echo htmlspecialchars($ _ SERVER [“PHP_SELF”]);?&gt;“  method =“post”&gt; 
&lt; span&gt;输入您的代码:&lt; / span&gt;&lt; br /&gt; 
&lt; input type =“text”name =“sha256”&gt;&lt; br /&gt; \  n&lt; p align = right&gt;&lt; input type =“submit”name =“shaSubmit”value =“Submit”/&gt;&lt; / p&gt; 
&lt; / form&gt; 
   \  n 
 

这是我的表单(在自定义php中),你可以看到它将数据发布到自己。 在我的自定义PHP代码的开头,我有

  if(isset($ _ POST ['Submit']))
   
  
 

但是没关系,只要我点击按钮,它就转发到domain.com/index.php

nnn

顺便说一句,这个自定义的php是在一个 有这样url的页面domain.com/custompage/

nn

如何使这个表格有效?

PS。 上面的代码用于从mysql读取。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongyan1899 2014-12-01 07:26
    已采纳

    You need to change 2 things.

    (1)make action=""

    don't need to use action=" echo htmlspecialchars($_SERVER["PHP_SELF"])"

    (2)if(isset($_POST['shaSubmit'])){

    the code ....

    }

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drzk21632 2014-11-30 23:25

    You're using the following conditional statement if(isset($_POST['Submit'])) along with the submit button's named element name="shaSubmit".

    You need to make those match.

    Either by changing the name of your submit button to name="Submit"

    or by changing your conditional statement to if(isset($_POST['shaSubmit']))

    which is why your code is failing because of the conditional statement you've set is relying on a form element named "shaSubmit".

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题