1

I'm trying to pass a string argument param in the form

   number,number,number.... for example 1,3,5,4,8,2

and split it up into an array of numbers and then carry out the checkuserbets function from the Check_Bets.php file on each of the numbers and return the results as an array. However, when I run this in a REST client, I get

{"error":true,"error_msg":"the response is null!"} 

as if I didn't pass the proper parameter or didn't use the POST method. This is what I am passing : enter image description here

Check_Bets_Handler.php

<?php
if (isset($_POST['param'])) {
    // get tag
   $param= $_POST['param'];
   $id = explode(",",$param);
   $arrlength = count($id);
    // include db handler
     require_once 'include/Check_Bets.php';


    $db = new Check_Bets();
    $response["bet"] = array();


   for($x = 0; $x < $arrlength; $x++) {

    $result= $db->checkuserbets($id[$x]);
    array_push($response["bet"], $result);
}
    echo json_encode($response);
}
else {
$response["error"] = TRUE;
    $response["error_msg"] = "the response is null!";
    echo json_encode($response);
}
?>
 

Check_Bets.php

<?php

class Check_Bets {
  
 

   function __construct() {

	require_once 'DB_Connect.php';
	$this->db = new DB_Connect();

	$this->db->connect();


}



function __destruct() {
   
  }

  public function checkuserbets($id) {

   $conn=mysqli_connect("****", "******", "****","****");

   $result = mysqli_query($conn,"SELECT Result FROM gamelist WHERE gid = '$id'");
 $no_of_rows = mysqli_num_rows($result);
     
    if ($no_of_rows > 0) {

 return mysqli_fetch_array($result,MYSQLI_ASSOC);
    
}
}
}
?>

enter image description here

6
  • See what you get when you do a print_r for your post variables. Put this directly under your opening PHP tag: print "<PRE><FONT COLOR=ORANGE>"; print_r($_POST); print "</FONT></PRE>"; exit; Commented Jun 1, 2015 at 17:46
  • also better past the code of the form not the interface. Commented Jun 1, 2015 at 17:48
  • Have you looked in the network tab in the browser debugger to see that the variable is being passed? Commented Jun 1, 2015 at 17:48
  • @Quixrick I uploaded the result as an image to the bottom of the question Commented Jun 1, 2015 at 17:52
  • You need to add the form code, as there isn't enough information for us to help properly. Commented Jun 1, 2015 at 17:54

2 Answers 2

1

Okay, I just ran a test script of this and it seems to work as expected. It seems that the problem you are having is that if (isset($_POST['param'])) { is not catching. However, with the code that you screenshotted, that doesn't make sense to me.

I didn't have a database set up for this, so I had to make my own function, but here is the code that I ran and it works fine for me.

<?php

// SET SOME INFORMATION
$_POST = array("param" => '3,4,5,6,7,8,9');

if (isset($_POST['param'])) {

    $param = $_POST['param'];
    print '<br>PARAM: '.$param;

    $id = explode(",",$param);
    print "<pre><font color=blue>"; print_r($id); print "</font></pre>";

    $arrlength = count($id);
    // include db handler
    //require_once 'include/Check_Bets.php'; // DON'T HAVE THIS


    //$db = new Check_Bets();
    $response["bet"] = array();


    for($x = 0; $x < $arrlength; $x++) {

        //$result= $db->checkuserbets($id[$x]);
        $result= checkuserbets($id[$x]); // MADE MY OWN FUNCTION BECAUSE I DO NOT HAVE A DATABASE
        array_push($response["bet"], $result);
    }

    echo json_encode($response);
}
else {
    $response["error"] = TRUE;
    $response["error_msg"] = "the response is null!";
    echo json_encode($response);
}

// DUMMY FUNCTION
function checkuserbets($id) {
    return $id * 5;    
}

This is what it returns:

PARAM: 3,4,5,6,7,8,9
Array
(
    [0] => 3
    [1] => 4
    [2] => 5
    [3] => 6
    [4] => 7
    [5] => 8
    [6] => 9
)
{"bet":[15,20,25,30,35,40,45]}

So if you are getting the $_POST['params'] back when you print_r them, but not in isset, then I have a couple more suggestions. You can try using either empty or not equal to nothing.

// CHECK TO MAKE SURE $_POST['param'] IS NOT EMPTY
if (!empty($_POST['param'])) { 

OR

// CHECK TO MAKE SURE $_POST['param'] IS NOT EQUAL TO NOTHING
if ($_POST['param'] != '') {

Hopefully one of those two will fix it for you.

Here is a working demo

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for the detailed response, I replaced the isset with !empty and it works fine :)
0

Check value can be passed in url.If value cannot passed in the url You did the mistake in posting the values

1 Comment

How do I check that? If you look at the bottom of the question you can see what the php file gets from the REST

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.