need help w/json encode

Hi, I’ve done a lot of searching and thus far have not found the resolution to my code error: Warning: json_decode() expects parameter
1 to be string, array given in C:\xampp\htdocs\testing…any help?
[php]<?php
function _NewDB()
{
try
{
$databaseName = “root”;$databaseUser = “homedb”;$databasePass = “cookie”;
$dbh = new PDO(‘mysql:host=localhost;dbname=’.$databaseName, $databaseUser, $databasePass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{error_log("PDOException: " . $e);return false;}
return $dbh;
}
// End connection stuff
$dbh = _NewDB();
$fieldList = array(“purpose”, “number1”, “opvalue”, “number2”, “total” ); if(!$_POST) exit(“No data submitted via POST!”);

  $data = json_decode($_POST, true);  [b]  // *** error line ***

[/b]
foreach($fieldList as $field)
if(!$data[$field]) exit(“No data submitted for '” . $field . “’!”);
$stmt = $dbh->prepare(“INSERT INTO calculator (purpose, number1, opvalue, number2, total) VALUES(:number1, :number2, :total, :opvalue, :purpose)”);
$stmt->execute($_POST);
echo ‘Sucessfully added a new row to the calculator table!’;
// Below pulling rows/results from the table with PDO
//$stmt = $dbh->prepare(“SELECT * FROM calculator”);
//$stmt->execute();
//$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
header( “refresh:3;url=‘http://localhost/testing/pdocalc.html’”);
?>

    [/php]

You have written json_decode instead of encode in the code. json_decode expects a json string (y)

You’re right, I got lost in my many efforts from googling and forums.
I’m on the bottom rung of a tall ladder.
Following is result of: $data = json_encode($_POST, true);
var_dump(file_get_contents(“php://input”));

string(9) “ReadOut=4”

Warning: Illegal string offset 'purpose' in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset ‘number1’ in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset ‘opvalue’ in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset ‘number2’ in C:\xampp\htdocs\testing\pdocalc.php on line 23

Warning: Illegal string offset ‘total’ in C:\xampp\htdocs\testing\pdocalc.php on line 23

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\testing\pdocalc.php on line 24

I don’t understand the “string(9) ReadOut=4”.
Following is the origin code:

[code]<!doctype html>

Calculator Insert

input { text-align: center; }


    	<form name="Keypad" action="http://localhost/testing/pdocalc.php" method="post">
    		<table border="2" width="50" height="60" cellpadding="1" cellspacing="5" style="margin: 0 auto;">
    			<tr>
    				<td colspan="3" align="middle">
    					<input name="ReadOut" type="Text" size="24" value="0" width="100%">
    				</td>
    				<td></td>
    				<td>
    					<input name="btnClear" type="Button" value=" C " onclick="Clear()" />
    				</td>
    				<td>
    					<input name="btnClearEntry" type="Button" value=" CE " onclick="ClearEntry()" />
    				</td>
    			</tr>
    			<tr>
    				<td>
    					<input name="btnSeven" type="Button" value=" 7 " onclick="NumPressed(7)" />
    				</td>
    				<td>
    					<input name="btnEight" type="Button" value=" 8 " onclick="NumPressed(8)" />
    				</td>
    				<td>
    					<input name="btnNine" type="Button" value=" 9 " onclick="NumPressed(9)" />
    				</td>
    				<td></td>
    				<td>
    					<input name="btnNeg" type="Button" value=" +/- " onclick="Neg()" />
    				</td>
    				<td>
    					<input name="btnPercent" type="Button" value=" % " onclick="Percent()" />
    				</td>
    			</tr>
    			<tr>
    				<td>
    					<input name="btnFour" type="Button" value=" 4 " onclick="NumPressed(4)" />
    				</td>
    				<td>
    					<input name="btnFive" type="Button" value=" 5 " onclick="NumPressed(5)" />
    				</td>
    				<td>
    					<input name="btnSix" type="Button" value=" 6 " onclick="NumPressed(6)" />
    				</td>
    				<td></td>
    				<td align="middle">
    					<input name="btnPlus" type="Button" value=" + " onclick="Operation('+')" />
    				</td>
    				<td align="middle">
    					<input name="btnMinus" type="Button" value=" - " onclick="Operation('-')" />
    				</td>
    			</tr>
    			<tr>
    				<td>
    					<input name="btnOne" type="Button" value=" 1 " onclick="NumPressed(1)" />
    				</td>
    				<td>
    					<input name="btnTwo" type="Button" value=" 2 " onclick="NumPressed(2)" />
    				</td>
    				<td>
    					<input name="btnThree" type="Button" value=" 3 " onclick="NumPressed(3)" />
    				</td>
    				<td></td>
    				<td align="middle">
    					<input name="btnMultiply" type="Button" value=" * " onclick="Operation('*')" />
    				</td>
    				<td align="middle">
    					<input name="btnDivide" type="Button" value=" / " onclick="Operation('/')" />
    				</td>
    			</tr>
    			<tr>
    				<td>
    					<input name="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)" />
    				</td>
    				<td>
    					<input name="btnDecimal" type="Button" value=" . " onclick="Decimal()" />
    				</td>
    				<td colspan=3></td>
    				<td>


Print
    	<script>
    		var FKeyPad = document.forms["Keypad"],
    			Accumulate = 0,
    			FlagNewNum = false,
    			PendingOp = ""
    			$data = {
    				"value1": 0,
    				"value2": 0,
    				"total": 0,
    				"op": "",
    				"purpose": ""
    			};
    			
    		function NumPressed(Num) {
    			if(FlagNewNum) {
    				FKeyPad.ReadOut.value = Num;
    				FlagNewNum = false;
    				$data["value2"] = Num;
    			} else {
    				if(FKeyPad.ReadOut.value == "0") FKeyPad.ReadOut.value = Num;
    				else FKeyPad.ReadOut.value += Num;
    				$data["value1"] = Num;
    			}
    		}
    		function Operation(Op) {
    			var Readout = FKeyPad.ReadOut.value;
    			
    			if(Op != "=") $data["op"] = Op;
    			
    			if(FlagNewNum && PendingOp != "=");
    			else {
    				FlagNewNum = true;
    				if('+' == PendingOp)
    					Accumulate += parseFloat(Readout);
    				else if('-' == PendingOp)
    					Accumulate -= parseFloat(Readout);
    				else if('/' == PendingOp)
    					Accumulate /= parseFloat(Readout);
    				else if('*' == PendingOp)
    					Accumulate *= parseFloat(Readout);
    				else
    					Accumulate = parseFloat(Readout);
    					
    				FKeyPad.ReadOut.value = Accumulate;
    				PendingOp = Op;
    				
    				$data["total"] = Accumulate;
    			}
    		}
    		function Decimal() {
    			var curReadOut = FKeyPad.ReadOut.value;
    			if(FlagNewNum) {
    				curReadOut = "0.";
    				FlagNewNum = false;
    			} else {
    				if(curReadOut.indexOf(".") == -1) curReadOut += ".";
    			}
    			FKeyPad.ReadOut.value = curReadOut;
    		}
    		function ClearEntry() {
    			FKeyPad.ReadOut.value = "0";FlagNewNum = true;
    		}
    		function Clear() {
    			Accumulate = 0;
    			PendingOp = "";
    			ClearEntry();
    		}
    		function Neg() {
    			FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1;
    		}
    		function Percent() {
    			FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accumulate);
    		}
    		
    		(function(){
    			document.getElementById("submit").onclick = function() {
    				var xhr = (window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    				if(xhr) {
    					xhr.onreadystatechange = function() {
    						if(xhr.readyState===4 && xhr.status===200) document.getElementById("update").innerHTML = xhr.responseText;
    					}
    					xhr.open("POST", "http://localhost/home/pdocalc.php", false);
    					xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    					xhr.send(JSON.stringify($data));
    				}
    				return false;
    			};
    		})();
    	</script>

    </center></body></html>

[/code]

Sponsor our Newsletter | Privacy Policy | Terms of Service