【问题标题】:posting data from an html page to a php file and redirect from the php file to the homepage将数据从 html 页面发布到 php 文件并从 php 文件重定向到主页
【发布时间】:2013-07-26 22:10:41
【问题描述】:

我希望将数据发布到 datahouse.php,它现在会将数据提交到服务器并重定向到索引页面,而不在页面上显示 datahouse.php 文件..

register.html

<form method="POST" action="DataHouse.php">
<input type="text" name="username" value="tempapilogin" /><br>
<input type="password" name="password" value="aF4YMjuQ" /><br>
<input type="text" required name="name" placeholder="Full Name" /><br>
<input type="email" required name="email" placeholder="EMail Address" /><br>
Gender:
    <fieldset data-role="controlgroup" data-mini="true" >
        <input type="radio" name="gender" id="radio-mini-1" value="MALE" />
        <label for="radio-mini-1">MALE</label>
    <input type="radio" name="gender" id="radio-mini-2" value="FEMALE" />
        <label for="radio-mini-2">FEMALE</label>
        </fieldset>     
Birthday: <input type="date" required name="birth_date" placeholder="dd/mm/yyyy" />
Phone Number: <input placeholder="02xxxxxxxx" type="tel" name="phone_number" />
Facebook ID: <input type="email" name="facebookid" />
<table>
<tr>
<td><input type="submit" name="submit" value="Submit"/></td>
</tr>
</table>
</form>

以下是将数据发布到服务器的 php 文件,然后必须重定向到 index.html 页面。

datahouse.php

<?php
//Index.html page to redirect to-->
header("Location: c:\Users\Selorm\Desktop\Maxwell-Internship @ Nandimobile\connect\Index.html");

//create cURL connection
$curl_connection = curl_init();

//create array of data to be posted
array{$post_items[] = 'Username='.strip_tags($_POST['username']);
$post_items[] = 'Password='.strip_tags($_POST['password']);
$post_items[] = 'Email='.strip_tags($_POST['email']);
$post_items[] = 'Gender='.strip_tags($_POST['gender']);
$post_items[] = 'Birthday='.strip_tags($_POST['birth_date']);
$post_items[] = 'Phone Number='.strip_tags($_POST['phone_number']);
$post_items[] = 'Facebook ID='.strip_tags($_POST['facebookid']);}

$username = $_POST['username'];
$password = $_POST['password'];
$name = $_POST['name'];
$email = $_POST['email'];
$gender = $_POST['gender'];
$birth_date = $_POST['birth_date'];
$phone_number = $_POST['phone_number'];
$facebookid = $_POST['facebookid'];

//traverse array and prepare data for posting (key1=value1)
foreach ( $_POST as $key => $value) {
    $post_items[] = $key . '=' . $value;
}

//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

//set options and set data to be posted
curl_setopt_array($curl_connection, array(
 CURLOPT_CONNECTTIMEOUT => 30,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_SSL_VERIFYPEER => false,
 CURLOPT_AUTOREFERER => true,
 CURLOPT_FOLLOWLOCATION => true,
 CURLOPT_POSTFIELDS => array(
 username => 'username',
 password => 'password',
 name => 'name',
 email => 'email',
 gender => 'gender',
 birth_date => 'birth_date',
 phone_number => 'phone_number',
 facebookid => 'facebookid' )
 ));

//perform our request
$result = curl_exec($curl_connection);
$result = json_decode($json);
foreach ($result as $key => $value)
{
 echo $key. ':' ;
}
print_r(curl_getinfo($curl_connection));

//show information regarding the request
print_r(curl_getinfo($curl_connection));
echo curl_errno($curl_connection) . '-' . 
curl_error($curl_connection);

//close the connection
curl_close($curl_connection);
exit;
?>

【问题讨论】:

  • 那么你的问题是什么?将您的标头放入 php 文件,它将重定向!
  • 这里有什么问题?
  • 更好地使用 Ajax POST。
  • @michael92 你的“标题(“位置:c:\Users\Selorm\Desktop\Maxwell-Internship @ Nandimobile\connect\Index.html”);”在执行所有代码之前将用户发送到另一个页面。你需要把它放在 END,否则它会在执行所有其他 PHP 之前将用户送走。
  • 我进行了编辑以删除 JQuery 标记,因为该问题与 JQuery 无关。

标签: php html


【解决方案1】:

我猜问题是页面重新定位,但没有发布任何内容。

datahouse.php一遇到就重定向

header("Location: c:\Users\Selorm\Desktop\Maxwell-Internship @ Nandimobile\connect\Index.html");

把它移到最后,不要在它之前输出任何东西。如果您回显任何内容,您将收到一条错误消息,告诉您标头已发送。

【讨论】:

  • 这其实是正确的答案。如果问这个问题的人更清楚他想要什么,我会发布同样的答案。
  • @Herbert...这就是我认为正在发生的事情...所以我想让你弄清楚 php 文件代码是否正确....
  • @Parrotmaster ,我想在 register.html 页面上发布数据,但首先 datahouse.php 文件应该获取数据,现在将其发布到服务器并根据服务器的响应.. .如果成功,它现在应该重定向到 index.html 页面
  • @Parrotmaster:在 SO 上花足够的时间,您就会掌握猜测问题 实际上 是什么的诀窍。有时这只是在黑暗中拍摄。
  • 您的一个循环中有一个echo 语句。如果您在标题之前输出任何内容,该页面将不会重定向。你得到一个空白页,因为你没有打开错误报告。即使在本地主机上,header("Location: index.html"); 应该也足够了。
【解决方案2】:

您需要向某个 Web 服务器上可见的 URL 发送重定向。

您不能将客户端重定向到服务器硬盘上的任意文件路径。

【讨论】:

  • @Slaks..im 使用服务器硬盘的任意文件路径,因为它尚未在线......
  • @michael92 在这种情况下,重定向仍然有效。如果您使用的是本地 PHP 服务器,您的 URL 将如下所示:“localhost/yourwebsite/page.html”或“127.0.0.1/yourwebsite/page.html
  • @Parrotmaster 我有连接到服务器的凭据,我会将数据发布到该服务器。我现在必须破译发布的数据是否真的发布到服务器,如果它被发布然后我现在应该被重定向到我正在谈论的 index.html 页面
  • 伙计们帮我解决这个我还没有完成的东西...@Parrotmaster,@herbert
  • @michael92:你在开发工具中看到了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多