0

Hi all! I'm not a very good coder and i'm down here with a question for you all.

Firstly, is it possible to code php within javascript?

How?

function confirmation() {
    var answer = confirm("Delete class?")
    if (answer){
        window.location = "deleteClass.php?id=<?php echo $row['idClass'];?>";
    }
    else{
        alert("Delete unsuccessful!")
    }
}

i'm unable to fetch the data using get function on the other page.
Your help is greatly appreciated.

Edited by mike_2000_17: Fixed formatting

3
Contributors
30
Replies
31
Views
7 Years
Discussion Span
Last Post by rajarajan07
0

If this javascript code is outputted by php then it is possible. If you're trying to access php from within a js file, then no, it is not possible.

0

Yes. it is been outputted by php code. However, i cant retrieve the data from other page.

0
<?php
session_start();
if(isset($_SESSION['nric'])){
$msg = '<p>Welcome! Logged in as ' .$_SESSION['nric'].'</p>';

$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'sjas';

$link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die(mysqli_connect_error());
$sql = "select cl.idClass,c.name, t.start_time,t.end_time,cl.exam_date, cl.closing_date,cl.day01,cl.day02,cl.day03 from course c, class cl,time_slot t where c.idCourse=cl.Course_idCourse and t.idTime_Slot=cl.Time_Slot_idTime_Slot";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
$numRows = mysqli_num_rows($result);
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <title>St John Ambulance Singapore</title>
        <script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />

  <style type="text/css">
<!--
body {
    background-color: #FFFFFF;
}
-->
</style><ul id="MenuBar1" class="MenuBarHorizontal">
    <li><a href="viewCourse.php" class="MenuBarActive">Course</a></li>
    <li><a href="viewClass.php">Class</a></li>
    <li><a href="viewUser.php">User</a></li>
    <li><a href="ViewVenue.php">Venue</a></li>
    <li><a href="assign.php">Assign</a></li>
  </ul>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
<!--
[COLOR="Red"]function confirmation() {
    var answer = confirm("Delete class?")
    if (answer){
        window.location = "deleteClass.php?id=<?php echo $row['idClass'];?>";
    }
    else{
        alert("Delete unsuccessful!")
    }
}
//-->
</script>
    </head>
    <?php
echo '<div align="right">' .$msg. '</div>';
?>
<form id="form1" name="form1" method="post" action="logout.php">
  <div align="right">
    <input type="submit" name="button" value="Logout" />
  </div>
</form>
    <hr />


        <h2 >View Class</h2>
        <form id="form2" name="form2" method="post" action="addClass.php">
            <input type="submit" name="button" value="Create" />
            </form>
        <table border='1' cellpadding='0' cellspacing='0'>
    <tr>
        <th>Class ID</th>
        <th>Course Name</th>
        <th>Days</th>
        <th>Start Time</th>
        <th>End Time</th>
        <th>Exam Date</th>
        <th>Closing Date</th>

    </tr>
  <?php
    if($numRows > 0){
        while($row = mysqli_fetch_array($result)){
            $classid = $row['idClass'];
            $course=$row['name'];
            $start = $row['start_time'];
            $end = $row['end_time'];
            $exam = $row['exam_date'];
            $closing = $row['closing_date'];
            $day1= $row['day01'];
            $day2= $row['day02'];               
            $day3= $row['day03'];       
            $day4= $row['day04'];
            echo "<tr>";
            echo "<td>".$classid."</td>";
            echo "<td>".$course."</td>";
            echo "<td><li>".$day1."<li>".$day2."<li>".$day3."<li>".$day4."</li>";"</li>";"</li>";"</li>";"</td>";
            echo "<td>".$start."</td>";
            echo "<td>".$end."</td>";
            echo "<td>".$exam."</td>";
            echo "<td>".$closing."</td>";
            echo "<td>"."<form>"."<input type='button' onclick='confirmation()' value='Delete'>"."</form>"."</td>";
            echo "</tr>";
        }//while
    }else{
        echo "<tr>";
        echo '<td colspan="2">No Record.</td>';
        echo "</tr>";
    }

  ?>
        </table>
    </body>
</html>

Edited by mike_2000_17: Fixed formatting

0

Correct. You can change the bottom red line to include the id within the brackets of confirmation, like so:

echo "<td>"."<form>"."<input type='button' onclick='confirmation(" . $classid . ")' value='Delete'>"."</form>"."</td>";

And your confirmation function like this:

function confirmation(deleteid) {
  var answer = confirm("Delete class?")
  if (answer) {
    window.location = "deleteClass.php?id=" + deleteid;
  }
  else {
    alert("Delete unsuccessful!")
  }
0

Thanks for your effort. But your code does not direct it to the another page.

0

Nope. No errors. Yes, it does execute and i am able to see the alerts. But it remain the same.

0
onclick="confirmation('deleteClass.php?id=<?php echo($classid);?>')">

Directly pass the value as you want as a php and get it in a url variable

function confirmation(url) {
  alert(url);
  var answer = confirm("Delete class?")
  if (answer) {
    window.location = url;
  }
  else {
    alert("Delete unsuccessful!")
  }

Edited by rajarajan07: n/a

0

nope. No errors. Yes.it does execute and i am able to see the alerts. But its remain the same. Does not direct to deleteClass.php

0

Hi.thanks for your help too. There some syntax errors. and i changed the code to
echo "<td>"."<form>"."<input type='button' onclick='confirmation('deleteClass.php?id=".$classid."')' value='Delete'>"."</form>"."</td>";

But the javascript does not execute

0
function confirmation(deleteClass.php) {
    alert (deleteClass.php);
    var answer = confirm("Delete class?")
    if (answer){
        window.location = "deleteClass.php";
    }
    else{
        alert("Delete unsuccessful!")
    }
}




<?php
    if($numRows > 0){
        while($row = mysqli_fetch_array($result)){
            $classid = $row['idClass'];
            $course=$row['name'];
            $start = $row['start_time'];
            $end = $row['end_time'];
            $exam = $row['exam_date'];
            $closing = $row['closing_date'];
            $day1= $row['day01'];
            $day2= $row['day02'];               
            $day3= $row['day03'];       
            $day4= $row['day04'];
            echo "<tr>";
            echo "<td>".$classid."</td>";
            echo "<td>".$course."</td>";
            echo "<td><li>".$day1."<li>".$day2."<li>".$day3."<li>".$day4."</li>";"</li>";"</li>";"</li>";"</td>";
            echo "<td>".$start."</td>";
            echo "<td>".$end."</td>";
            echo "<td>".$exam."</td>";
            echo "<td>".$closing."</td>";
            echo "<td>"."<form>"."<input type='button' onclick='confirmation('deleteClass.php?id=".$classid."')' value='Delete'>"."</form>"."</td>";
            echo "</tr>";
        }//while
    }else{
        echo "<tr>";
        echo '<td colspan="2">No Record.</td>';
        echo "</tr>";
    }

  ?>

Edited by mike_2000_17: Fixed formatting

0
onclick="confirmation('deleteClass.php?id=<?php echo($classid);?>')">

Add the code as above in your input element


Remove your js code and add this one.

function confirmation(url) {
  alert(url);
  var answer = confirm("Delete class?")
  if (answer) {
    window.location = url;
  }
  else {
    alert("Delete unsuccessful!")
  }
0

sorry to trouble you. Do you mind code out the whole echo for the sentence? Cause i keep having syntax errors and all after copy your code.

0

Sorry my bad. The errors is came from the function after i removed the javascripts code

0

Then If you dont mind, give me the structure of the table, inserted records, and your proper code as an attachment. because you gave the code without the connection establish and the javascript you used does not make sense with your passing parameters. so go to the advanced edit and attach your file and also the table creation statements and inserted records. so that I will test it here. and make the corrections appropriate

0

Check it and denote the error properly what is shown on the screen.

Attachments
<?php
session_start();
if(isset($_SESSION['nric'])){
$msg = '<p>Welcome! Logged in as ' .$_SESSION['nric'].'</p>';

$HOST = 'localhost';
$USERNAME = 'root';
$PASSWORD = '';
$DB = 'sjas';
	
$link = mysqli_connect($HOST,$USERNAME,$PASSWORD,$DB) or die(mysqli_connect_error());
$sql = "select cl.idClass,c.name, t.start_time,t.end_time,cl.exam_date, cl.closing_date,cl.day01,cl.day02,cl.day03,cl.day04 from course c, class cl,time_slot t where c.idCourse=cl.Course_idCourse and t.idTime_Slot=cl.Time_Slot_idTime_Slot";
$result = mysqli_query($link, $sql) or die(mysqli_error($link));
$numRows = mysqli_num_rows($result);
}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<title>St John Ambulance Singapore</title>
		<script src="SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<link href="SpryAssets/SpryMenuBarHorizontal.css" rel="stylesheet" type="text/css" />

  <style type="text/css">
<!--
body {
	background-color: #FFFFFF;
}
-->
</style><ul id="MenuBar1" class="MenuBarHorizontal">
    <li><a href="viewCourse.php" class="MenuBarActive">Course</a></li>
    <li><a href="viewClass.php">Class</a></li>
    <li><a href="viewUser.php">User</a></li>
    <li><a href="ViewVenue.php">Venue</a></li>
    <li><a href="assign.php">Assign</a></li>
  </ul>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgDown:"SpryAssets/SpryMenuBarDownHover.gif", imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
<!--

//-->
</script>
<script language="javascript">
function confirmation(url) {
	alert (url);
	var answer = confirm("Delete class?");
	if (answer){
		window.location = url;
	}
	else{
		alert("Delete unsuccessful!")
	}
}
</script>
</head>
<?php
echo '<div align="right">' .$msg. '</div>';
?>
<form id="form1" name="form1" method="post" action="logout.php">
  <div align="right">
    <input type="submit" name="button" value="Logout" />
  </div>
</form>
	<hr />

	
		<h2 >View Class</h2>
		<form id="form2" name="form2" method="post" action="addClass.php">
			<input type="submit" name="button" value="Create" />
			</form>
		<table border='1' cellpadding='0' cellspacing='0'>
  	<tr>
  		<th>Class ID</th>
		<th>Course Name</th>
		<th>Days</th>
		<th>Start Time</th>
		<th>End Time</th>
		<th>Exam Date</th>
		<th>Closing Date</th>
		
  	</tr>
  <?php
  	if($numRows > 0){
  		while($row = mysqli_fetch_array($result)){
			$classid = $row['idClass'];
			$course=$row['name'];
			$start = $row['start_time'];
			$end = $row['end_time'];
			$exam = $row['exam_date'];
			$closing = $row['closing_date'];
			$day1= $row['day01'];
			$day2= $row['day02'];				
			$day3= $row['day03'];		
			$day4= $row['day04'];
			echo "<tr>";
			echo "<td>".$classid."</td>";
			echo "<td>".$course."</td>";
			echo "<td><li>".$day1."<li>".$day2."<li>".$day3."<li>".$day4."</li>";"</li>";"</li>";"</li>";"</td>";
			echo "<td>".$start."</td>";
			echo "<td>".$end."</td>";
			echo "<td>".$exam."</td>";
			echo "<td>".$closing."</td>";
			echo "<td>"."<form>"."<input type='button' value='Delete' onclick="confirmation('deleteClass.php?id=<?php echo($classid);?>')">/>"."</form>"."</td>";
			echo "</tr>";
		}//while
  	}else{
  		echo "<tr>";
		echo '<td colspan="2">No Record.</td>';
		echo "</tr>";
  	}

  ?>
		</table>
	</body>
</html>
0

echo "<td>"."<form>"."<input type='button' value='Delete' onclick='confirmation(\deleteClass.php?id=<?php echo($classid);?>\)'>/>"."</form>"."</td>";

I can't find the parse error

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.