2

So I have a simple form where I ask the user some info to register and make an account, this is the part of the form I am using to achieve that:

<form class="login-form2" action="index.html">    
   <div class="center sliding"><img style='width:20%; margin: 42%; margin-top: 8%; margin-bottom: 0%;'src="./images/logoTemporal.png"></div>    
    <div class="login-wrap">
        <p class="login-img"><i class="icon_lock_alt"></i></p>

        <!-- Name -->
        <div class="input-group">
          <span class="input-group-addon"><i class="icon_profile"></i></span>
          <input type="text" id="Name" name="Name" class="form-control" placeholder="Name or Name.Corp" autofocus>
        </div>

        <!-- Email -->
        <div class="input-group">
            <span class="input-group-addon"><i class="icon_key_alt"></i></span>
            <input type="email" id="Email" name="Email" class="form-control" placeholder="Email">
        </div>


        <!-- Passwrod -->
        <div class="input-group">
            <span class="input-group-addon"><i class="icon_key_alt"></i></span>
            <input type="password" id="Password" name="Password" class="form-control" placeholder="Password">
        </div>

        <!-- Confirm password -->
        <div class="input-group">
            <span class="input-group-addon"><i class="icon_key_alt"></i></span>
            <input type="password" class="form-control" placeholder="Confirm Password">
        </div>

        <!-- Tipo -->
        <div class="item-content input-group-addon">
          <div class="item-media"><i class="icon f7-icons">Tipo Usuario</i></div>
          <br>
            <div class="item-input">
              <select id="Tipo" name="Tipo">>
                <option value="0" selected>Empresa</option>
                <option value="1">Usuario</option>
              </select>
            </div>
        </div>
       <br>

       <!-- Button-->
        <!-- <a class="btn btn-primary btn-lg btn-block" href="register.html">Register</a> -->
        <p> <input type="button" class="btn btn-primary btn-lg btn-block" id="signup" value="Send"/></p>
    </div>
  </form>

I am trying to get the values into some variables in a jc, but for some reason I am not getting them:

$(document).ready(function() {
$("#signup").click(function() {

alert("Im in the function");//this is the only alert showing when I test the page...

//From here on it is not working
var Name = $('#Name').val();
var Email = $('#Email').val();
var Password = $('#Password').val();
var Tipo = $('#Tipo').val();

if (Name == '' || Email == '' || Password == '' || Tipo == '') 
{ alert("please complete the information"); }
else {
    // AJAX code to submit form.
     $.ajax({

     type: "POST",
     url: 'dummy url',
     crossDomain: true,
     beforeSend: function(){ $mobile.loading('show')},
     complete: function(){ $mobile.loading('hide')},
     data: ({Name: Name, Email: Email, Password: Password, Tipo: Tipo}),
     dataType: 'json',
     success: function(html){
                              alert("Thank you for Registering with us! you 
 can login now"); 
                            },

      error: function(html){ 
                        alert("Not Working"); 
                      }

     });
  }//else end
});
});

I am still trying to learn many things here but what I need to know is why the variables are not getting the values from the form, could be something dumb but I just cant see it... Would appreciate some help...

EDIT: Adding my php code, now I get an error when trying to send the data to my host´s php using JSON, the javascript prints the "not working" alert, what I think is that my php is not really getting the json so its not working but im not 100% sure so here is my php code:

<?php 

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, DELETE, OPTIONS');

require 'data/DataCollector.php';

$varData = json_decode($data);
$Name = $varData->Name;
$Email = $varData->Email;
$Password = $varData->Password;
$Tipo = $varData->Tipo;



$database = new DataCollector([
    'database_name' => 'dummy url',
     'server' => 'dummy server',
     'username' => 'dummy username',
     'password' => 'dummy password',
     'charset' => 'utf8',
    ]);

if($_POST){

    $database->insert("Usuario", [
    "nombre" => $_POST[$Name],
    "email" =>  $_POST[$Email],
"password" => $_POST[$Password],
    "tipoUsuario" => $_POST[$Tipo]
]);
}

?>

I have never used JSON and I am trying to learn about it, there is probably A LOT of issues with my code but any help will be very much appreciated! thanks!

6
  • 1
    An errors in the console? Commented Oct 4, 2017 at 22:39
  • A silly question... you did click OK and close the first alert, right? There must be some error. Try alert($('#Name').val()) ... what do you get? Commented Oct 4, 2017 at 23:01
  • I had some, they had to do nothing with the issue I had and now I dont have that problems anymore at least... Commented Oct 4, 2017 at 23:02
  • That is working now, my issue now is sending the JSON to my host´s php, but i think that my php is not expecting a json so I am trying to fix that and see what i get... Commented Oct 4, 2017 at 23:09
  • Can you include your PHP code? Commented Oct 4, 2017 at 23:39

1 Answer 1

1

your dictionary does not seem to be correct.

try this:

data: {"Name": Name, "Email": Email, "Password": Password, "Tipo": Tipo}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks haha, that just fixed one of the many issues I have, now it is not sending anything to my php at the host but not thanks to the error you just helped me with!
I have no knowledge of php, but someone else could help if you show the php code that handles the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.