我试图将数据从表单传递到另一个文件中的另一个函数,但是这个函数已经有来自另一个函数的其他变量。
<form method="post" action="data.php">
<input type="hidden" name="url" value="http://www.website.com/">
<input type="hidden" name="description" value="description of website">
<input type="hidden" name="image" value="http://www.website.com/image.jpg">
<input type="hidden" name="board_ids" value="$board_ids">
</form>
<?php
function login()
{
random variables with cURL included which is passing data to the post function
post($ch, $cookie, $url);
}
function post()
{
uses $ch, $cookie, $url.
}
?>
现在,在上面的函数login()中,我有以下内容..
$post_url = $_POST['url'];
$post_desc = $_POST['description'];
$post_image = $_POST['image'];
$board_ids = $_POST['board_ids'];
PS:board_ids是一个包含一些特定ID的数组,例如:$board = array('102938', '483756', '002938', '239384');
for($i = 0; $i < count($board_ids); $i++ )
{
post($board_ids[$i]);
}
我想知道如何将$ post_url,$ post_desc,$ post_image和$ board_ids发送到post()函数。
我实际上想要每次使用每个board_id数组运行post(),这可能吗?以上一直说我在帖子功能上有PHP Notice: Undefined offset: 2
。已经连续9个小时了,无法将变量传递给后期功能,不得不寻求帮助。
修改
<?php
foreach($pinterestClass->retrieveAllInfo() as $data)
{
echo '
<tr>
<form method="post" action"data.php">
<td rowspan="1" colspan="1">
<input type="hidden" name="post_url" value="'.$data['p_Url'].'">'.$data['p_Url'].'
</td>
<td rowspan="1" colspan="1">
<input type="hidden" name="post_description" value="'.$data['p_Description'].'">'.$data['p_Description'].'
</td>
<td rowspan="1" colspan="1">
<input type="hidden" name="post_image" value="'.$data['p_Image'].'">'.$data['p_Image'].'
</td>
<td rowspan="1" colspan="1">
<section id="list1" class="dropdown-check-list" onclick="firstDropDown(this)">
<span class="anchor">Boards</span>
<ul class="items">';?>
<?php
$p_iD = $data['p_iD'];
$checkedBoards = $oClass->pCheckedBoards($p_iD);
foreach($pClass->retrieveBoards() as $data)
{
$boardiD = $data['boardiD'];
$boardName = $data['boardName'];
$checked = '';
for($i = 0; !$checked && $i < sizeof($checkedBoards); $i++)
{
if($checkedBoards[$i]['board_iD'] == $boardiD)
{
$checked = 'checked="checked"';
}
}
echo '
<li><input '.$checked.' type="checkbox" name="post_board_id" value="'.$boardiD.'">'.$boardName.'</li>
';
}
?>
<?php echo '
</ul>
</section>
</td>
<td rowspan="1" colspan="1">
Status
</td>
<td rowspan="1" colspan="1">
<div style="background: none repeat scroll 0px 0px rgb(229, 229, 229); padding-top: 5px; height: 22px;">
<i class="fa fa-edit"></i> <input name="post_info" type="submit" class="btn btn-xs default" value="Edit">
</div>
</td>
</form>
</tr>
';
}
?>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$post_desc = $_POST['post_description'];
$post_url = $_POST['post_url']';
$post_image = $_POST['post_image'];
$board = $_POST['post_board_id'];
for($i = 0; $i < count($board); $i++ )
{
$board[$i];
}
function pinLogin()
{
$username = '';
$password = '';
$Cookie = '/cookie.txt'; //cookie session
// this is the http post data for logging in - username & password are substituted in later
$login_post = array(
'source_url' => '/login/',
'data' => '{
"options":{
"username_or_email":"%s",
"password":"%s"
},
"context":{}}',
);
// cURL so it needs $ch, $url is the login url
pinPost($ch, $Cookie, $url);
}
function Post($ch, $pinCookie, $url)
{
$boardId = $board[$i];
$postDesc = $post_desc;
$postUrl = $post_url;
$postImage = $post_image;
}
Login();
?>
答案 0 :(得分:1)
首先,您正在调用pinPost
,但您的函数实际上名为Post
。将其重命名为:
function pinPost($ch, $pinCookie, $url)
其次,您的pinPost
函数正在接收三个从不使用的参数($ch, $pinCookie, $url
)。
第三,您的pinPost
函数正在尝试使用从未传递给它的四个变量:
function Post($ch, $pinCookie, $url)
{
$boardId = $board[$i];
$postDesc = $post_desc;
$postUrl = $post_url;
$postImage = $post_image;
}
$board[$i]
,$post_desc
,$post_url
和$post_image
来自何处?
最后,如果您想在pinPost
函数中访问这些变量,只需直接访问它们:
function pinPost()
{
$postDesc = $_POST['post_description'];
$postUrl = $_POST['post_url']';
$postImage = $_POST['post_image'];
$boardId = $_POST['post_board_id'];
}
您可能希望阅读一些PHP教程,特别是有关函数的教程。这是非常基本的东西,你真的想要理解。
答案 1 :(得分:-2)
对于帖子标记,您需要一个sumbit按钮或ajax提交。
例如:
<form method="post" action="data.php">
<input type="hidden" name="url" value="http://www.website.com/">
<input type="hidden" name="description" value="description of website">
<input type="hidden" name="image" value="http://www.website.com/image.jpg">
<input type="hidden" name="board_ids" value="$board_ids">
<button type="submit">Submit</button> // submit button
</form>
或使用jquery
$("#theForm").ajaxForm({url: 'data.php', type: 'post'});
另外我建议在data.php页面上使用if(isset(variable))statment来检查变量是否存在