0

My 1st problem is I'm calling a function from javascript inside php(is my syntax correct, or is it possible to call that function),my 2nd problem is my query in mysql(the scenario is the value is incrementing, and there are values that are currently null )? I appreciate all the help that you could give, Thank you

<?php 
echo "<input type='hidden' name='totalElements' value='".$counter."' />";
echo '<input type="button" value="Submit" onclick="javascript:document.getElementById("gtdb")/>';
echo('</form>'); 
?>

<script>
function gtdb()
{

$dbhandle = mysql_connect('localhost', 'root', '123456')
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

mysql_select_db("website_comment",$dbhandle)
or die("Could not select examples");

mysql_query("INSERT INTO comments(id, tag_name, comment,url_id) 
VALUES ('',['$cke".$counter."'],'','','')")
or die("Could not select examples");  
}
</script>
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Mr. Fox
  • 328
  • 1
  • 7
  • 27

3 Answers3

2

PHP is executed on the server. JavaScript is executed on the client. You can't call a PHP function directly from JavaScript, typically we use AJAX to do this.

Call php function from javascript

I don't understand your second question.

Community
  • 1
  • 1
Halcyon
  • 57,230
  • 10
  • 89
  • 128
0

No, it's not possible. You are writing PHP code inside a client-side script...

Function gtdb() on the client should call (via AJAX, for example), a server-side script which implements your database access logic...

MarcoS
  • 17,323
  • 24
  • 96
  • 174
0

JavaScript goes in <script></script> tags and php belongs in <?php ?> tags. Your syntax is all mixed up.

Which IDE are you using? There should have been red squiggly lines everywhere.

Also onclick="javascript:document.getElementById("gtdb") is not doing anything at the moment, you can't call a javascript function like this and you certainly can't call php functons within javascript.

To be honest without being rude you should seriously consider going back and start on beginner tutorials.

J2D8T
  • 827
  • 11
  • 24