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

Posted on 2011-09-24
Medium Priority
Last Modified: 2012-05-12
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.
Question by:kbios
  • 5
  • 5
  • 2
  • +2
LVL 13

Expert Comment

ID: 36594240
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 )
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36594290
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.
LVL 12

Expert Comment

by:Dean OBrien
ID: 36595023
Or you could post to an invisible IFRAME on the page.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 111

Expert Comment

by:Ray Paseur
ID: 36595175
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.

HTH, ~Ray
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36595183
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.)

Author Comment

ID: 36595428
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.
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36595678
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.)

Author Comment

ID: 36595760
Allow me to add some programtic detail.

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


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.
LVL 13

Accepted Solution

themrrobert earned 1000 total points
ID: 36596078
Show me your page that generates the content.

does txtCtr contain the counter of how many ROWS or is the number of ADDITIONAL (details) fields?

either way you can remove it using a foreach loop.

lets say the header table contains a static number of fields, this is easy enough and you shouldnt have any trouble.

for the 2nd "detail table" i imagine that you add additional rows for EACH additional piece of detail info. likely the table takes this form:

TABLE `detail`
-> TEXT `uniqueID`, TEXT `content`

You might have an additional tag in there, you can figure it out.

You should probably use ajax, I know its a bit intimidating at first, but its really very simple,
this is probably your best reference to quickly learn how to do it.

Google "Send php POST data ajax" (no quotes) to get more tutorials.

If you store these additional details into an array, you can passthis array to php and then in php generate multiple insert statements like this:
//add the code for the first table, i'm sure you've got it

$detail_array = $_POST['detailarray'];

foreach($detail_array as $value) {
//$uid is the unique id you already generated earlier
  $sql = "INSERT INTO `detail` VALUES ('$uid','$value');";
  mysql_query($sql); //or PEAR's DB::Query

you can modify it to fit your needs

Open in new window

LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 1000 total points
ID: 36596226
I think Robert's approach is far better than GET (put the info in the URL).  However, you could solve the "URL too long" problem by sending part of the data in one request then another part of the data in the second request, etc.

I want to take a step back and ask sending data via HTTP requests the best approach available to you?  FTP is out of the question?  I have C language software that needs to get data from one of our customer's computers to our server.  I wrote a program that gets the data into a nice format (XML) and then calls an FTP program to upload.  (SSH also works but there's nothing secret about the data about to be published publicly.)  I only mention this  just in case you do what I do sometimes, forget the overall mission.

Author Comment

ID: 36596571
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?
LVL 13

Expert Comment

by:Hugh McCurdy
ID: 36596603
$_GET goes in the HREF tag


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

Author Comment

ID: 36596635
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[2]=localStorage.item3 ..........

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

Author Comment

ID: 36596784
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.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month16 days, 7 hours left to enroll

862 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