简体   繁体   English

科尔多瓦jQuery Ajax发布到服务器的PHP文件

[英]Cordova jquery ajax post to server php file

I want to compare input value of the input in the cordova app to an table in my database. 我想将cordova应用程序中输入的输入值与数据库中的表进行比较。

This is my jquery ajax post: 这是我的jQuery ajax帖子:

$('.postToken').click(function(){


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

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

and this is my get_schema.php on server: 这是我在服务器上的get_schema.php:

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

    $stringData = $_POST['dataString'];
    ini_set('display_errors', 1);

    var_dump($stringData);
?>

But it returns me null I first type in value data in my cordova app and then refresh the php page but keeps returning me null . 但是它返回我为null我首先在我的cordova应用程序中输入了值数据,然后刷新了php页面,但始终返回null Could somebody help me out on this issue? 有人可以帮我解决这个问题吗?

Edit: 编辑:

You need to see if the value exists in the database table like this 您需要像这样查看数据库表中是否存在该值

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

$stringData = $_POST['dataString'];
ini_set('display_errors', 1);

$checkToken = "SELECT * FROM Exercises WHERE YOUR_FIELD_NAME = ('$stringData')";
$checkResult = mysqli_query($conn, $checkToken);

if (mysqli_num_rows($checkResult) > 0) {

echo "Result already exists";

} else {

echo "0 results";

}

?>

Replace YOUR_FIELD_NAME for the column name you use for tokens. 将YOUR_FIELD_NAME替换为用于令牌的列名。

The javascript I have already provided will console log wether or not the token exists. 我已经提供的javascript将会控制台日志,无论令牌是否存在。

Original 原版的

When you refresh the php file $stringData will be undefined. 当您刷新PHP文件$ stringData时,它将是未定义的。

Try this. 尝试这个。

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

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

This should console log the result of the var_dump. 这应该控制台记录var_dump的结果。

If not change this line of the php file. 如果没有更改此行的php文件。

var_dump($stringData);

To. 至。

echo var_dump($stringData);

You cannot just use ajax in your cordova app because of cross domain issues. 由于跨域问题,您不能仅在cordova应用中使用ajax。 Either you have to setup your server or php to get it or you can use jsonp . 您必须设置服务器或php才能获取它,也可以使用jsonp

For ajax solution you can try this on your php: 对于ajax解决方案,您可以在您的php上尝试:

<?php
    header("Access-Control-Allow-Origin: " . $_SERVER["HTTP_ORIGIN"]);
    header("Access-Control-Allow-Methods: POST");
    header("Access-Control-Allow-Credentials: true");
    header("Access-Control-Allow-Headers: origin, referer, content-type");
    header("Access-Control-Max-Age: 1728000");

    if(strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS'){
        header("Content-Type: text/plain");
        header("Content-Length: 0");
        http_response_code(204);
        exit;
    }

include("connect.php");

$stringData = $_POST['dataString'];
ini_set('display_errors', 1);

var_dump($stringData);
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM