PDA

View Full Version : Php js problem


kar2905
06-18-2009, 04:39 PM
I have a simple form of three columns : date , todo , delete
The last column is for deleting the specific entry . I am using onclick to transfer the control to a javascript function while passing the date and todo as arguments . Then in the function , i am sending query through php to mysql to delete the entry from the database . But , I am getting errors while running it

this the js function :

<script type="text/javascript">
function delete($date,$todo)

{
<?php
session_start();
include 'mysqlconnect.php';

$sql="DELETE FROM todo WHERE Date = '".$date ."' AND Todo = '".$todo."' AND user = '".$_SESSION['user']."' ";
$result=mysql_query($sql) or die(mysql_error());
?>
}
</script>


This is where I call the function :

<?php
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" ;
echo $row['Date'] ;
echo "</td>";


echo "<td>";
echo $row['Todo'] ;
echo "</td>";

echo "<td>";

?>
<input type="button" value="Delete" name="delete" onClick="delete(<?php echo $row['Date'] . ',' . $row['Todo']; ?>);" />
<?php
echo"</td>";
echo "</tr>";
}
?>

pls help

ohgod
06-18-2009, 05:25 PM
you can't call php through js like that... js is client side, php is server side and is processed before the js.

if you need to call php after the page is generated you need ajax... which is why you've posted here. however, i see no ajax in your code....

if you don't want to learn ajax, then submit your info via a regular html form to a processor page...


also, please wrap code tags around code to make it easier to read.

kar2905
06-19-2009, 10:20 AM
Thanks for the reply ..
I changed my code .. now pls help
This is the JS function

<script type="text/javascript">
var x=false;
if(window.XMLHttpRequest)
x=new XMLHttpRequest();
else if(window.ActiveXObject)
x= new ActiveXObject("Microsoft.XMLHTTP");
function del(date,todo)
{
alert("Hello its in delete");
if (x)
{
var url="delete.php";
url=url+"?date="+date+"&todo="+todo;
x.open("GET",url);
x.onreadystatechange=function()
{
if(x.readyState==4 && x.status==200)
{
document.getElementById("Profile").innerHTML=x.responseText;
}

}
x.send(null);

}
}


</script>

This is where I call
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" ;
echo $row['Date'] ;
echo "</td>";


echo "<td>";
echo $row['Todo'] ;
echo "</td>";

echo "<td>";

?>
<input type="button" value="Delete" name="delete" onClick="del("<?php echo $row['Date'];?>", "<?php echo $row['Todo']; ?>")" />
<?php
echo"</td>";
echo "</tr>";
}
?>

This is the delete.php file where it is being deleted ..

<?php
session_start();

include 'mysqlconnect.php';
$sql="DELETE FROM todo WHERE Date = '".$_GET['date'] ."' AND Todo = '".$_GET['todo']."' AND user = '".$_SESSION['user']."' ";
$result=mysql_query($sql) or die(mysql_error());
?>

kar2905
06-19-2009, 10:21 AM
The error is SYNTAX ERROR IN DEL FUNCTION

ohgod
06-19-2009, 04:38 PM
can't say as i see the problem outright. get firebug for firefox and you'll be able to watch the entire transaction to see where the break is.

that aside, you should really be sanitizing that info before dropping it in a sql query.