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

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..
0
hhbargi
Asked:
hhbargi
  • 5
  • 2
1 Solution
 
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
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.

 
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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