【发布时间】: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 无关。