Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Php js problem

  1. #1
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts

    Php js problem

    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

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    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.
    Last edited by ohgod; 06-18-2009 at 05:29 PM.

  • #3
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts
    Thanks for the reply ..
    I changed my code .. now pls help
    This is the JS function

    Code:
    <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
    Code:
    <?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 Code:
    <?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());
    ?>

  • #4
    New Coder
    Join Date
    Jun 2009
    Location
    Manipal
    Posts
    45
    Thanks
    2
    Thanked 3 Times in 3 Posts
    The error is SYNTAX ERROR IN DEL FUNCTION

  • #5
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •