Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2010
    Messaggi
    144

    Inviare dati da JS a PHP

    Ciao a tutti!
    Allora ho una domanda da porre perchè non riesco ad arrivare a capo di questo problema...

    Allora io ho un form in php che tramite js e ajax viene controllato dinamicamente.
    Ho una pagina php ch econtiene il form e le chiamate alle due pagine javascript...
    La pagina validate.js si occupa di prendere i vari dati del form e poi farne delle verifiche:
    validate.js
    Codice PHP:
    window.onload attachFormHandlers;

    function 
    attachFormHandlers()
    {
      
    // Make sure we're on a newer browser
        
    if (document.getElementsByTagName)
        { 
            var 
    objInput document.getElementsByTagName('input'); // get all input tags
            
    var form document.getElementById('form1'// get the form
        
                
    for (var iCounter=0iCounter<objInput.lengthiCounter++)
                {
                    
    objInput[iCounter].onchange = function(){return attach(this);} // attach the onchange event to each input tag
                
    }        
        
    form.onsubmit = function(){return validate();} // atttach the onsubmit to the form 
        
    }
    }


    function 
    attach(objInput)
    {
        
    sVal objInput.value//sVal is the value of the input field being validated
        
    var sFeedBack//feedback message sent back to the user
        
    var sFeedbackLoc =  objInput.getAttribute('message');
        
        if (
    objInput.getAttribute('required') == "true")
        {
            
    sFeedback validateRequired(sVal); //validateRequired() checks if it is invalid and sends back feedback
        
    }
        else 
    sFeedback "";
        
                
        if (
    sVal != ""//if the value is blank we don't need to validate.  If it is required, the word
            //"required"  will already be the feedback message from the validateRequired() function
        
    {
        
    // check the different validation cases (ie: email, phone, etc.)
            
    switch (objInput.getAttribute('validate'))
            {
                
                case 
    "email":
                    
    sFeedback validateEmail(sVal);
                    break;        
                case 
    "zip":
                    
    sFeedback validateZip(sVal);
                    break;
                case 
    "password":
                    
    sFeedback validatePassword(sVal);
                    break;
                case 
    "name":
                    
    sFeedback validateName(sVal);
                    break;
                case 
    "lastname":
                    
    sFeedback validateLastName(sVal);
                    break;
                
                
            }
        }
                
    // after validation is complete return the feedback 
        
    document.getElementById(sFeedbackLoc).innerHTML sFeedback;    
    }

    function 
    validateRequired(sVal)
    {
               if (
    sVal != ""//if it is rquired and blank then it is an error and continues to be required
            
    {
                return (
    "Thank You");
            }
            else 
            {
                
    gContinue false;
                return(
    "Required");
            }
    }



    function 
    validateEmail(sVal)
    {
        
    // our email regular expression ([url]http://www.regexlib.com[/url])
     
    var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
     
        
    // do the comparison, if we have a match write thank you or else the email is invalid
        
    if (regex.test(sVal))
        {
            var 
    email sVal;
            return 
    email;
            
          return 
    "Thank You";
        }
        else
        {
          return 
    "Invalid Email Address";
        }
    }

    function 
    validatePhone(sVal)
    {
        
    // our phone regular expression
    // This expression is a very simplex expression that allows null values or 3 digits, dash, 
    //3 digits, dash, 4 digits. It validates a basic US phone number. Written by Jason N. Gaylord.([url]http://www.regexlib.com[/url])
    // Matches:       [555-555-1212], [123-456-7890]
     
    var regex=/^(\d{3}-\d{3}-\d{4})*$/;
     
        
    // do the comparison, if we have a match write thank you or else the email is invalid
        
    if (regex.test(sVal))
        {
          return 
    "Thank You";
        }
        else
        {
          return 
    "Invalid Phone";
        }
    }

    function 
    validateZip(sVal)
    {
        
    // our email regular expression
    //Javascript matches US zipcodes not allowing all zeros in first 5 or +4 ([url]http://www.regexlib.com[/url])
    // Matches:       [12345], [12345-6789], [123456789]
     
    var regex=/(^(?!0{5})(\d{5})(?!-?0{4})(-?\d{4})?$)/;
     
        
    // do the comparison, if we have a match write thank you or else the email is invalid
        
    if (regex.test(sVal))
        {
          return 
    "Thank You";
        }
        else
        {
          return 
    "Invalid ZipCode";
        }
    }

    function 
    validatePassword(sVal)

    //Description: The password's first character must be a letter, it must contain at least 4 characters
    //and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
    //Matches:     [abcd], [aBc45DSD_sdf], [password] ([url]http://www.regexlib.com[/url])
     
    var regex=/^[a-zA-Z]\w{3,14}$/;
     
        
    // do the comparison, if we have a match write thank you or else the email is invalid
        
    if (regex.test(sVal))
        {
          return 
    "Thank You";
        }
        else
        {
          return 
    "Invalid Password";
        }
    }

    function 
    validateName(sVal)

    //This is the simplest RegEx for validating someone's name. The name can contain only alphabets(in either case) & 
    //should be of minimum length 4 & maximum length 32. Only white spaces are allowed apart from alphabets.
    //Matches:     [some body], [hey there], [hello] ([url]http://www.regexlib.com[/url])
     
    var regex=/^([a-zA-z\s]{4,32})$/;
     
        
    // do the comparison, if we have a match write thank you or else the email is invalid
        
    if (regex.test(sVal))
        {
          return 
    "Thank You";
        }
        else
        {
          return 
    "Invalid Name";
        }
    }

    function 
    validateLastName(sVal)

    //This is the simplest RegEx for validating someone's name. The name can contain only alphabets(in either case) & 
    //should be of minimum length 4 & maximum length 32. Only white spaces are allowed apart from alphabets.
    //Matches:     [some body], [hey there], [hello] ([url]http://www.regexlib.com[/url])
     
    var regex=/^([a-zA-z\s]{4,32})$/;
     
        
    // do the comparison, if we have a match write thank you or else the email is invalid
        
    if (regex.test(sVal))
        {
          return 
    "Thank You";
        }
        else
        {
          return 
    "Invalid Cognome";
        }

    Ok, ora c'è la pagina submitvalidate.js che si occupa principalmente di mandarmi l'errore se tutti i dati non sono stati inseriti correttamente. oppure mi ridirige ad inserisci_dati_form.php

    Codice PHP:
    var gErrors 0//number of errors is set to none to begin with

    function validate()
    {
    var 
    tables//variable which will become an array holding all elements with the td tagname

    // store all <td> elements in the tables array
    tables document.getElementsByTagName('td')

    //loop through all the <td> elements 
        
    for (i=0i<tables.lengthi++)
            {
            
    // if the class name of that td element is rules check to see if there are error warnings
            
    if (tables[i].className == "rules")
                {
                    
    //if there is a thank you or its blank then it passes
                    
    if (tables[i].innerHTML == 'Thank You' || tables[i].innerHTML == '' )
                    {
                    
    tables[i].style.color '#000000';//the color is changed to blank or stays black
                    
    }
                    else
                    {
                    
    gErrors gErrors 1//the error count increases by 1
                    
    tables[i].style.color '#ff0000';//error messages are changed to red
                    
    }
                }
            }
            
            if (
    gErrors 0){
                
    //if there are any errors give a message
                
    alert ("Assicurati che tutti i campi obbligatori siano compilati correttamente!");
                
    gErrors 0;// reset errors to 0
                
    return false;
            }
            
            else 
            {
                
    //alert("The Form Is Valid!");
                
    window.location 'inserisci_dati_form.php'
                
    return false;//set this to true in practice to allow the form to submit
            
    }
        


    Io però vorrei che alla pagina inserisci_dati_form.php inviassi in qualche modo i dati presi dal form per riceverli con post e lavorare con il database...
    Se fossi in php non avrei problemi... ma in js come faccio?
    Qualcuno mi da una mano???

    GRAZIE!

  2. #2
    Moderatore di Annunci siti web, Offro lavoro/collaborazione, Cerco lavoro L'avatar di cavicchiandrea
    Registrato dal
    Aug 2001
    Messaggi
    26,133
    Prova a cambiare questa riga:
    window.location = 'inserisci_dati_form.php'
    con queste:
    document.nomeTuoForm.action = 'inserisci_dati_form.php';
    document.nomeTuoForm.submit();
    Cavicchi Andrea
    Problemi con javascript, jquery, ajax clicca qui

  3. #3
    Utente di HTML.it
    Registrato dal
    May 2010
    Messaggi
    144
    Grazie per la risposta!

    Adesso più o meno funziona, bisogna solo aggiustare qualcosina, per chiunque fosse interessato la spiegazione è qua:

    http://forum.html.it/forum/showthrea...0#post13298070

  4. #4
    Utente di HTML.it L'avatar di Xinod
    Registrato dal
    Sep 2000
    Messaggi
    13,649
    Bravo! questo si chiama crossposting ed e' vietato da regolamento

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 © 2025 vBulletin Solutions, Inc. All rights reserved.