Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A simple problem where a javascript function is being called by button through onclick..
And that js function calls a php function..

My actual motto is that a function should be called when a html button is clicked..so that the function should be able to block a div.


Quick reply will help me alotttt

What I have tried:

<?php
function f1()
{
 echo "f1 called";
}
function f2()
{
 echo "f2 called";
}
?>

<html>
<head><title>Trail</title></head>
<body background="yellow" onload="start()">
<div style="display:none" id="1">
<h1>SYSTEM</h1>
</div>
<div style="display:none" id="2">
<p>Welcome</p>
</div>
<div style="display:none" id="3">
<p>Ramaa</p>
</div>
<input type="button" value="welcome" onclick="one()">
<input type="button" value="welcome" onclick="two()">
<script>
function start()
{
 document.getElementById("1").style.display="block";
}
function one()
{
 document.getElementById("2").style.display="block";
 document.getElementById("3").style.display="none";
 <?php
  f1();
 ?>
}
function two()
{
 document.getElementById("2").style.display="none"
  document.getElementById("3").style.display="block";
 <?php
  f2();
 ?>
}
</script>
</html>





//problem

Before inserting php code it worked really great i.e, onload is working and onclicking buttons corresponding functions are called.But when I put php code in it..onload is not working and onclick is also not working.It will be very helpful if someone let me know the mistake in it.
Posted
Updated 4-Apr-17 20:25pm
v3
Comments
Peter_in_2780 5-Apr-17 0:29am    
Think about it. PHP runs on the server, while the page is being built for transmission to the client.

1 solution

a very simple test for you, hope it will get you going

index.php
<HTML>
  <HEAD>
    <SCRIPT SRC="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></SCRIPT>
    <SCRIPT>
      function call_function (value) {
        var a = document.forms[0].a.value;
        var b = document.forms[0].b.value;
        $.get({
            url: 'func.php?a=' + a + '&b=' + b+"&action="+value, 
            success: function(result){
                $('#here').html(result);
              }
        }
      );
      return false;
      }
    </SCRIPT>
  </HEAD>
  <BODY>
      <FORM onsubmit='return false'>
      <INPUT NAME=a> <br>
      <INPUT NAME=b> <br>
      <INPUT TYPE=SUBMIT VALUE=add NAME=action  onclick="call_function(this.value)">
       
       
       
       
       
      <INPUT TYPE=SUBMIT VALUE=multiply NAME=action onclick="call_function(this.value)">
      <FORM>
    <div id='here'>
    </div>
  </BODY>
</HTML>



func.php
<?php
  function add($a, $b) {
    return $a+$b;
  }
  function multiply($a, $b) {
    return $a*$b;
  }
  if($_GET['action'] == "add") {
    echo add($_GET['a'], $_GET['b']);
    
  } else if($_GET['action'] == "multiply") {
    echo multiply($_GET['a'], $_GET['b']);
  }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900