Integrate PHP code in Javascript

Hi experts,

I'm trying to do my php code inside javascript but couldn't figure out how:

<?php
  include ('hbargi_fns.php');
  // The shopping cart needs sessions, so start one
 session_start();
 ?>
<html>
    <body>
<?php
do_html_header("Analyze Customer Purchase");
echo "This function analyzes the customers purchases in Aladen's website which checks the following:</br>";
echo "<ul><li>A customer profile that used more than three different credit cards within one day.</li>";
echo"<li>A credit card number that used in more than one profile.</li>";
echo "<li>A crdedit card that used  more than three phone numbers in one day.</li>";
echo "<li> A credit card with more than one card holder name.</li></ul>";
?>
<button type="button" onclick="displayData()">Display Data</button>

<script type='text/javascript'>
function displayData()
{
<?php
echo "<table>";

  $conn = db_connect();
  $query = "select customerid, payment_first_name,payment_last_name, card_num from orders";
  $result= @$conn->query($query);
  if (!$result) {
     return false;
   }
   $num = @$result->num_rows;
   $i=0;
   
 // Get the first list  
echo "<tr bgcolor=#AAAAAA>A customer profile that used more than three different credit cards within one day.</tr>";
while ($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "Customer ID: " .$row["customerid"]. "</br>";
    echo "Customer first name: " .$row["payment_first_name"]. "</br>";
    echo "Customer last name: " .$row["payment_last_name"]. "</br>";
    echo "Credit CArd Number: " .$row["card_num"]. "</br>";
    echo "</tr>";
}
echo"</table>";
?>
}
</script>
    </body>
</html>
<?php
  do_html_footer();
  
?>

Open in new window


Thanks..
hhbargiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

haloexpertsexchangeCommented:
what you need is an ajax call to php page that does what you want.
0
hhbargiAuthor Commented:
So how can I do it in my code..
0
haloexpertsexchangeCommented:
ok I going to use jquery with the ajax since that is what I know the best.
<button type="button" id="displayData">Display Data</button><-adding an id to make things a little easier
This is on your main page somewhere
$(document).ready(function() {
$('displayData').click(function(){
$.get('yourFileName',{comma,seperate,get, values,'formated':'like this'},function(data){
$('body').append(data);
});
});
});

Open in new window

On your seperate php file you will echo out anything that you want to show up on your main page.
Don't forget to set up the database connections here as well as well as session_start if you need it.
<?php
echo "<table>";

  $conn = db_connect();
  $query = "select customerid, payment_first_name,payment_last_name, card_num from orders";
  $result= @$conn->query($query);
  if (!$result) {
     return false;
   }
   $num = @$result->num_rows;
   $i=0;
   
 // Get the first list  
echo "<tr bgcolor=#AAAAAA>A customer profile that used more than three different credit cards within one day.</tr>";
while ($row = $result->fetch_assoc()) {
    echo "<tr>";
    echo "Customer ID: " .$row["customerid"]. "</br>";
    echo "Customer first name: " .$row["payment_first_name"]. "</br>";
    echo "Customer last name: " .$row["payment_last_name"]. "</br>";
    echo "Credit CArd Number: " .$row["card_num"]. "</br>";
    echo "</tr>";
}
echo"</table>";
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Dave BaldwinFixer of ProblemsCommented:
I'm afraid that won't ever work.  Much of what you have in the PHP sections is plain HTML so it doesn't need to be PHP code and none of what you have in the javascript tags is javascript.  I've fixed the formatting and some other things below.

I have to tell you that you saving credit card numbers in your database will make you the target of every thief and hacker in the world.  In the US, there are very stringent security requirements for storing credit card information and fines for allowing it to be stolen.
<?php
include ('hbargi_fns.php');
// The shopping cart needs sessions, so start one
session_start();
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <body>
<?php
do_html_header("Analyze Customer Purchase");
?>
This function analyzes the customers purchases in Aladen's website which checks the following:</br>
<ul><li>A customer profile that used more than three different credit cards within one day.</li>
<li>A credit card number that used in more than one profile.</li>
<li>A crdedit card that used  more than three phone numbers in one day.</li>
<li> A credit card with more than one card holder name.</li></ul>

<button type="button" onclick="displayData()">Display Data ???</button>

<?php
echo "<table>";

  $conn = db_connect();
  $query = "select customerid, payment_first_name,payment_last_name, card_num from orders";
  $result= $conn->query($query);
  if (!$result) {
     return false;
   }
   $num = $result->num_rows;
   $i=0;
   
 // Get the first list  
echo "<tr bgcolor=#AAAAAA><td>A customer profile that used more than three different credit cards within one day.</td></tr>";
while ($row = $result->fetch_assoc()) {
    echo "<tr><td>";
    echo "Customer ID: " .$row["customerid"]. "</br>";
    echo "Customer first name: " .$row["payment_first_name"]. "</br>";
    echo "Customer last name: " .$row["payment_last_name"]. "</br>";
    echo "Credit Card Number: " .$row["card_num"]. "</br>";
    echo "</td></tr>";
}
echo"</table>";
?>
<?php
  do_html_footer();
?>
</body>
</html>

Open in new window

0
haloexpertsexchangeCommented:
about the jquery functions that I used
http://api.jquery.com/jQuery.get/<-ajax get there is a post too
http://api.jquery.com/click/<-click function
0
hhbargiAuthor Commented:
haloexpertsexchange,

For the first code, do I have to put it in html file.
0
haloexpertsexchangeCommented:
you can, or you can link it like you would do any other js file.
One thing I forgot to mention is that you need the jquery file to be included before you include the function that showed you for this to work.
You can get one here http://code.google.com/p/jqueryjs/downloads/list
0
haloexpertsexchangeCommented:
You can also check here for the jquery file http://docs.jquery.com/Downloading_jQuery
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.