Solved

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

Posted on 2011-09-24
14
276 Views
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.
0
Comment
Question by:kbios
  • 5
  • 5
  • 2
  • +2
14 Comments
 
LVL 13

Expert Comment

by:themrrobert
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 )
0
 
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.
0
 
LVL 12

Expert Comment

by:Dean OBrien
ID: 36595023
Or you could post to an invisible IFRAME on the page.
0
 
LVL 108

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.
http://php.net/manual/en/tutorial.forms.php

HTH, ~Ray
0
 
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.)
0
 

Author Comment

by:kbios
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.
0
 
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.)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:kbios
ID: 36595760
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.
0
 
LVL 13

Accepted Solution

by:
themrrobert earned 250 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,
http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
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:
<?php
//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

0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 250 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.
0
 

Author Comment

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

Expert Comment

by:Hugh McCurdy
ID: 36596603
$_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.
0
 

Author Comment

by:kbios
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[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?? ***
     
0
 

Author Comment

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

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

9 Experts available now in Live!

Get 1:1 Help Now