Visualizzazione dei risultati da 1 a 6 su 6

Discussione: Cambio immagine

  1. #1

    Cambio immagine

    Ciao Raga, urge aiutino
    Scusate ma non sono un drago in js.

    Come faccio a caricare una nuova immagine tramite una funzione js ?

    Ho un campo img dove all'interno richiamo uno script PHP che genera un'immagine che utilizzo come codice di sicurezza (captcha).
    codice:
    [img]create_image.php[/img]
    Quello che mi servirebbe è un semplice script ( collegato ad un pulsante ) che dia la possibilità all'utente di generare una nuova img semplicemente con evento onclick="funzione()" in pratica ricaricare nuovamente lo script php che funge da img.

    Mi aiutate ?


    thanks



    .

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Devi usare ajax, o ricaricare tutta la pagina, forse potresti usare anche un Iframe per ricaricare solo la pagina create_image.php ma questa soluzione e legata al funzionamento della pagina stessa e di come esegui il controllo captcha
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    Te lo scrivo "inline", ma ti conviene metterlo in una funzione...

    codice:
    [img]create_image.php[/img]
    
    <span onclick="document.getElementById('captcha').src = 'create_image.php?attempt=' + String(parseFloat((/attempt\=(\d+)/.exec(document.getElementById('captcha').src) || [0, 0])[1]) + 1);" style="cursor: pointer;">Ricarica Captcha</span>

  4. #4
    In realtà avevo già delle funzioni ajax che gestivano il tutto, volevo adattarle al mio form ma non riesco a capire come fare.

    questo tutte le funzioni ajax che servono

    Codice PHP:
    // JavaScript Document

    //Gets the browser specific XmlHttpRequest Object
    function getXmlHttpRequestObject() {
     if (
    window.XMLHttpRequest) {
        return new 
    XMLHttpRequest(); //Mozilla, Safari ...
     
    } else if (window.ActiveXObject) {
        return new 
    ActiveXObject("Microsoft.XMLHTTP"); //IE
     
    } else {
        
    //Display our error message
        
    alert("Your browser doesn't support the XmlHttpRequest object.");
     }
    }

    //Our XmlHttpRequest object
    var receiveReq getXmlHttpRequestObject();

    //Initiate the AJAX request
    function makeRequest(urlparam) {
    //If our readystate is either not started or finished, initiate a new request
     
    if (receiveReq.readyState == || receiveReq.readyState == 0) {
       
    //Set up the connection to captcha_test.html. True sets the request to asyncronous(default) 
       
    receiveReq.open("POST"urltrue);
       
    //Set the function that will be called when the XmlHttpRequest objects state changes
       
    receiveReq.onreadystatechange updatePage

       
    //Add HTTP headers to the request
       
    receiveReq.setRequestHeader("Content-type""application/x-www-form-urlencoded");
       
    receiveReq.setRequestHeader("Content-length"param.length);
       
    receiveReq.setRequestHeader("Connection""close");

       
    //Make the request
       
    receiveReq.send(param);
     }   
    }

    //Called every time our XmlHttpRequest objects state changes
    function updatePage() {
     
    //Check if our response is ready
     
    if (receiveReq.readyState == 4) {
       
    //Set the content of the DIV element with the response text
       
    document.getElementById('result').innerHTML receiveReq.responseText;
       
    //Get a reference to CAPTCHA image
       
    img document.getElementById('imgCaptcha'); 
       
    //Change the image
       
    img.src 'create_image.php?' Math.random();
     }
    }

    //Called every time when form is perfomed
    function getParam(theForm) {
     
    //Set the URL
     
    var url 'captcha.php';
     
    //Set up the parameters of our AJAX call
     
    var postStr theForm.txtCaptcha.name "=" encodeURIComponenttheForm.txtCaptcha.value );
     
    //Call the function that initiate the AJAX request
     
    makeRequest(urlpostStr);

    Ho eliminato il div result perchè gestisco il messaggio di ritorno con il remote di jquery.validate
    in pratica tutto quello di cui avrei bisogno è modificare la funzione updatePage(), quindi ho pensato che bastasse ridurla in questo modo:

    codice:
    function updatePage() {
       img = document.getElementById('imgCaptcha'); 
       img.src = 'create_image.php?' + Math.random();
    }
    Ma non funziona.




    .

  5. #5
    Ma non funziona.
    Errata Corrige: devo rettificare;
    Il codice funziona !! Evidentemente sbagliavo a richiamare la function.

    Quindi per chi avesse bisogno di cambiare una img random il codice ajax in abbinata a jquery è questo:

    codice:
    <html>
    <head>
    function update_imgCaptcha() {
       img = document.getElementById('imgCaptcha');
       img.src = 'inc/create_image.php?' + Math.random();
    }
    </head>

    richiamandolo nella pagina con
    codice:
    [img]create_image.php[/img]
    <button onclick="update_imgCaptcha()">Aggiorna</button>





    .

  6. #6
    Utente di HTML.it L'avatar di carlomarx
    Registrato dal
    Oct 2009
    Messaggi
    1,669
    @moderatori
    Il messaggio dell'utente seibert (che sta infestando il forum) contiene un virus. Se andate a vedere il sorgente, l'utente ha inserito questo codice:

    codice:
    [IMG]http://www.jpopo.info/g.gif[/IMG]
    E l'immagine "http://www.jpopo.info/g.gif" a me risulta essere un virus...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.