使用foreach循环将$ var发送到另一个.php页面? [英] Using foreach loop to send $var's to another .php page?

查看:187
本文介绍了使用foreach循环将$ var发送到另一个.php页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从API脚本中创建一种方法来更新数据库中的价格,一次搜索一个项目。

I'm trying to create a way to update prices in a database, from an API script, that searches out items one at a time.

API脚本

另一个页面,将有一个cronjob和一个foreach循环,所有可变项目编号被输出

Another page, will have a cronjob, and a foreach loop with all the variable item numbers being outputted into the function.

然后,foreach循环函数逐个获取项目编号,将它们发送到API.php页面,然后按项目编号搜索并更新数据库。

The foreach loop function then, takes the item numbers one by one, sends them to the API.php page that then searches by item number and updates into the database.

目前,我试图这样做与echo的,和html帖子到所需的页面。除了一些javascript,页面加载后自动提交的项目到api页面。也许我会这样错了!

Currently I'm trying to do this with echo's, and html "posts" to the desired page. Along with some javascript that automatically submits the item to the api page once the page is loaded. Perhaps I'm going about this all wrong!

发生的是,foreach循环读取所有的数字并输出正确,但然后我的javascript代码在那里提交。我只得到发送到API的第一个条目号条目,当然插入数据库。

What is happening, is, the foreach loop reads all the numbers and outputs them correctly, but then the javascript code I have in there submits. And I only get the very first item number entry sent to the API, and of course inserted into the database.

因为这使得页面远离.php foreach循环页面,foreach循环不能继续,我只停留在我的第一个条目。

Since this makes the page navigate away from the .php foreach loop page, the foreach loop cannot continue and I am stuck with only my first entry.

对不起,长的帖子!

以下是我的代码:

$sql = "SELECT id, item_number FROM products ORDER BY id DESC";
$query = mysql_query($sql) or die (mysql_error());
while ($result = mysql_fetch_array($query)) {

$itemnumber = array($result['item_number']);

//因为所有item_numbers都被拉出数据库,

//above the array is being set since all the item_numbers being pulled out of the database--so this is more than just one item number, as can seen in the echo...

foreach ($itemnumber as $item_number) {

echo "<form method=\"post\" action=\"api.php\" name=\"ChangeSubmit\" id=\"ChangeSubmit\" >";
echo "<input type=\"text\" name=\"item_number\" value=\"{$item_number}\"/>";

echo "<script type=\"text/javascript\">
function myfunc () {
var frm = document.getElementById(\"ChangeSubmit\");
frm.submit();
}
window.onload = myfunc;
</script></form>";

/*This outputs on the page all the item numbers of every product, with input text boxes next to them, then after a split second the javascrip echo submits -- only the very first item number to the api. */
}
}

?>

这是主页面,然后发送到api.php (搜索:$ _ POST ['item_number'] ---然后插入/更新数据库)。

So this is the main page, then sends it over to the "api.php" which just looks up the item number by something like (Search:$_POST['item_number'] ---then insert/update database).

我想就是这样!希望一切都有意义!谢谢!

I think that is about it! Hope that all makes sense! Thanks!

推荐答案

现在,代码回顾了我的数据库中的所有项目编号。如果我单击其中的一个,它连接到'api.php',并插入一个新的条目到我的数据库,但它不会返回到回到项目编号的页面。我不知道我是否应该回应这个,或者如果ajax在正确的地方。

Right now the code echos all of the item numbers from my database. If I click one of them it connects to the 'api.php' and inserts a new entry into my database but it does not return to the page that echos the item numbers. I am not sure if I should be echoing any of this or if the ajax is in the right place.

项目编号被使用并发送到'api.php'这将连接到我的数据库和更新我的数据库。

The item number is used and sent to 'api.php' which will connect to my database and update my database.

这是我一直在尝试整合ajax。让我知道你们的想法。
感谢

Here is how I have been trying to incorporate ajax. Let me know what you guys think. Thanks

<?php

mysql_connect("", "", "") or die(mysql_error());

mysql_select_db("") or die (mysql_error());


$sql = "SELECT id, item_number FROM products WHERE retailer LIKE ('%api%') ORDER BY id DESC";
$query = mysql_query($sql) or die (mysql_error());
while ($result = mysql_fetch_array($query)) {


$itemnumber = array($result['item_number']);

foreach ($itemnumber as $item_number) {

echo "<form method=\"post\" action=\"api.php\" name=\"ChangeSubmit\" id=\"ChangeSubmit\" >";
echo "<input type=\"submit\" name=\"item_number\" value=\"{$item_number}\" />";


echo "<script type=\"text/javascript\">
$('#ChangeSubmit').click(function() {
  $.ajax({
    url: \"api.php\",
    dataType: \"json\",
    type: \"POST\",
    data: {
      itemNumber: <?php print $item_number; ?>,
    },
    success: function (m) {
        console.log(m);
    },
    error: function (e) {
      console.log(\"Something went wrong ...: \"+e.message);
    },
  }); /* end ajax*/
  e.preventDefault();
});
</script>" ;



}


}

?>

这篇关于使用foreach循环将$ var发送到另一个.php页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆