【问题标题】:connect form page on wordpress with php file将wordpress上的表单页面与php文件连接起来
【发布时间】:2015-06-25 05:31:10
【问题描述】:

我在 wordpress 的一页上创建了表单:

<form action="scores_send.php" method="post">
<strong>Date: </strong><input type="date" name="date" /><br /> 
<strong>TEAM 1:</strong>
Player 1.: <input type="text" name="team1_player1" /><br /> 
Player 2.: <input type="text" name="team1_player2" /><br /> 
<strong>TEAM 2:</strong>
Player 1.: <input type="text" name="team2_player1" /><br /> 
Player 2.: <input type="text" name="team2_player2" /><br /> 
<strong>SCORE:</strong>
Team 1. <input type="number" name="score_team1" min="0" max="5"/> : <input type="number" name="score_team2"  min="0" max="5"/> Team 2.<br /> 
<input type="submit" value="Add" /> 
</form>

我还编写了连接到我的外部(无 wordpress)postgres 数据库的 php 代码(代码中的连接参数不正确)并将信息从表单插入到一个表中:

<?php
$date = $_POST['date']; 
$t1p1 = $_POST['team1_player1']; 
$t1p2 = $_POST['team1_player2'];
$t2p1 = $_POST['team2_player1']; 
$t2p2 = $_POST['team2_player2'];
$score1 = $_POST['score_team1'];
$score2 = $_POST['score_team2'];

if($date and $t1p1 and $t1p2 and $t2p1 and $t2p2 and $score1 and $score2) { 

    $connection = pg_connect("host=127.0.0.1 port=5432 dbname=scores user=user password=pass")
    or die('Brak polaczenia z serwerem PostgreSQL'); 
    $db = @pg_select_db('scores', $connection) 
    or die('Nie moge polaczyc sie z baza danych'); 

    $team1 = @pg_query("INSERT INTO scores.games (score_team1, score_team2, datetime, team1_player1, team1_player2, team2_player1, team2_player2) VALUES ('$score1', '$score2', '$date', '$t1p1', '$t1p2', '$t2p1', '$t2p2'"); 

    if($team1) echo "Scores added."; 
    else echo "ERROR! Scores not added"; 
     pg_close($connection); 
} 
?>

我尝试了两种方法: 1.在我的主题文件夹中创建模板php文件和 2.将此代码粘贴到wordpress中的内容编辑器(使用此插件:https://wordpress.org/plugins/insert-php/

两种方式都行不通。我在 中更改了部分“动作”,但可能是错误的。

【问题讨论】:

  • 使用($date && $t1p1 && $t1p2 && $t2p1 && $t2p2 && $score1 && $score2)
  • 而不是 "if($date and $t1p1 and $t1p2 and $t2p1 and $t2p2 and $score1 and $score2)" 或者如何?我对 PHP 的了解不够 ;)

标签: php wordpress forms postgresql


【解决方案1】:

只需将表单的操作更改为:http://yoursite.com/scores_send.php

确保您的 scores_send.php 位于安装的根目录中。

所以,你的表单看起来像:

<form action="http://yoursite.com/scores_send.php" method="post">

【讨论】:

  • 我试过你的方法,在表单浏览器中单击“添加”后显示空白页面,并且在数据库中没有添加任何内容。也许是我的 PHP 代码的问题?
【解决方案2】:

您应该在表单操作中使用http://example.com/page-slug/

page-slug 是通过将 scores_send.php 作为模板创建的另一个页面的 slug。它将所有表单值发布到 score_send.php。从那里您可以进一步插入外部数据库。

<form action="http://example.com/page-slug/" method="post">

【讨论】:

  • 我也尝试了你的方法,它与 Sunil Chaudhary 的方法中的情况相同 - 空白页,没有任何内容添加到我的数据库中。 :(
  • 首先您需要检查目标页面中的 get_header() 和 get_footer() 函数,如果不存在则使您的页面空白。另一个您需要测试“回声”并且还需要测试'echo' post value 以交叉检查目标值以及您的表单是否实际发布。如果这样您无法获得结果,请尝试使用 Ajax 方法发布表单值并从那里插入数据库。
猜你喜欢
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-03
  • 2015-01-28
  • 1970-01-01
相关资源
最近更新 更多