Using foreach loop to send $var's to another .php page?

775

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.

The API script is searching by item numbers, then updating the price in the same page.

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

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.

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!

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.

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.

Sorry for the long post! Just trying to be clear...

Here's what my code looks like:

$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']);

//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. */
}
}

?>

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!

135

Answer

Solution:

use jQuery like this:

<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>
378

Answer

Solution:

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.

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

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>" ;



}


}

?>

People are also looking for solutions to the problem: php - how to get specific parts in a page using preg_match_all

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.