do u know how php page is evaluated on server.
means:
when a php page is parsed on server , then php code is evaluated and and evaluated result is send back to page .then page is sent to browser.my mean to say, you will have only static content as html javascript, css on your browser.if you want to evaluate your php code then you have to send a request to server.
It can be sent in two ways.
By Page reloading
By AJAX.
so if you don't want to reload you page then you should go for ajax.
then you code will be
<a href='javascript: refresh_captcha();'>refresh</a>
function refresh_captcha()
{
// ajax call to server to get new captcha string
// evaluate this code on server and send string back to browser
//<?php
// $captcha1 = new CaptchaCode();
// $code = str_encrypt($captcha1->generateCode(6));
//
//?>
// var code = <ajax response>
var img = document.getElementById('captcha_img');
img.src = '/captcha_images.php?width=120&height=40&code='+code; // change this line
//document.getElementById ("captcha_img").src = img.src;
}
for ajax call read this
http://www.w3schools.com/ajax/