从HTML获取值到javascript

时间:2022-11-16 07:41:11

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:

我试图将值放入jc中的某些变量,但由于某种原因我没有得到它们:

$(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代码,现在我在尝试使用JSON将数据发送到我的主机的PHP时出现错误,javascript打印“不工作”警报,我认为我的PHP并没有真正得到json所以它不工作,但我不是100%肯定所以这里是我的PHP代码:

<?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!

我从未使用过JSON,而我正在尝试了解它,我的代码可能存在很多问题,但是我们非常感谢任何帮助!谢谢!

1 个解决方案

#1


1  

your dictionary does not seem to be correct.

你的字典似乎不正确。

try this:

尝试这个:

data: {"Name": Name, "Email": Email, "Password": Password, "Tipo": Tipo}

#1


1  

your dictionary does not seem to be correct.

你的字典似乎不正确。

try this:

尝试这个:

data: {"Name": Name, "Email": Email, "Password": Password, "Tipo": Tipo}

相关文章