Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

PHP javascript link onclick


ghost's Avatar
0 0

Heyy guys … I need a piece of php script to be exicuted when a link is clicked on…

e.g.

<a onclick="phpstuff()">delete Item</a>


ghost's Avatar
0 0

You can't, PHP is ran on the server, not the client. However, if you use AJAX you can do what you need.


ghost's Avatar
0 0

are you suree?? ive seen it done …. im sure .. Hmm k … i suppose ill just use php Get or something


ghost's Avatar
0 0

you could make that jscript load a new page with the php…. what are you trying todo?


ghost's Avatar
0 0
&lt;script&gt;
function phpstuff(){
  &lt;?php
  echo &quot;my php stuff&lt;br /&gt;&#92;n&quot;;
  ?&gt;
}
&lt;/script&gt;

&lt;a onclick=&quot;phpstuff()&quot;&gt;delete Item&lt;/a&gt;

ghost's Avatar
0 0

dw .. thanks guys i done it like this :

viewer.php

  &lt;a href=deleteitem.php?id=$id&gt;Delete Item&lt;/a&gt;&lt;br&gt;  
  &lt;br&gt;&lt;/b&gt;&lt;/font&gt;
        &quot;);
	}
?&gt;
&lt;/center&gt;```

then deleteitem.php

```markup&lt;?
require &quot;config.php&quot;;
	database_connect();
    $id = $_GET[&#39;id&#39;];
	$sql = &quot;DELETE FROM contact 
				WHERE id = &#39;$id&#39;&quot;;
	$query = mysql_query($sql)or die(&quot;There was a problem while deleting: &quot;. mysql_error()); 	
?&gt;```

sawted

ghost's Avatar
0 0

xJorDyx wrote:

&lt;script&gt;
function phpstuff(){
  &lt;?php
  echo &quot;my php stuff&lt;br /&gt;&#92;n&quot;;
  ?&gt;
}
&lt;/script&gt;

&lt;a onclick=&quot;phpstuff()&quot;&gt;delete Item&lt;/a&gt;

The problem is, is that the code will be executed even before the link is clicked. Not a very good idea.


ghost's Avatar
0 0

i think what you want is:

&lt;script&gt;
function phpstuff()
{
window.location=&quot;thephp.php&quot;;
}
&lt;/script&gt;

and have the thephp.php redirect back to your page but really, i would do it with $_GET….works mo betta