Sireini
Sireini

Reputation: 4262

Cordova - jquery ajax post to php on server

I want to create an token based system, where I want to push the value of an input field to my server side php file to insert it into my database.

Cordova html:

<input type="text" class="valueToken" name="usertoken" >
<button class="postToken">PostToken</button>

Cordova js:

$('.postToken').click(function(){
  console.log($('.valueToken').val());
  var tokenValue = $('.valueToken').val();

$.ajax({
    type: "POST",
    url: "http://domainName.com/fysioWebapp/php/get_token.php",
    data: { 'dataString': tokenValue },
    cache: false,
    success: function(){
        alert("Order Submitted");
    }
});

});

And here is my serverside php:

<?php
include("connect.php");

$stringData = $_POST['tokenValue']; 
echo $stringData

$insertToken = "INSERT INTO User_Token VALUES ('$stringData')";
$tokenresult = mysqli_query($conn, $sql);

 if($tokenresult){
    echo "Successful";
}else {
        echo("Error description: " . mysqli_error($conn));
}

?>

But it is not posting the data to my get_token.php file what am I missing here?

With this as error on the cordova side: POST http://domainName.com/fysioWebapp/php/get_token.php 500 (Internal Server Error)

Upvotes: 1

Views: 1015

Answers (2)

L Balsdon
L Balsdon

Reputation: 995

Edit:

500 Internal Server Error will be shown if your php code has fatal errors like syntax errors and error displaying is switched off.

In your php file try adding:

ini_set('display_errors', 1);

In .htaccess file add:

php_flag display_errors 1

Found a similar answer when looking how to debug 500 errors in PHP.

Original:

Try this code.

JS

$('.postToken').click(function(){
   console.log($('.valueToken').val());
   var tokenValue = $('.valueToken').val();

   $.ajax({
       type: "POST",
       url:  "http://domainName.com/fysioWebapp/php/get_token.php",
       data: { dataString: tokenValue },
       cache: false,
       success: function(){
          alert("Order Submitted");
          }
   });
});

PHP

<?php
include("connect.php");

$stringData = $_POST['dataString']; 

echo $stringData

$insertToken = "INSERT INTO User_Token VALUES ('$stringData')";
$tokenresult = mysqli_query($conn, $insertToken);

if($tokenresult){
    echo "Successful";
}else {
    echo "Error description: " .  (mysqli_error($conn));
}
?>

Upvotes: 1

OldPadawan
OldPadawan

Reputation: 1251

I have put some names/values almost identical to yours, but I make a little difference with Ajax because I'm used to do that way... you'll have to adapt to your needs :) EDIT: you did not get the proper $vars in get_token.php because using wrong name.

<script type="text/javascript">

$(document).ready(function(){

$("#submit").click(function(e){
   e.preventDefault();

   console.log($('.valueToken').val());
   var tokenValue = $('.valueToken').val();

    $.ajax({
        method: "POST",
        url: "get_token.php",
        data: "token="+tokenValue,
        success: function(html){ alert(html); }
    });
  });
});
</script>

<form action="#" id="form" method="post">
<input type="text" class="valueToken" name="usertoken" >
<input type="submit" class="postToken" id="submit" value="PostToken" />
</form>

/-- get_token.php --/

<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

$token = $_POST['token'];

echo"[ $token ]";

?>

Upvotes: 0

Related Questions