0

I have a HTML Table that is populated from a mySQL table as seen below:

$sql = "SELECT `ID`,`SaveName`, `CourseNumber`, `FormType`, `POSTString`, `DateModified` FROM `SavedFormsTable` WHERE 1";
$result = mysqli_query($con,$sql);

echo "<table border='1'>
<tr>
<th>Save File Name</th>
<th>Course Number</th>
<th>Date Modified</th>
<th>Load Old Form</th>
</tr>";

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['SaveName'] . "</td>";
  echo "<td>" . $row['CourseNumber'] . "</td>";
  echo "<td>" . $row['DateModified'] . "</td>";
  echo '<td><button type="button" onclick="LoadFormJS('.$row['ID'].');">Load Form!</button>';
  echo "</tr>";
}    
echo "</table>";

My problem is on the 4th line from the bottom. in the HTML <button>

I am trying to create a button IN THE HTML TABLE that when clicked will call a PHP function. But i have to echo the HTML in order to create the table from the mySQL database.

Is there a clever way of to do this???

enter image description here

The PHP function I need to call:

function LoadFormPHP($ID){

$con=mysqli_connect("","User636626","EasyPassword","pansyc5_SavedForms");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = sprintf("SELECT `POSTString` FROM `SavedFormsTable` WHERE `ID`=%d",$ID);
$result = mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
//mysql_close($con);
$_POST = unserialize(base64_decode($row["POSTString"]));

}

The JS function i tried (and failed) to call the PHP function from:

<script>
         function LoadFormJS(ID){
        alert(ID);
        <?php LoadFormPHP(ID) ?>
         }
      </script>
4
  • 2
    Unfortunately, you cannot call a PHP function after the page has loaded like this. Have you looked into using AJAX for this functionality? developer.mozilla.org/en-US/docs/AJAX/Getting_Started Commented Jul 22, 2014 at 18:01
  • You need to use ajax , can you post code of the php function you need to call ? Commented Jul 22, 2014 at 18:02
  • As stated you cannot call PHP after the page has loaded. I suggest taking a look at Ajax, in particular look at the Jquery ajax functions as, in my opinion, they are by far the easiest to use. Commented Jul 22, 2014 at 18:04
  • I added my failed attempt at using a javascript function to call a PHP function... Commented Jul 22, 2014 at 20:37

2 Answers 2

1

You cannot mix html (client side) and php (server side) together. For this kind of cases AJAX is a good tool. The onclick can contain a request to a javascript function that fires the request back to your server, and returns the desired HTML.

Sign up to request clarification or add additional context in comments.

Comments

1

You just have some syntax issues -

echo '<td><button type="button" onclick="LoadForm(' .$row['ID'] .')">Load Form!</button>';

What you could do to call the PHP function is to surround your button with a form using a hidden input to hold the value of $row['ID']. Clicking on the button would load the proper form at that point and you could ditch the inline JS in favor of a form action.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.