0

Hello friends developers,

I need an explanation regarding the interpretation of my php code in jquery.

this is my code in jquery using $_post

 var $this = $(this);
        e.preventDefault();

        $.post('/public/create_account', { data: $this.serialize()}, function (data) {

            if (data.reponse == "ok") {
                $('#signup').modal('hide');
                alert('it's ok');
            }
            else {

            $("#errors").html("<\?php  if (errors) : \?>"+
              '<div class="alert alert-danger alert-dismissable fade in">'+
              '<span style="font-size:16px;line-height:20px;font-weight: 700;"><i class="fa fa-exclamation-triangle"></i>' +
              ' Oops ! errors .</span>'+ 
              ' <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>'+
              ' <ul style="list-style:none;"><br>'+
              " <\?php  foreach (errors as error) : \?>"+
              ' <li>'+
              '  <span class="help-block" style="color:#a94442;"> <i class="fa fa-check"></i>&nbsp;&nbsp;"<\?php  echo error ; \?>"</span>'+
              '   </li>'+
              "   <\?php  endforeach \?>"+
              ' </ul></div>'+
              "    <\?php  endif \?>")
            }

this is my code in php

  $validation = $this->validator->validate($request, [
        'civilité' =>                 v::notEmpty()->intVal()->between(1,2),
        'name' =>                     v::notEmpty()->alpha()->length(1,55),
        'email' =>                    v::notEmpty()->email()->ConfirmEmail(),
        'user_type' =>                v::notEmpty()->intVal()->between(1,2),
        'CodePostal'   =>             v::notEmpty()->intVal(),
        'ville'   =>                  v::notEmpty()->alpha(),
        'password' =>                 v::noWhitespace()->notEmpty()->length(6),
        'password_confirmation' =>    v::noWhitespace()->notEmpty()->ConfirmPassword($request->getParam('password')),
        'phone' =>                    v::notEmpty()->phone(),
        'term' =>                     v::notEmpty()->equals(1),
    ]);


  // failed
    if($validation->failed()){
       $reponse = 'ko';
       //return $response->withRedirect($this->router->pathFor('create_account'));
    }



    if($validation){
        $user = User::create([
                                    'pseudo' =>   htmlentities($request->getParam('name')),
                                    'email' =>     htmlentities($request->getParam('email')),
                                    'telephone' => htmlentities($request->getParam('phone')),
                                    'password' =>  htmlentities(password_hash($request->getParam('password'),PASSWORD_DEFAULT)),
                                    'navigateur' => $_SERVER['HTTP_USER_AGENT'],
                                    'ip' => $_SERVER['REMOTE_ADDR'],  
                                    'date_crea' => Carbon::now(),
                                    'civilite' => htmlentities($request->getParam('civilité')),  
                                    'user_type' => htmlentities($request->getParam('user_type')), 
                                    'code' => FunctionConfig::doGenerateCodeRandom(),
                                    'actif'=> 0,
                                    'codeuser' => FunctionConfig::doGenerateCodeUser()
                            ]);

        $reponse = 'ok';

     }

    echo json_encode(['reponse' => $reponse]);

When submitting my form fails, the php code is not interpreted. Why ??? this is the response in my DOM

enter image description here

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
phpnewbie
  • 1
  • 1
  • how are you running this file? Is it a .js file included in an html/php file? – itsundefined May 23 '17 at 08:36
  • You're *breaking* PHP tags by escaping characters in them. This: `<\?php if (errors) : \?>` won't trigger the PHP engine, *this* will: `` Though I suspect you're misunderstanding *when* the PHP code runs. It won't be in response to the JavaScript, it'll be server-side before the page is even sent to the browser. – David May 23 '17 at 08:38
  • there's a lot that looks wrong here `alert('it's ok');` the last ``` does not have closing partner – Masivuye Cokile May 23 '17 at 08:38
  • 2
    you have (client-side) javascript, trying to parse (server-side) php code – Sam Janssens May 23 '17 at 08:38
  • you don't need this `$("#errors").html("<\?php if (errors) : \?>"+` – Masivuye Cokile May 23 '17 at 08:40
  • 2
    See [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – Masivuye Cokile May 23 '17 at 08:42
  • 1
    PHP runs on the server and it has finished, packed up and gone home for the night before JavaScript even wakes up and starts tinkering on the client. So attempting to generate PHP *from* JavaScript is impossible (AJAX can call PHP though). `$("#errors").html("` tag in the HTML source code. – CD001 May 23 '17 at 08:47

0 Answers0