0

I'm having some trouble getting my HTML and php to work together.

I've made a onClick that should run a PHP function i have in the top of the PHP file. But every time i click it, it says: ReferenceError: deleteSub is not defined. Can someone tell me what I've done wrong?

HTML:

<?php
                if(mysql_num_rows($sql_select_delete) > 0){
                     while($row = mysql_fetch_assoc($sql_select_delete)){
                         ?><div class="abo_box">
                           <label class="abo_navn" id="<? echo $row["id"]; ?>"><? echo $row['abo_name'];  ?></label>
                           <label class="delete" onclick="deleteSub();">Delete</label>
                         </div>
                         <?php
                     }
                }
           ?>

PHP:

function deleteSub(){
      echo "deleted";
 }
5
  • With onClick, you're calling a JS function not php. You can't do this without a refresh or ajax call. Commented Mar 31, 2015 at 8:12
  • onClick call javascript function, not PHP function... Commented Mar 31, 2015 at 8:12
  • Okay? I googled it before posting, and found a stackoverflow where this was the "right anwser"(cant find the link agian. sorry). I might have been reading it wrong tho, looks like it. Is there a way to do this then? Commented Mar 31, 2015 at 8:15
  • stackoverflow.com/questions/19323010/… Commented Mar 31, 2015 at 8:18
  • Thanks @Zdenek Leitkep for your link. That helped! Commented Mar 31, 2015 at 8:20

2 Answers 2

2

You can't call directly a PHP code like this What you can do is to create a javascript function and you need to use Ajax witch will send a request to your re server and here you can call your PHP function Remember : PHP is server side code and HTML, JS is client side code, the only way to make them work together is using HTTP requests.

Sign up to request clarification or add additional context in comments.

Comments

0

you could do something like this. But to use server side information you should use AJAX and http requests. This example is just for changing DOM elements.

 <p id="demo" onclick="myFunction()">Click me to change my text color.</p>

<script>
function myFunction() {
    document.getElementById("demo").style.color = "red";
}
</script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.