Solved

Include Java in a PHP application

Posted on 2009-04-04
22
1,250 Views
Last Modified: 2013-12-14
With the help of EEs, I just finished an HTML that works excellently picking dates.  Unfortutanetly, I just discover that the routins of the date-picking is not for a HTML file, its for a PHP file.  I can't re-write for HTM and add the date-picking, but I would like to include in the PHP application the newly scripts.

I'm trying various forms with no success.  Here is the code in HTML that I woul like to include in PHP:
(can it be done)

<head>
      <title>jQuery UI Datepicker - Format date</title>
      <link type="text/css" href="ui.all.css" rel="stylesheet" />
      <script type="text/javascript" src="jquery-1.3.2.js"></script>
      <script type="text/javascript" src="ui.core.js"></script>
      <script type="text/javascript" src="ui.datepicker.js"></script>
      <link type="text/css" href="demos.css" rel="stylesheet" />
      <script type="text/javascript">
      $(function() {
        $("#From").datepicker({ dateFormat: 'yy-mm-dd' });
        $("#To").datepicker({ dateFormat: 'yy-mm-dd' });
      });
      </script>
</head>
<body>

<div class="demo">

<p>From: <input type="text" id="From"></p>
<p>To: <input type="text" id="To"></p>
0
Comment
Question by:rayluvs
  • 13
  • 9
22 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
Yes, you can include the html and javascript from PHP. It is a very common thing to do. I would do something like this:

Save the code above up to and including <body> as "datepicker_head.html". Write your PHP file like this:
<hmtl>

<?php include('datepicker_head.php');?>

<div class="demo">
 

<p>From: <input type="text" id="From"></p>

<p>To: <input type="text" id="To"></p>

Open in new window

0
 

Author Comment

by:rayluvs
Comment Utility
I want to include or import HTML and JAVA to PHP, not from it.  The code provided above is HTML and I want my PHP application to run those instructions.
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
How will this PHP application be used? Is it to be run in a browser?

A php file can contain "normal" html code, including <script> tags and javascript. If the file extension is .php the web server will parse anything between <?php ?> tags as PHP code, anything outside those tags will be sent directly to the browser as HTML.

Java is not involved here. Java and Javascript are two separate programming languages.
0
 

Author Comment

by:rayluvs
Comment Utility
So the script I placed at the initial at the question can't be included in a PHP application?

I have a PHP program where it asks for dates.  I found a Datepicker and tested and run it but when I finished it, I remember that the dates field is in a PHP apps not in a HTML apps.  The script at top is HTML.  That HTML calls *.js... (see above)

Any suggestions?
0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
>> So the script I placed at the initial at the question can't be included in a PHP application?

That is not a script, it is a html snippet. And yes, it CAN be included into a PHP script, using the include() function like I showed you in my first reply.

You did not answer my questions: How will this PHP application be used? Is it to be run in a browser?
0
 

Author Comment

by:rayluvs
Comment Utility
By your first reply I assume that you think I want to have a HTML incluide a PHP; Its the other way around.  I have a PHP apps that I want to use the HTML statements (the one I placed at my question).  In other words, how can I have those instruction within my PHP.

Ok.  The PHP application is currently being used thru browser and it asks the user for dates.  The HTML at top is a apps I developed with the help of the EE to pick dates.  I wanted to modify my PHP in order for the user to pick dates.  But when I finished with EE, I noticed the application is under HTML and the apps I am using is under PHP.

Hope I made a clearer statement of what I want.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
Comment Utility
The PHP script outputs HTML. The browser does not understand PHP, it only understands HTML and javascript. The web server executes the PHP code, and the output of the PHP script is "normal" HTML.

When you have a PHP file (a file with extenstion .php) it will contain PHP start and stopp tags "<?php" and "?>". Anything between these tags are evaluated as PHP code and executed on the server. Anything OUTSIDE of those tags, will be sent directly to the browser. Example:

<?php
  echo 'Hello from PHP!';
?>
<b>Hello from HTML</b>
<?php
  echo 'back in PHP';
?>

You can "jump" between HTML and PHP by using the <?php ?> tags. This is a typical usage:

<a href="<?php echo $url; ?>">Click here</a>

The PHP script can start in HTML mode, and "jump" in and out of PHP mode how many times you like. You can also start in PHP mode and output all the HTML from the PHP code:

<?php
 

include('configuration.php');

echo '<html><head><title>';

echo $DocumentTitle;

echo '</title>'; 
 

?>

Open in new window

0
 

Author Comment

by:rayluvs
Comment Utility
Ok, since I need this HTML code withing my PHP file, I can just write the PHP section in my HTML and that way I can have my PHP features and also my HTML DatePicker all in  one file (only keep the PHP code within "<?php?" and "?>")?

If that's the case, and I can successfully embed in my HTML datepicker code, the PHP portions, how to I transfer the Date Fields values to the PHP section?
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
Comment Utility
Yes, just remember to save the file with .php extension, not .html.

Make the date fields part of a form, attach the form to a specific php file to handle the submit, and set a name on the input fields:

<form action="DateInput.php" method="post">
<p>From: <input type="text" name="From" id="From"></p>
<p>To: <input type="text" name="To" id="To"></p>
<input type="submit" value="Submit" />
</form>

In the DateInput.php handling script, you can read the dates like this:

$From = strtotime($_POST['From']);
$To = strtotime($_POST['To']);
0
 

Author Comment

by:rayluvs
Comment Utility
Prior working with the HTML file, with DatePicker, my solutions was based on 2 PHP files.

The First File (InputDate.php):

Ask the ClientID (load from MySql table the IDs), and asks for the "From" "To" dates. In this PHP file I have the   form action="ShowResults.php" method="post"> statement

On the second file (ShowResults.php):

I has the "$From = strtotime($_POST['From'])" lines (for both dates and ClientID).

At end, my application is already like that.   So in my first file "InputDate.php", I can include the actual statements of the HTML but not include them within "<?php?" and "?>")?



0
 
LVL 39

Expert Comment

by:Roger Baklund
Comment Utility
That should work, yes.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:rayluvs
Comment Utility
sounds good...I'll try!
0
 

Author Comment

by:rayluvs
Comment Utility
It didn't work.

First I change the extention to PHP in my HTML file (which I have datepicker see HTML lines with my question) and ranned and it worked perfectly.

Then I copied my PHP contents and placed all within "<?php?" and "?>" (to open up MySql and ask uder which client to shoose).

So now the file should look ike this:

<!doctype html>
html lines...
html lines... (lines for running for showing title & info
html lines...
html lines...

<?php
php lines...
php lines...
php lines... (lines for accessing MySQL and showing drop-down selections: select client)
php lines...
?>

html lines...
html lines... (lines with DatePicker fields and user select "From" and "To" dates to use)
html lines...
html lines...

<?php
php lines...
php lines...
php lines... (lines for running the ShowServiecesLog.php file - actual report apps)
                     <form action=ShowServicesLog.php method=post>
php lines...
?>

</body>
</html>
------ this is the end.

What am I missing?

I want to use my PHP file and also the HTML file
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
Comment Utility
The description of your document seems ok, it should produce a valid document.

The "lines for running the ShowServiecesLog.php file", does it produce html output? Does it redirect?

In many cases we need to run php code before any output is sent to the server. Redirecting with header(Location) is one such case, starting a session and setting cookies is other cases. That is why php files often start with a <?php tag. This is a typical pattern:

<?php
php lines
?><!doctype
<html>
<head>
head lines
<?php
php lines
?>
</head>
<body>
<?php
php lines
?>
html lines
<?php
php lines
?>
</body>
</html>

The number of times you use <?php ?> within the body part of the html output varies a lot. Sometimes it is mostly html with many small <?php ?> tags, other times it is one or a few "large" <?php ?> tags, echoing the html.

In what way is it not working? Do you get an error message, blank page, or what? Can you post your code?
0
 

Author Comment

by:rayluvs
Comment Utility
Question:

1. Starting my PHP with HTEML tags ok? (as I showed previouls)?

2. Do you recommend to start as HTML or PHP?  And if HTML, what should be the first line (and vice-versa)?

2. The error message I get is "Parse error: parse error, expecting `','' or `';'' in
     /home/www/msys/a/zSAMPLE.DatePicker.php on line 40"

The "lines for running the ShowServiecesLog.php file", does'nt produce html output since I get the parse error (but in my original; PHP, it does display results perfectly)





0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
Comment Utility
1. Yes, unless you are redirecting, starting a session or setting a cookie later in the script.

2. Depends on your needs, one is not better than the other. In any case, the OUTPUT should start with <!DOCTYPE or <html> (if you don't use doctype).

2. Parse error means the php code is malformed. The php parser did not recognize it as php. The error is in line 40 in file zSAMPLE.DatePicker.php.
0
 

Author Comment

by:rayluvs
Comment Utility
Done & it works perfectly!

This new PHP file you help me combine inputs data from user and then transfers them to a secin PHP file (""ShowServiecesLog.php"") thats displays the results.  I can't seem to send the Dates values to the "ShowServiecesLog.php" file (every other variable does goes to the file except the dates).

Before combining the DatePicker and PHP file, the Date input was like this:

<input type='text' name='From'> - From Date (YYYY-MM-DD)<br />
<input type='text' name='To'> - To Date (YYYY-MM-DD)<br />

After combining the HTML and PHP, the Date input is like this:

$("#From").datepicker({ dateFormat: 'yy-mm-dd' });
$("#To").datepicker({ dateFormat: 'yy-mm-dd' });

I have no errors now, it works perfectly, it just the Date values entered by the user is not transfer to the "ShowServiecesLog.php".

Am I missing something?
0
 

Author Comment

by:rayluvs
Comment Utility
I just saw the difference: the "input type" lines.

Prior combining, I had:

   <input type='text' name='DateFrom'><br />
   <input type='text' name='DateTo'><br />

With DatePicker, its:

<p><input type="text" id="DateFrom"></p>
<p><input type="text" id="DateTo"></p>

Can this be it?

0
 

Author Comment

by:rayluvs
Comment Utility
I just tried and change the "ID=" to "NAME=" and DatePicker didn't work.

Any suggestion?
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 500 total points
Comment Utility
Use both name and id:

<p><input type="text" name="DateFrom" id="DateFrom"></p>
<p><input type="text" name="DateTo" id="DateTo"></p>
0
 

Author Comment

by:rayluvs
Comment Utility
U R THE MASTER!!!!!  THANX!!!

If I could give u mire point I would!!!
0
 

Author Closing Comment

by:rayluvs
Comment Utility
EXCELLENT !!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wordpress Query 1 30
PHP Variable into a number 3 33
PHP Sum Column in Table 3 26
php image upload 3 24
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 …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

10 Experts available now in Live!

Get 1:1 Help Now