Solved

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

Posted on 2011-09-27
11
222 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:kbios
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 36709684
0
 

Author Comment

by:kbios
ID: 36709774
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?
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 36709843


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>

0
 

Author Comment

by:kbios
ID: 36709906
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.
0
 

Author Comment

by:kbios
ID: 36711292
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?


0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 33

Expert Comment

by:Slick812
ID: 36712556
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.
0
 

Author Comment

by:kbios
ID: 36712731
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?
0
 
LVL 13

Expert Comment

by:haloexpertsexchange
ID: 36712795
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.
0
 

Author Comment

by:kbios
ID: 36712854
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?
0
 
LVL 13

Accepted Solution

by:
haloexpertsexchange earned 500 total points
ID: 36712907
you need the jquery library for this to work, but normally you can interchange it with normal javascript freely.
You can use the $.get function from jquery. Different items of information will be coma seperated.
This is a really basic ajax call that uses get information.
http://api.jquery.com/jQuery.get/
$.get("dataload.php", { item: "item1"} , function(data){do something with the data you get back});

Open in new window

This jquery site has information on most jquery functions and how to use them.
0
 

Author Closing Comment

by:kbios
ID: 36713446
Thanks. I think I can use what you provided as a place to begin.

Thanks to all who contributed.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now