Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试使用ajax将数据发送到OOP类_Javascript_Php_Jquery_Ajax_Oop - Fatal编程技术网

Javascript 尝试使用ajax将数据发送到OOP类

Javascript 尝试使用ajax将数据发送到OOP类,javascript,php,jquery,ajax,oop,Javascript,Php,Jquery,Ajax,Oop,我试图从HTML表单->ajax->实例->oop类文件发送包含用户名、密码等的数据 但是我不确定我有没有正确的方法 它以index.php上的表单开始 <!-- Formular for signing up --> <form method="post"> <div class="form-group"> <label> Username </label> <input type="

我试图从HTML表单->ajax->实例->oop类文件发送包含用户名、密码等的数据

但是我不确定我有没有正确的方法

它以index.php上的表单开始

 <!-- Formular for signing up -->
 <form method="post">
    <div class="form-group">
        <label> Username </label>
        <input type="text" class="form-control" name="newusername"> 
    </div>    

    <div class="form-group">
        <label> Password </label>
        <input type="password" class="form-control" name="newpassword"> 
    </div>

    <div class="form-group">
        <label> Your club </label>
        <input type="text" class="form-control" name="newclub"> 
    </div>   

   <input type="button" id="btn-reg" class="btn btn-success" value="Sign up!">

</form> 
然后,我的数据将进入一个页面,classCalling.php,在那里我实例化了我的类

    ?php

     include("class/userClass.php");
     include("class/pagesClass.php");



                // Creating instance of the class userClass.php
                 $user = new User();




                // Defining variables
                $newusername = $_POST['newusername'];
                $newpassword = $_POST['newpassword'];
                $newname = $_POST['newclub'];

                // Password hash
                $hashpassword = sha1($newpassword);

                $user->newUsers($newusername, $hashpassword, $newname);         


 ?>
最后是我的OOP课程,但我还没走到这一步

 public function newUsers($newusername, $newpassword, $newclub) {

        // Using prepared statement to prevent mysql injections.
        $stmt = $this->db->prepare("INSERT INTOusers(username,password, club)VALUES(?, ?, ?);");
        $stmt->bind_param("sss", $newusername, $newpassword, $newclub);

        if($stmt->execute()) {
            echo "<h3 class='usercreated'>Användare skapad</h3>";
            } else {
                echo "<h3 class='usercreated'> Gick ej att skapa användare</h3>";
            }
}
公共函数newUsers($newusername、$newpassword、$newclub){
//使用prepared语句防止mysql注入。
$stmt=$this->db->prepare(“插入用户(用户名、密码、俱乐部)值(?,?);”;
$stmt->bind_参数(“sss”、$newusername、$newpassword、$newclub);
如果($stmt->execute()){
回声“Användare skapad”;
}否则{
回声“Gick ej att skapa användare”;
}
}

我收到这些错误注意:未定义的索引:newusername in/Applications/MAMP/htdocs/web 2.0/projektet/classCalling.php第13行

我认为错误是因为你没有将任何参数传递给reg


尝试将表单值传递给reg函数,这样就可以了

我认为错误是因为您没有向reg传递任何参数


尝试将表单值传递给reg函数。这样就可以了。

代码的问题是您没有从表单中获取值。。 您不需要将代码放入表单标记中。 使用此表单和脚本,希望对您有所帮助

<div class="form-group">
    <label> Username </label>
    <input type="text" class="form-control" name="newusername" id="username"> 
</div>    

<div class="form-group">
    <label> Password </label>
    <input type="password" class="form-control" name="newpassword" id="password"> 
</div>

<div class="form-group">
    <label> Your club </label>
    <input type="text" class="form-control" name="newclub" id="club"> 
</div>   

代码的问题是您没有从表单中获取值。。 您不需要将代码放入表单标记中。 使用此表单和脚本,希望对您有所帮助

<div class="form-group">
    <label> Username </label>
    <input type="text" class="form-control" name="newusername" id="username"> 
</div>    

<div class="form-group">
    <label> Password </label>
    <input type="password" class="form-control" name="newpassword" id="password"> 
</div>

<div class="form-group">
    <label> Your club </label>
    <input type="text" class="form-control" name="newclub" id="club"> 
</div>   
尝试此功能:

function reg() {
var newusername=$(this).val();
var newpassword=$(this).val();
var newclub=$(this).val();

$.post('classCalling.php', {
    newusername: newusername,
    newpassword: newpassword,
    newclub: newclub
},function(data){
 console.log(data);
});
}
尝试此功能:

function reg() {
var newusername=$(this).val();
var newpassword=$(this).val();
var newclub=$(this).val();

$.post('classCalling.php', {
    newusername: newusername,
    newpassword: newpassword,
    newclub: newclub
},function(data){
 console.log(data);
});
}
试试这个

    $(document).ready(function () {
      console.log('Script loaded...');

      $("#btn-reg").click(function(){
        var newusername = $("#username").val();
        var newpassword = $("#newpassword").val();
        var newclub = $("#newclub").val();

        $.ajax({
          method: "POST",
          url: "classCalling.php",
          data: { newusername: newusername,newpassword: newpassword,newclub: newclub },
         success:function(data){
            alert(data);
         }

      });
     });

});
并将
newUsers()
方法调整为

 public function newUsers($newusername, $newpassword, $newclub) {

        // Using prepared statement to prevent mysql injections.
        $stmt = $this->db->prepare("INSERT INTO  users(username,password, club)VALUES(?, ?, ?);");
        $stmt->bind_param("sss", $newusername, $newpassword, $newclub);

        if($stmt->execute()) {
            echo "<h3 class='usercreated'>Användare skapad</h3>";
            } else {
                echo "<h3 class='usercreated'> Gick ej att skapa användare</h3>";
            }
}
公共函数newUsers($newusername、$newpassword、$newclub){
//使用prepared语句防止mysql注入。
$stmt=$this->db->prepare(“插入用户(用户名、密码、俱乐部)值(?,?);”;
$stmt->bind_参数(“sss”、$newusername、$newpassword、$newclub);
如果($stmt->execute()){
回声“Användare skapad”;
}否则{
回声“Gick ej att skapa användare”;
}
}
试试这个

    $(document).ready(function () {
      console.log('Script loaded...');

      $("#btn-reg").click(function(){
        var newusername = $("#username").val();
        var newpassword = $("#newpassword").val();
        var newclub = $("#newclub").val();

        $.ajax({
          method: "POST",
          url: "classCalling.php",
          data: { newusername: newusername,newpassword: newpassword,newclub: newclub },
         success:function(data){
            alert(data);
         }

      });
     });

});
并将
newUsers()
方法调整为

 public function newUsers($newusername, $newpassword, $newclub) {

        // Using prepared statement to prevent mysql injections.
        $stmt = $this->db->prepare("INSERT INTO  users(username,password, club)VALUES(?, ?, ?);");
        $stmt->bind_param("sss", $newusername, $newpassword, $newclub);

        if($stmt->execute()) {
            echo "<h3 class='usercreated'>Användare skapad</h3>";
            } else {
                echo "<h3 class='usercreated'> Gick ej att skapa användare</h3>";
            }
}
公共函数newUsers($newusername、$newpassword、$newclub){
//使用prepared语句防止mysql注入。
$stmt=$this->db->prepare(“插入用户(用户名、密码、俱乐部)值(?,?);”;
$stmt->bind_参数(“sss”、$newusername、$newpassword、$newclub);
如果($stmt->execute()){
回声“Användare skapad”;
}否则{
回声“Gick ej att skapa användare”;
}
}

为避免类调用.php文件出错,请始终检查您试图获取的POST值是否已设置。 检查下面的代码

<?php
include("class/userClass.php");
include("class/pagesClass.php");

// Creating instance of the class userClass.php
$user = new User();

if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['newclub'])){

    $username = $_POST['username'];
    $password = $_POST['password'];
    $newclub = $_POST['newclub'];

    $hashpassword = sha1($newpassword);
    $user->newUsers($newusername, $hashpassword, $newname); 

}else{
    // handle your request when POST parameters are missing.
}

为避免类调用.php文件出错,请始终检查您试图获取的POST值是否已设置。
检查下面的代码

<?php
include("class/userClass.php");
include("class/pagesClass.php");

// Creating instance of the class userClass.php
$user = new User();

if(isset($_POST['username']) && isset($_POST['password']) && isset($_POST['newclub'])){

    $username = $_POST['username'];
    $password = $_POST['password'];
    $newclub = $_POST['newclub'];

    $hashpassword = sha1($newpassword);
    $user->newUsers($newusername, $hashpassword, $newname); 

}else{
    // handle your request when POST parameters are missing.
}

阅读此内容:您还遗漏了一个问题:“插入用户(用户名,pa)”此问题与OOP无关。您必须研究数据流。将
插入用户(
更改为
插入用户(
尝试使用
var\u dump($\u POST)打印$\u POST)
在你的
classCalling.php
中读到:你也遗漏了一个问题:“插入用户(用户名,pa)”这个问题与OOP无关。你必须研究数据流。将
插入用户(
改为
插入用户(
尝试使用
变量转储($\u POST)打印$\u POST);
在您的
classCalling.php中
同意,在没有参数的情况下调用“reg”…btn.on('click',reg)如果他可以检查post请求,打开浏览器上的开发控制台并打开选项卡网络,在新窗口中单击脚本并打开参数选项卡。这实际上并不是导致
未定义索引问题的原因。
。至少他应该得到硬编码值“newusername”同意,在没有参数的情况下调用“reg”…btn.on('click',reg')如果他可以检查post请求,在浏览器上打开开发控制台并打开网络选项卡,在新窗口中单击脚本并打开参数选项卡。这实际上并不是导致
未定义索引问题的原因。至少他应该有硬编码值“newusername”