简体   繁体   English

如何使用HTML表单输入获取json_decode和OOP语句的工作

[英]How do I get json_decode working in and OOP statement using HTML form input

I have a simple HTML form using json_encode to send a PHP variable (via value=) to a file and then using json_decode to extract and echo the results. 我有一个简单的HTML表单,使用json_encode将PHP变量(通过value =)发送到文件,然后使用json_decode提取并回显结果。 I am battling to get the correct syntax or method to decode the json_decode in the object environment, required by Opencart. 我正在努力获取正确的语法或方法,以在Opencart所需的对象环境中对json_decode进行解码。 It works fine when I use the procedural method below. 当我使用下面的过程方法时,它工作正常。

I have attempted various syntax changes, but they return errors, so I believe that the syntax is incorrect, or my method cannot be done this way. 我尝试了各种语法更改,但是它们返回错误,因此我认为语法不正确,否则我的方法无法通过这种方式完成。 1st Code is the Procedural method that returns the correct result. 第一个代码是返回正确结果的过程方法。 2nd code is the OOP method which fails. 第二代码是失败的OOP方法。 - (assume syntax is wrong. -(假设语法错误。

Code Working:-
  <form id="myForm" action="radio_result.php" method="post" 
  enctype="multipart/form-data">
  <input type="radio" name="service" value="<?php echo 
  htmlentities(json_encode($service_onx));?>"> ONX
//additional code excluded.
radio_result.php // not all code shown
  <?php   
    if(!empty($_POST['service'])) {
    $service = json_decode($_POST['service'], true);
    print_r($service);

Code failing:-
    <form id="myForm" action="index.php?route=checkout/checkout" 
    method="post" enctype="multipart/form-data">
    <input type="radio" name="service" value="<?php echo 
    htmlentities(json_encode($service_onx));?>"> ONX
    checkout.php // not all code shown
    $this->session->data['service'] = (isset($this->request- 
   >post(json_decode(['service'])))) ? $this->request->post['service'] : 
"not_set";
    $data['onx'] = $this->session->data['service'][0];
    $data['eta'] = $this->session->data['service'][1];
Error result:-
Fatal error: Cannot use isset() on the result of an expression (you can 
use "null !== expression" instead) in 
    C:\wamp64\www\catalog\controller\checkout\checkout.php on line 101


I would like to get the json_decode working in the Opencart framework 
checkout.php so that I can use the reult further.

If I understand you correctly, you need an object?If so you can first do 如果我对您的理解正确,那么您需要一个对象吗?

$service = json_decode($_POST['service'], true);

And than cast this array as an object: 然后将此数组转换为对象:

$serviceObject = (object) $service;

And you will have an object. 您将拥有一个对象。 Try it out. 试试看。

Had a similar problem. 遇到类似的问题。 Solved it by also encoding/decoding on base64. 通过在base64上进行编码/解码来解决该问题。 base64_encode(json_encode($string)) and the json_decode(base64_decode($string)) base64_encode(json_encode($string))json_decode(base64_decode($string))

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

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