Link to home
Start Free TrialLog in
Avatar of kbios
kbios

asked on

How do I launch 2 separate php programs from within 1 html program?

I need some design/program input. I have the need to run 2 php programs from within 1 html program. What is the easiest best way to do this?

Here is the design concept:

I need to update two tables in a mysql database. Table A is a header table and Table B is a detail table. I will create a unique key field within the html via javascript that will be passed to the php header program along with a few header fields. php program 1 will update the mysql database. Once the php header program is done I want to launch the second php program that will pass the same unique key field along with the additional item fields. I feel that I need to launch these php program from within html because my detail table will be getting its data from client-side local storage. Within html/javascript I will be setting up a loop cycling through my client-side local storage and launching the second php program for each item found.

Any ideas or suggestions would be appreciated.
Avatar of themrrobert
themrrobert
Flag of United States of America image

Here is a suggestion:

Use 1 php script and send all of the data to it, then run 2 different mysql queries inside this script.

You can use ajax if you need to dynamically do this, but i don't see why you would need 2 separate php scripts considering both of your examples use the same unique data id (meaning the same data, so why handle separate?  - rhetorical )
I'm with themrrobert.  I don't understand why you need 2 scripts.  A more complicated php script should be able to handle this.  

AJAX might be a good idea especially if you don't wan the user to think the program is stuck.  AJAX can also show there's still activity.
Avatar of Dean OBrien
Or you could post to an invisible IFRAME on the page.
This sounds potentially problematic to me: I will create a unique key field within the html via javascript that will be passed to the php header program ...

My concern is the way client-server systems work and the way HTTP, PHP, HTML and JavaScript interact.  HTTP is a client-server architecture.  A client sends requests and a server responds to requests.  PHP is a server-side scripting language that is used to generate HTML; the HTML subsequently gets sent to the client, so on any page request the PHP has completed its work before the client sees the HTML.  JavaScript is part of the HTML output generated by the server-side script.  The first thing JavaScript can do is an "onLoad" action.  By the time onLoad occurs, the server has completed its work.

There are ways to make it look like your script gets around this architecture using AJAX, for example.  But that may be an unnecessarily complicated addition to the mix.  It also introduces security concerns.

You might consider using HTML forms to submit a request to the PHP server-side script.  I would recommend a design using one script, not two.
http://php.net/manual/en/tutorial.forms.php

HTH, ~Ray
kbios, I stand by my post but I would like to state that Ray's post is excellent and I suggest you carefully read what he wrote and attempt to do what he suggested.  (I learned something too.)
Avatar of kbios
kbios

ASKER

Thanks for the suggestions. They are ALL appreciated. The consensus thus far seems to be no need for 2 php scripts. OK, I will drop the 2 script approach.

I thought about passing all of the data through 1 php and then executing 2 mysql calls. If I use this scenario I would have something like:
 
<a href=phpscript.php?hdrval1=val1&hdrval2=val2&hdrval3=val3&itemval1=ival1&itemval2=ival2.........>

I can do something like this but I have a few questions using this design:
 
1) I may hundreds of itemval entries for the href tag; is there a size/length limitation?
2) The php program will not know how many lines are coming at it; so do I include a counter variable in the href and then in the php program built my $GET?
3) What I mean here is that I am used to having the $GET in php script matching the fields that were sent to it. Since I don't know how many lines are coming do I create a loop for my counter variable and then build the $GET statement?

I have only previously used php/mysql to pass small amounts of data. I wasn't sure if trying to pass a large amount of data was good design.

Thanks in advance for your follow-up.
I'm not confident enough to answer your questions directly but I think I get the gist and will instead "answer" with a question.

Does the program on the client end know when it's done (has no more to send)?  If so, it could send a "sentinel" message to say "all done."  I don't think you'll need a counter at the server.  (However, if the client side program knows, before it starts its loop, how many records it will send, then it could start by sending a record count.  Still, I'd go with the sentinel approach but that's opinion.)
Avatar of kbios

ASKER

Allow me to add some programtic detail.

On the client-side I am storing data in localStorage variables. For example:

localStorage.user
localStorage.email_address
localStorage.trxCtr
localStorage.item1
...
localStorage.itemn

The ls variables user and email_address will be INSERTed INTO a mysql header table. NOTE: I will create a unique KEY that will also be inserted into the header table record.

The ls.trxCtr variable is the number of item records that I have. trxCtr will not be inserted into the db but I was keeping it so I could use it for a maximum loop counter. I then will INSERT INTO the mysql detail table the same unique KEY (for referencing) along with item1. And repeat this INSERT process for each item that I have in localStorage.

Again, I can create one GIGANTIC href tag that contains the header info and the detail info; but I'm concerned that the URL length will be too large and I think that's not necessarily a good design approach. I've looked into AJAX but I think it's a little more than what I need for this application.

I basically want to launch 1 mysql call to INSERT the record into the header table. And launch 'n' mysql calls to INSERT the item records into the detail table; where 'n' is the number of items in my localStorage.

Again. much appreciation for all help offered.
ASKER CERTIFIED SOLUTION
Avatar of themrrobert
themrrobert
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
SOLUTION
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

trxCtr would be the number of ROWS (ie. the number of item records I would have in the item detail table).

Many different pages create the content. I end up localStorage variables and values as outlined in my last post.

I'm still going to defer on the AJAX for now. I also am going to stick with HTTP and not go with ftp. I'm sure both options are viable but I would like to continue using the HTTP approach.

I would like to persue the $_POST['detailarray']. Do I create this array in the HTML that calls the php? Can you give me some sample(s) as to how I create the detailarray? Is it passed in the href tag?
$_GET goes in the HREF tag

http://www.w3schools.com/php/php_forms.asp
http://www.w3schools.com/php/php_get.asp
http://www.w3schools.com/php/php_post.asp

Much of the w3schools site is useful.  My professor used it in conjunction with the several textbooks.
Avatar of kbios

ASKER

My data is not being stored in forms so the forms solution is not applicable. I also know how to use get and post while sending individual variables. How do I create the array (item) on the client-side and then pass it to the php?

For example:

On the client-side HTML I'm envisioning creating a loop for my localStorage items and then make an assignment like:
item[0]=localStorage.item1
item[1]=localStorage.item2
item[2]=localStorage.item3 ..........

Then I would ....<a href=load.php?hdrvar1&hdrvar2&hdvar3&   *** how do I reference the array?? ***
     
Avatar of kbios

ASKER

I've decided to abandon the 2 table approach. I am just going to include the header info in the detail table. This way I can simply deal with 1 php program. This is bad db design, lack of normalization and db bloat will occur. Fortunately app is small. With larger app I may look into AJAX. The parameter option didn't seem to be much better than simply sending values in the url.

Thanks for the effort. I appreciate the help.