1

Im trying to send a data containing username, password etc from a HTML form-> ajax -> instance -> oop class file.

But im not sure i have the right approach...

It starts with the form on 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> 

And then it goes trough my script file and ajax

$(document).ready(function () {

console.log('Script loaded...');
$("#btn-reg").on("click", reg);      


// Function for registrate of new users
function reg(newusername, newpassword, newclub) {
    $.post('classCalling.php', {
        newusername: 'newusername',
        newpassword: 'newpassword',
        newclub: 'newclub'
    });
};
});

And then my data is going to a page, classCalling.php where i instance my class

    ?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);         


 ?>

And finaly my OOP Class, but im not getting this far

 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>";
            }
}

Im getting these errors Notice: Undefined index: newusername in /Applications/MAMP/htdocs/web 2.0/projektet/classCalling.php on line 13

mackeemackee
  • 161
  • 4
  • 16

5 Answers5

6

I think the error is because ur not passing any arguments to reg.

Try passing the form values to reg function it ll be fine

Vijaykrish93
  • 150
  • 8
  • 1
    Agree, calling "reg" with no arguments... btn.on('click',reg) part – Julo0sS Mar 01 '16 at 12:02
  • If He can check post request, open dev console on browser and open tab network, click on script and open parameters tab, in new window. – Naumov Mar 01 '16 at 12:02
  • This is not actually causing this issue of `Undefined Index`. Atleast he should have got hard-coded value 'newusername' – Thanga Mar 01 '16 at 12:13
1

problem with your code is that you are not fetching value from form.. You don't need to put the code in form tag. Use this form and Script, hope it may help.....

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

and script:

$(document).ready(function () {

console.log('Script loaded...');
$("#btn-reg").on("click", reg);      

var newusername=$("#newusername").val();
var newpassword=$("#newpassword").val();
var newclub=$("#club").val();

function reg() {
$.post('classCalling.php', {
    newusername: newusername,
    newpassword: newpassword,
    newclub: newclub
});
};
});
Sonu Bamniya
  • 1,095
  • 1
  • 13
  • 29
0

Try this function:

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);
});
}
madalinivascu
  • 32,064
  • 4
  • 39
  • 55
0

Try out this

    $(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);
         }

      });
     });

});

And adjust your newUsers() method into

 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>";
            }
}
Little Phild
  • 785
  • 1
  • 6
  • 17
0

To avoid error in your classCalling.php file always check if the POST value you are trying to get is set or not. check below code

<?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.
}
Ronak Rathod
  • 430
  • 4
  • 14