0

Im so noob at this, been working with PHP and Js for like 4 months, sorry if im making a noob question, also, im a spanish speaker, sorry for english grammar fails you are going to read =[

Basically, this is my problem: In this Php File i have some Vars and VarArrays, i need to send them to another one

//First PHP File - This one Search on DataBase, at the end, i have 5 vars, 2 of them are arrays
<?php
$var1 = '1';
$var2 = '2';
$var3 = '3';

$arr1 = array();
$arr2 = array();

//¿How to json_encode the 5 vars above?//
?>

In this one i need to catch the previus values

//Second PHP File
<?php
$newVar1 = $_POST['var1'];
$newVar2 = $_POST['var2'];
$newVar3 = $_POST['var3'];

$newArr1 = $_POST['arr1'];
$newArr2 = $_POST['arr2'];
?>

I think i have to do something like this, but how should i do it?:

$.ajax({
        type: "POST",
        url: "php/FIRSTFILE.php",
        data: ????,
        dataType: "json",
        success:
                function(respuesta)
                {
                  $('#MainDiv').load('php/SECONDFILE.php', function(data) {
                      $(this).html(data);
                  });
                  $('#MainDivLabelVar1').val(respuesta.¿¿EncodeStuff??);
                }
 });
3
  • How does the code in the php file php/CATBuscar.php send back the data? Note you need to echo out a JSON item. Commented Jan 7, 2015 at 1:37
  • The variables you are sending up do not match what is in the php file. Commented Jan 7, 2015 at 1:38
  • 1. You can't "send" values from PHP to PHP (unless you use curl or similar directly). I presume you are rendering one PHP page, and progressing to the other either by normal POST request or by AJAX? 2. Can we see the output of php/CATBuscar.php with id and CAT parameters? You can get it from the Network panel of your browser's Developer Tools. Commented Jan 7, 2015 at 1:38

3 Answers 3

1

maybe you can encode your data like this

json_encode(array(
         'var1'   =>    '1',
         'var2'   =>    '2',
         'var3'   =>    '3',
         'arr1'   =>     array(),
         'arr2'   =>     array()
          ));
Sign up to request clarification or add additional context in comments.

Comments

0

In this line you are sending values in POST to your php file:

 data: {id: id, CAT: CAT},

So, if you need to receive data like (in file "second.php")

<?php
  $newVar1 = $_POST['var1'];
  $newVar2 = $_POST['var2'];
?>

You should send it by this:

$.ajax({
        type: "POST",
        url: "php/second.php", 
        data: {var1: 'value1', var2: 'value2'}
});

2 Comments

Maybe i shouldnt add the javascript part, is not part of the PHP code, i know that, what i dont know is why ajax with the dataType "json" atribute Its returning "[object object]" as ERROR But the real real problem, is send multiple Vars, some of them are arrays. How to json_encode the 5 vars?
To json_encode your 5 vars you need to make 1 array with 5 variables. So it should looks like json_encode(array('1','2','3',array(),array()))
0

you could deocde the json string into object or array

$js_array = json_encode(array(
         'var1'   =>    '1',
         'var2'   =>    '2',
         'var3'   =>    '3',
         'arr1'   =>     array('a' => 'mobile', 'b' => 'PC'),
         'arr2'   =>     array('price' => 600, 'day' => 7)
          ));

$decode = json_decode($js_array, true); //decode into array

//you could use or seperate something you get back from json decode like this
foreach($decode as $key => $value) {
    if(is_array($value)) {  
        foreach ($value as $k => $v) {
            echo "$k => $v <br />";
        }
    } else {
        echo "$key: $value <br />";     
    }
}

and the output:

var1: 1 
var2: 2 
var3: 3 
a: mobile 
b: PC 
price: 600 
day: 7

Comments

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.