• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

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.
  • 5
  • 5
  • 2
  • +2
2 Solutions
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 )
Hugh McCurdyCommented:
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.
Dean OBrienCommented:
Or you could post to an invisible IFRAME on the page.
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Ray PaseurCommented:
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
Hugh McCurdyCommented:
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.)
kbiosAuthor Commented:
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.
Hugh McCurdyCommented:
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.)
kbiosAuthor Commented:
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.
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

Hugh McCurdyCommented:
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.
kbiosAuthor Commented:
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?
Hugh McCurdyCommented:
$_GET goes in the HREF tag


Much of the w3schools site is useful.  My professor used it in conjunction with the several textbooks.
kbiosAuthor Commented:
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?? ***
kbiosAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now