I need to add a confirm statement for when people click to delete on my page.
I can't seem to get it working where do I put the java tags etc. in the code below:

echo"<td><img src='erase.jpg'><a href='delete.php?item=$Item'>Erase</a></td>"

And here's the code for the confirmation in Java:

onclick="return confirm('Are you sure you want to delete?')"

    Do it like this:
    function confirm (id){
    var agree = confirm ("Are you sure you want to delete it?");
    if (agree) {
    window.location.href = ("delete.php?item="+id);
    }
    else {
    // code to exercute if 'no' goes here
    alert('You cancelled the operation');
    //return false;
    window.location.href = ("same_page");
    }
    }

    and on your html:
    <a href="javascript: delete($item_id)">Erase</a>

      I've put this code in but it doesn't work unfortunately is it right?

      echo"
      <tr>
      <td>$Item</td>
      <td>$Description</td>
      <script language='javascript'>
      function confirm ($Item){
      var agree = confirm ('Are you sure you want to delete it?');
      if (agree) {
      window.location.href = ('delete.php?item=$Item');
      }
      else {
      // code to exercute if 'no' goes here
      alert('You cancelled the operation');
      //return false;
      window.location.href = ('glossaryadmin.php?item=$Item');
      }
      }
      </script>
      <td><a href='javascript: delete($Item)'>Erase</a></td>
      <td><img src='erase.jpg'><a href='delete.php?item=$Item'>Erase</a></td>
      <td><img width='22' height='22' src='edit.jpg'><a href='edit.php?item=$Item'>Edit</a></td>
      
      </tr>";
      

        remove the $ sign from the function:
        instead function confirm ($Item){

        function confirm (Item){

        and from your javascript call to confirm function:
        instead <a href='javascript: delete($Item)'>Erase</a>
        put <a href='javascript: confirm($Item)'>Erase</a>

        Assaf

          I've changed what you said but still doesn't work, i'm unsure as to whether the php tags need to be closed before the java ones start any ideas, here's the full code:

          <?php
          echo"
          
          
          <table border=1><tr>
          <td><strong>Item</strong></td><td><strong>Description</strong></td></tr>";
          $glossary = "SELECT * FROM glossary WHERE authorisation='n' ORDER BY 'item' ASC";
          $glossaryResults = mysql_query($glossary);
          while ($glossaryRow = mysql_fetch_array ($glossaryResults)){
          $Item = $glossaryRow["item"];
          $Description = $glossaryRow["description"];
          
          echo"
          <tr>
          <td>$Item</td>
          <td>$Description</td>
          <script language='javascript'>
          function confirm (Item){
          var agree = confirm ('Are you sure you want to delete it?');
          if (agree) {
          window.location.href = ('delete.php?item=$Item');
          }
          else {
          // code to exercute if 'no' goes here
          alert('You cancelled the operation');
          //return false;
          window.location.href = ('glossaryadmin.php?item=$Item');
          }
          }
          </script>
          <td><a href='javascript: confirm($Item)'>Erase</a></td>
          <td><img src='erase.jpg'><a href='delete.php?item=$Item'>Erase</a></td>
          <td><img width='22' height='22' src='edit.jpg'><a href='edit.php?item=$Item'>Edit</a></td>
          
          </tr>";}
          
          
          echo "</table>";
          
          ?>
          

            your mistake:
            window.location.href = ('delete.php?item=$Item');
            should be:
            window.location.href = ("delete.php?item="+Item);

              So sorry thanks for helping but i'm afraid that hasn't worked either. You don't think it has anything to do with the <script> tags or anything?

                I am using:
                <script type="text/javascript">

                but I don't think this is the problem.

                Try to debug it by adding alert under the function to see where is the problem. If you using Firefox they have under tools error consol for javascript.

                  You have a 2nd instance of $item, that has to be concatenated too. You can't reference PHP variables in javascript, the variable value has to be what's printed to the page, therefore in the echo statement it has to be concatenated, not included in a literal string.

                    Im getting error messages saying:

                    missing) after argument list
                    javascript:confirm(test)

                    confirm (test)

                      Really not a clue what I need to do from here, I don't understand what you mean about putting the links together (concatenate). Can what I want to do be done with the code above if it's changed slightly?

                        echo"
                        <tr>
                        <td>$Item</td>
                        <td>$Description</td>
                        <script language='javascript'>
                        function confirm (Item){
                        var agree = confirm ('Are you sure you want to delete it?');
                        if (agree) {
                        window.location.href = ('delete.php?item='".$Item."); "

                        I would break up the echo statement, into several.

                          So like this then, I click on the "erase" link but no good unfortunately

                          <?php
                          echo"
                          <tr>
                          <td>$Item</td>
                          <td>$Description</td>";?>
                          
                          <script type='text/javascript'>
                          function confirm(Item){
                          var agree = confirm ('Are you sure you want to delete it?');
                          if (agree) {
                          window.location.href = ('delete.php?item='+Item);
                          }
                          else {
                           code to exercute if 'no' goes here
                          alert('You cancelled the operation');
                          return false;
                          window.location.href = ('glossaryadmin.php?item=$Item');
                          }
                          }
                          
                          </script>
                          <?php echo"
                          <td><a href='javascript: confirm($Item)'>Erase</a></td>
                          <td><img src='erase.jpg'><a href='delete.php?item=$Item'>Erase</a></td>
                          <td><img width='22' height='22' src='edit.jpg'><a href='edit.php?item=$Item'>Edit</a></td>
                          
                          </tr>";}
                          
                          
                          echo "</table>";
                          
                          ?>
                          

                            This is how I'd do it:

                            <?php 
                            echo"
                            <tr> 
                            <td>$Item</td> 
                            <td>$Description</td>";?> 
                            
                            <script type="text/javascript">
                            <!--
                            function confirmDelete()
                            {
                            	var agree=confirm("Are you sure you wish to delete this record?");
                            	if (agree)
                            	return true ;
                            	else
                            	return false ;
                            }
                            -->
                            </script>
                            
                            <?php echo" 
                            <td>
                            <img src='erase.jpg'><a href='delete.php?item=$Item' onClick=\"return confirmDelete()\">Erase</a></td> 
                            <td><img width='22' height='22' src='edit.jpg'><a href='edit.php?item=$Item'>Edit</a></td> 
                            
                            </tr>";
                            
                            
                            echo "</table>";
                            
                            ?>

                              Thats fantastic worked first time!!!
                              Thankyou so much everyone for all your help on this thread!

                                Write a Reply...