PC Ekspert Forum

PC Ekspert Forum (http://forum.pcekspert.com/index.php)
-   Web dizajn, programiranje i ostalo (http://forum.pcekspert.com/forumdisplay.php?f=39)
-   -   Ajax Messages on PHP form submit (http://forum.pcekspert.com/showthread.php?t=287906)

cdhr 11.09.2017. 09:03

Ajax Messages on PHP form submit
 
Zanima me koje sve načine koristite prikazati messages od formi?
Problem: User koristi krivi input, submita, stranica se refresha( php server side validacija), i user dobija poruke o neispravnom inputu.

Ovo je forma:

Code:


 

   

Sign Up


 

 

   
 

 

   
 

 

   
 

 

   
 

 

   

By clicking Sign Up, you agree to our Terms and Conditions and that you have read our Privacy Policy, including our Cookie Usage.You can opt out at any time.


 


Želim postići pri submitu stranica se ne refresha, nego samo prikazuje poruke od forme.
Hvala na pomoći, koristim jquery library, može i vanilla js.

Više sam php orijentiran i pokušavam nešto tipa :
Code:

$('form').submit(function(event) {

    // get the form data before sending via ajax
    var formData = {
        'name'              : $('input[name=name]').val(),
        'number'            : $('input[name=number]').val(),
        'message'            : $('input[name=message]').val(),
        'contactSubmit'      : 1
    };

    // send the form to your PHP file (using ajax, no page reload!!)
    $.ajax({
        type: 'POST',
        url:  'file.php', // <<<< -------  complete with your php filename (watch paths!)
        data:  formData,  // the form data
        dataType: 'json', // how data will be returned from php
        encode: true
    })
    // JS (jQuery) will do the ajax job and we "will wait that promise be DONE"
    // only after that, we´ll continue
    .done(function(data) {
        if(data.success === true) {
          // show the message!!!
          $('#success').show();
        }
        else{
            // ups, something went wrong ...
            alert('Ups!, this is embarrasing, try again please!');
            }
        });
    // this is a trick, to avoid the form submit as normal behaviour
    event.preventDefault();
});
    });

Ali bezuspiješno.
Hvala na pomoći :fala:
p.s.
Aplikacija koja se radi na laravel, ovo je signup.blade.php unutar components foldera.
Mislio sam bacit reserach na :
socket.io ali koristiti socket samo za form validaciju na kraju se ispostavilo kao vulnerability i overkill za tako nešto.

sinisa1989 11.09.2017. 12:42

Code:

url:  'file.php'
Umjesto file.php staviš putanju do ajax controllera. Npr. https://host.com/ajax-handler/signup.
Ovo je pretpostavka da se u laravelu route hendlaju na način {controller}/{method}. Pretpostavljam da kužiš što hoću reći. :)

U ajax controlleru "odradiš" POST i obradiš sve što ti treba, pa vratiš podatke npr. u ovom obliku:
PHP kod:

<?php 
public function signup()
{
    
$data = array('response' => 'error''message' => 'Invalid email address');
    echo 
json_encode($data);
    exit;
}
?>

Nakon toga unutar ajax bloka napraviš nešto poput ovog:

Code:

.complete(function(data) {
    $('.ajax-response).html(data.message).addClass('data.response');
}

Klasu dodaš da u css-u možeš srediti output na temelju poruke. :)

U view fajlu dodaš:
HTML:

<div class="ajax-response"></div>

cdhr 18.09.2017. 19:48

thx na pomoći upalilo je :)


Sva vremena su GMT +2. Sada je 07:54.

Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© 1999-2023 PC Ekspert - Sva prava pridržana ISSN 1334-2940
Ad Management by RedTyger