Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to reload only a captcha image and not the whole page
    primarykey
    data
    text
    <p>I need some help, as I can't reload a captcha image in my form, if the writing over the image is not very clear or simple to read, as it can happens sometimes.</p> <p>I read a lot about that, here and around in the internet but my problem is different from what most people seem to have to face. In fact I create the captcha image in a page called - captcha.php - and then in another page called - contacts.php - I create the writing for the image and a casual unique string that I pass to the captcha.php file, as I don't want to use the sessions or cookies, but I use instead a database with a table called - validation. Translating all this in PHP, the captcha.php has this code to retrieve the string:</p> <pre><code>$pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); try { $stmt = $pdo-&gt;prepare("SELECT * FROM validation WHERE url_key = ? AND expire_date &gt; NOW()"); $stmt-&gt;execute(array($_GET['token'])); $row = $stmt-&gt;fetch(PDO::FETCH_ASSOC); } catch(PDOException $e) { echo "Error!: " . $e-&gt;getMessage() . "&lt;br /&gt;"; die(); } etc..... </code></pre> <p>and then my contacts.php has this:</p> <pre><code> $url_key = sha1(uniqid(rand(), true)); $captcha = random_string(4); try { $stmt = $pdo-&gt;prepare("INSERT INTO validation (id_validation, url_key, captcha, expire_date) VALUES ('', ?, ?, DATE_ADD(NOW(), INTERVAL 5 MINUTE))"); $stmt-&gt;execute(array($url_key, $captcha)); } catch(PDOException $e) { echo "Error!: " . $e-&gt;getMessage() . "&lt;br /&gt;"; die(); } </code></pre> <p>Now I do need, of course, to refresh just my image and not the entire contacts.php page. I know you can do that with Ajax, but I've got no good knowledge about that.</p> <p>My contacts.php has the tag img to show the reader the captcha:</p> <pre><code> &lt;img src="captcha.php?token=&lt;?php echo $url_key; ?&gt;" alt="" /&gt; </code></pre> <p>I tried to change it this way:</p> <pre><code> &lt;img src="captcha.php?token=&lt;?php echo $url_key; ?&gt;" id="captcha" /&gt; &lt;a href="#" onClick="return changeCaptcha('captcha')"&gt;Reload the image&lt;/a&gt; </code></pre> <p>and then in my head section, I put this Javascript code:</p> <pre><code> function changeCaptcha() { document.getElementById('captcha').src = 'captcha.php' + Math.floor(Math.random()*100); } </code></pre> <p>but it doesn't work at all. If I click on the link the captcha disappears completely.</p> <p>I tried then to change this:</p> <pre><code> &lt;img src="captcha.php?token=&lt;?php echo $url_key; ?&gt;" id="captcha" /&gt; &lt;a href="#" onClick="return reloadImg('captcha');"&gt;Reload the image&lt;/a&gt; </code></pre> <p>and then with a new Javascript file, that's:</p> <pre><code> function reloadImg(id) { var obj = document.getElementById(id); var src = obj.src; var pos = src.indexOf('?'); if (pos &gt;= 0) { src = src.substr(0, pos); } var date = new Date(); obj.src = src + '?v=' + date.getTime(); return false; } </code></pre> <p>but it doesn't work even this way. The problem is in the - php echo $url_key; - as I don't know how to treat this. I can't do this for example:</p> <pre><code> function changeCaptcha() { document.getElementById('captcha').src = 'captcha.php?token=&lt;?php echo $url_key; ?&gt;' + Math.floor(Math.random()*100); } </code></pre> <p>as it's wrong again.</p> <p>Do you have some ideas? Thank you in advance. Bye.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload