0

I wrote an Javascript script which should send some Data to an PHP file which would send it to an Database, in the Browser it worked out fine but when i loaded it into android studio and tried it on the phone it doesn't send the data to the database. Does anyone know what i have to change?

Thanks for any answer

This is my JS code:

var name = $("#name").val();
            var nname = $("#nname").val();
            var passwort = $("#passwort").val();
            var email = $("#email").val();
            var telnummer = $("#telnummer").val();
            var gebdatum = $("#gebdatumJ").val()+$("#gebdatumM").val()+$("#gebdatumT").val();
            var region = $("#region").val();
            if ($.trim(name).length >0) {   
$.ajax({
        type: "POST",  //Request type
        url: "http://quyre.de/SignUpSch.N.php",
        data: {
            name: name,
            nname: nname,
            passwort: passwort,
            email: email,
            telnummer: telnummer,
            gebdatum: gebdatum,
            region: region
        },
        cache: false,
        success: function (data) {
            window.location = "http://quyre.de/LoginSch.html";
        }
    })
}

PHP Code:

$pdo = new PDO('mysql:host=host;dbname=name', 'lol', 'dasbestepasswort');

$name = htmlspecialchars($_POST['name'], ENT_QUOTES);
$nname = htmlspecialchars($_POST['nname'], ENT_QUOTES);
$passwort1 = htmlspecialchars($_POST['passwort'], ENT_QUOTES);
$passwort = password_hash($passwort1, PASSWORD_DEFAULT);
$email = htmlspecialchars($_POST['email'], ENT_QUOTES);
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  $Text = "Die Email-Adresse war ungültig";
  $datenzUE = json_encode(['Text' => $Text,'Go' => 0]);
  echo $datenzUE;
    die;
}
$telnummer = htmlspecialchars($_POST['telnummer'], ENT_QUOTES);
$gebdatum = htmlspecialchars($_POST['gebdatum'], ENT_QUOTES);
$region = htmlspecialchars($_POST['region'], ENT_QUOTES);
$gattung = md5( rand(0,1000000) );

$statement = $pdo->prepare("SELECT * FROM LoginSch WHERE eMail LIKE :eMail");
$statement->execute(array(':eMail' => $email));
$num = $statement->rowCount();

if ($num == 0) {
  $statement = $pdo->prepare("INSERT INTO LoginSch(pwd,nam,nnam,eMail,TNummer,GebDatum,Region,Gattung) VALUES(:passwort,:name,:nname,:eMail,:telnummer,:gebdatum,:region,:gattung)");

  $statement->bindParam(':passwort', $passwort, PDO::PARAM_STR);
  $statement->bindParam(':name', $name);
  $statement->bindParam(':nname', $nname);
  $statement->bindParam(':eMail', $email);
  $statement->bindParam(':telnummer', $telnummer);
  $statement->bindParam(':gebdatum', $gebdatum);
  $statement->bindParam(':region', $region);
  $statement->bindParam(':gattung', $gattung);

  $statement->execute();

  $to = "$email";
  $subject = "The subject";
  $txt = "Das ist eine wunderschöne mail alla beste :www.quyre.de/zertify.php?email=$email&hash=$gattung";
  $headers = "From: anmelden@quyre.de";

  mail($to,$subject,$txt,$headers);

  $Text = "Sie haben sich erfolgreich angemeldet";
  $datenzUE = json_encode(['Text' => $Text,'Go' => 1]);
  echo $datenzUE;
}elseif ($num > 0) {
  $Text = "Die Email-Adresse gibt es bereits";
  $datenzUE = json_encode(['Text' => $Text,'Go' => 0]);
  echo $datenzUE;
}
 ?>

new code JS

$(document).ready(function(){
      //Button der Daten an PHP überträgt
        $('#sub').submit(function(event){
          event.preventDefault();
          var formValues = $(this).serialize();
    $.ajax({
    url:"quyre.de/SignUpSch.php",
    method:"POST",
    data:formValues,
    dataType:"JSON",
        success:function(data){
            if(data === 'ok'){
                $('#result').html(data);
            } else {
                $('#result').html(data);
                $('#frm')[0].reset();
            }
        }
    });
});
})
  • This is mostly PHP related question. So please share your PHP code. That will be helpful for others to identify cause. – Shaharia Azam Feb 26 '20 at 21:42
  • @ShahariaAzam However it worked when i tried it in the browser and there is the same php code. – Hole Closer Feb 26 '20 at 21:49
  • Please share your PHP code here. Because your question is related to PHP. – Shaharia Azam Feb 26 '20 at 21:51
  • @ShahariaAzam Ok i made it – Hole Closer Feb 26 '20 at 21:57
  • @Dlk this is all my ajax code ore should i add all the js code – Hole Closer Feb 26 '20 at 22:18
  • I recommend you to use seriliaze method for Ajax! This is my answer for seriliazetion, https://stackoverflow.com/a/59874137/12232340 you don’t need to specify values 1 by one, it will auto load values to php from your form. –  Feb 26 '20 at 22:27
  • @Dlk It doesnt work with your method to put quotation marks on the variables – Hole Closer Feb 26 '20 at 22:38
  • @Dlk I post the new code but when i click the button to get the results nothing happend – Hole Closer Feb 26 '20 at 23:18
  • It should return response in result div Do you get Any error response ? if not! Leave Ajax out and debug php first see here https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php check your error_log file it should tell you what is wrong. I don’t see what is wrong in your codes –  Feb 26 '20 at 23:46
  • Damn! I found `INSERT INTO LoginSch( pwd,nam,nnam,eMail,TNummer,GebDatum,Region,Gattung)` doesn’t match values `VALUES(:passwort,:name,:nname,:eMail,:telnummer,:gebdatum,:region,:gattung)");` –  Feb 26 '20 at 23:50
  • @Dlk what do you want to say with that? – Hole Closer Feb 26 '20 at 23:54
  • Change your insert query like this, `INSERT INTO LoginSch(passwort,name,nname,eMail,telnummer,gebdatum,region,gattung) VALUES(:passwort,:name,:nname,:eMail,:telnummer,:gebdatum,:region,:gattung)");` values should match columns names –  Feb 26 '20 at 23:58
  • make sure if your column names are correct! I mean Example if your column name is psw in database then change passwort to psw in query same for others too. You also need to do that in `$statement->bindParam(':passwort'` –  Feb 27 '20 at 00:07

0 Answers0