Link to home
Start Free TrialLog in
Avatar of kbios
kbios

asked on

How do I call a php script from within a javascript loop?

I have 2 distinct sets of localStorage data that I need INSERTed INTO 2 tables in a mysql db. I'm using hidden fields in a form as 'placeholders' for the localStorage data. The hidden fields are then accessed from the php programs via $_POST and passed to the mysql_query. This process works great and I want to continue using this approach.

The first set of variables will be loaded into a 'header' table. I'm good with the header table. The second set of variables need to be loaded into a 'detail' table.  The localStorage variables for the detail table are as follows:

localStorage.trxCtr
localStorage.item1
localStorage.item2
localStorage.itemn

The trxCtr is the maximum number of items in localStorage. Here is the approach with javascript thus far:

var val=parseInt(localStorage.trxCtr);

for(var i = 1; i <= val; i++)
     {
       if ( localStorage["item" + i] != "X"
         {

         **** call load.php passing the item number in the href tag ****
         **** the php will receive the item number an run mysql     ****
           
          }
      };

Please help with the php call syntax that is called from within a javascript. Thanks in advance for any assistance offered.
Avatar of plusone3055
plusone3055
Flag of United States of America image

Avatar of kbios
kbios

ASKER

Thanks for the link. I found it earlier but I wasn't able to follow all of the threads. Which part of the link did you think was most applicable?


ive used this myself

taken from
http://forums.devarticles.com/javascript-development-22/calling-php-functions-with-javascript-3471.html

// .... here some php lins... variables o some functions....

//... maybe some post variables an some new values.... proccessed over post variables....


<script type="text/javascript">
function test(){
document.getElementById("php_code").innerHTML="<?php for($i=0; $i<10; $i++) echo $i; //maybe a function ?>";
}
</script>

<a href="#" style="display:block; color:#000033; font-family:Tahoma; font-size:12px;" onclick="test(); return false;"> test </a>
<span id="php_code"> </span>

Avatar of kbios

ASKER

It 'appears' to me that this code is a javascript from within a php script. Is that correct? If so, then I think I have a problem. My localStorage variables are on the client-side once I step over to the server how would I access my localStorage or hidden fields? Please help 'connect the dots' between my code snipet and yours.
Avatar of kbios

ASKER

OK since response has been very limited let me try a different approach. As previously mentioned I have the following:

localStorage.trxCtr
localStorage.item1
localStorage.item2
localStorage.itemn

I can easily pass the data to the php script via a hidden field. The issue is that I won't know how many items (e.g. hidden fields) there are. Can anyone provide some code snipets or suggestions as to how I can dynamically create html hidden fields for each of my localStorage variables?

HTML javascript:

var val=parseInt(localStorage.trxCtr);

for(var i = 1; i <= val; i++)
     {
       if ( localStorage["item" + i] != "X"
         {

         **** create dynamic hidden fields here ****          
          }
      };


The only problem I forsee here is that is there a MAXIMUM number of hidden fields a form can have AND/OR a maximum number of $_POST's a php script can have?


greetings kbios, , I do not know, or have ever heard of being able to get anything from PHP into javascript, except with a form POST (or GET) send to PHP page  - - - OR by using browser AJAX send-receive. From what you have said, I would think that you will need to use AJAX to send the item number to a PHP page and then get back the MySQL result in the AJAX return. If you have never used AJAX, this may not be easy for you.

Ask questions if you need more info.
Avatar of kbios

ASKER

Thanks for the comment. I would prefer to NOT use ajax at this time. I believe there has to be an easier way to do what I want to do. I don't want to go from PHP into javascript. I want to go from javascript into PHP.

I have a list of items stored in localStorage from a 'shopping cart' process. Think of my localStorage as follows:

item1="shaving cream"
item2="razor blades"
item3="X"
item4="soap"

Within an HTML page via javascript I'd like to loop through my localStorage for each item; and for each item simply pass the item to a php program, perhaps via the href tag. The sole purpose of the php program is to INSERT INTO a table the item that was passed. The php script is done and the HTML/javascript loop repeats until the end of the for each.

Does that make sense?
The problem with your request is that javascript is client side and php is server side and client side does not talk to the server side with out either a page form submit or without some kind of ajax call.
If you use something like jquery, ajax does not have to be complicated at all.
Avatar of kbios

ASKER

I get the fact that the javascript is client-side and the php is server-side. As you point out and I have done; I can setup a client-side javascript that creates a call to a php script something like a href= dataload.php?item=item1 and based on a submit/click(that works fine.) What I'm trying to accomplish is to not have to click/submit for every item that needs 'loaded'.

Can you provide any jquery/ajax snipets that may help?
ASKER CERTIFIED SOLUTION
Avatar of haloexpertsexchange
haloexpertsexchange
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbios

ASKER

Thanks. I think I can use what you provided as a place to begin.

Thanks to all who contributed.