Jump to content

How to call javascript function in php


doforumda

Recommended Posts

hi

 

i created a button "Edit" and i make a function for that in javascript. when the user click on the button it should call that javascript function and go to the specified page. i did all this but it is not doing anything.

 

how can this problem be solved?

Link to comment
Share on other sites

Here is the code

 

<?php

$db_connect = mysql_connect("localhost","username","password");

mysql_select_db("database",$db_connect);

 

if(isset($function) && $function == "edit")

{

echo "Function is not set";

$sql_query = "select * from statusvalues where statVal_id = &mode";

print($sql_query);

}

 

 

if(isset($function) && $function == "add")

{

echo "This is the value - $regionNumber<br>";

$sql_query = "insert into statusvalues (firstname,lastname,region,ssn) values ('".$fname."','".$lname."','".$regionNumber."','".$ssn."')";

$exe_query = mysql_query($sql_query);

}

 

$sql_reg = "select * from region";

$sqlQuery = mysql_query($sql_reg);

 

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<script language="JavaScript">

 

function go_to_page(hlink)

{

window.location = "indexxx2.php?function=edit&mode="+hLink+"";

}

</script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<title>Untitled Document</title>

</head>

 

<body>

<form name="form1" method="post" action="indexxx.php?function=add">

  <p>First Name:

    <input type="text" name="fname" id="fname">

</p>

  <p>Last Name:

    <input type="text" name="lname" id="lname">

</p>

  <p>Select Region Number:

  <?php

    echo "<select name='regionNumber' id='regionNumber'>";

    while($record = mysql_fetch_array($sqlQuery))

{

    echo "<option value = '".$record['regionNumber']."'>".$record['regionNumber']."-".$record['regionName']."</option>";

    }

echo "</select>";

?>

 

  </p>

  <p>Enter SSN No Dashes:

    <input type="text" name="ssn" id="ssn">

  </p>

  <p>

    <input type="submit" name="button" id="button" value="OK">

  </p>

</form>

<?php

 

$selectQuery = "select * from statusvalues";// order by lastname ASC";

$resultName = mysql_query($selectQuery);// or die(mysql_error());

?>

<table width='400' border='1' cellspacing='0' cellpadding='0'>

  <tr>

    <td>ID</td>

    <td>First Name</td>

    <td>Last Name</td>

    <td>Region</td>

<td> </td>

  </tr>

<?php while($record = mysql_fetch_array($resultName)) { ?>

 

  <tr>

    <td><?php echo $record['statVal_id'] ?></td>

    <td><?php echo $record['firstname'] ?></td>

    <td><?php echo $record['lastname'] ?></td>

    <td><?php echo $record['region'] ?></td>

<td><input type = "button" name = "edit1" value = "Edit" onclick="javascript(go_to_page(<?php echo $record['statVal_id'] ?>));"/></td>

  </tr>

<?php } ?>

  </table>

</body>

</html>

 

Link to comment
Share on other sites

please use [ code ] [ /code ] tags.

<?php 
$db_connect = mysql_connect("localhost","username","password");
mysql_select_db("database",$db_connect);

if(isset($function) && $function == "edit")
{
   echo "Function is not set";
   $sql_query = "select * from statusvalues where statVal_id = &mode";
   print($sql_query);
}


if(isset($function) && $function == "add")
{
   echo "This is the value - $regionNumber<br>";
   $sql_query = "insert into statusvalues (firstname,lastname,region,ssn) values ('".$fname."','".$lname."','".$regionNumber."','".$ssn."')";
   $exe_query = mysql_query($sql_query);
}

$sql_reg = "select * from region";
$sqlQuery = mysql_query($sql_reg);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="JavaScript">

   function go_to_page(hlink)
   {
      window.location = "indexxx2.php?function=edit&mode="+hLink+"";
   }
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>

<body>
<form name="form1" method="post" action="indexxx.php?function=add">
  <p>First Name:
    <input type="text" name="fname" id="fname">
</p>
  <p>Last Name:
    <input type="text" name="lname" id="lname">
</p>
  <p>Select Region Number:
  <?php 
    echo "<select name='regionNumber' id='regionNumber'>";
     while($record = mysql_fetch_array($sqlQuery)) 
    {
       echo "<option value = '".$record['regionNumber']."'>".$record['regionNumber']."-".$record['regionName']."</option>";
     } 
    echo "</select>";
?>

  </p>
  <p>Enter SSN No Dashes:
    <input type="text" name="ssn" id="ssn">
  </p>
  <p>
    <input type="submit" name="button" id="button" value="OK">
  </p>
</form>
<?php

   $selectQuery = "select * from statusvalues";// order by lastname ASC";
   $resultName = mysql_query($selectQuery);// or die(mysql_error());
?>
<table width='400' border='1' cellspacing='0' cellpadding='0'>
  <tr>
    <td>ID</td>
    <td>First Name</td>
    <td>Last Name</td>
    <td>Region</td>
   <td> </td>
  </tr>
<?php while($record = mysql_fetch_array($resultName)) { ?>
  
  <tr>
    <td><?php echo $record['statVal_id'] ?></td>
       <td><?php echo $record['firstname'] ?></td>
       <td><?php echo $record['lastname'] ?></td>
       <td><?php echo $record['region'] ?></td>
      <td><input type = "button" name = "edit1" value = "Edit" onclick="javascript(go_to_page(<?php echo $record['statVal_id'] ?>));"/></td>
     </tr>
<?php } ?>
  </table>
</body>
</html>

Link to comment
Share on other sites

hi

 

i created a button "Edit" and i make a function for that in javascript. when the user click on the button it should call that javascript function and go to the specified page. i did all this but it is not doing anything.

 

how can this problem be solved?

 

You can't call a javscript function from php. Php is a server side script and javascript client side. Php is parsed on the server and outputs an html file from which you can call the javascript function by using php to print a button with an onclick attribute that called the function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.