Viewed   103 times

I want to manipulate a JavaScript array in PHP. Is it possible to do something like this?

$.ajax({
       type: "POST",
       url: "tourFinderFunctions.php",
       data: "activitiesArray="+activities,
       success: function() {
            $("#lengthQuestion").fadeOut('slow');
       }
    });

Activities is a single dimensional array like:

var activities = ['Location Zero', 'Location One', 'Location Two'];

The script does not complete when I try this... How can I fix it?

 Answers

5
data: { activitiesArray: activities },

That's it! Now you can access it in PHP:

<?php $myArray = $_REQUEST['activitiesArray']; ?>
Wednesday, October 19, 2022
1
<script>

function getSummary(id)
{
   $.ajax({

     type: "GET",
     url: 'Your URL',
     data: "id=" + id, // appears as $_GET['id'] @ your backend side
     success: function(data) {
           // data is ur summary
          $('#summary').html(data);
     }

   });

}
</script>

And add onclick event in your lists

<a onclick="getSummary('1')">View Text</a>
<div id="#summary">This text will be replaced when the onclick event (link is clicked) is triggered.</div>
Thursday, August 18, 2022
3

Got it!

To properly access the JavaScrtipt objects in PHP I need to JSON.stringify them when pushing on the array. Then, on PHP, json_decode them to a PHP object, and access their properties with the '->' operator.

The final solution is as follows:

<?php 
include 'ChromePhp.php';

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

    $newUsers = $_POST['newUsers'];

    foreach ($newUsers as $user) {
        # code...
        $usr = json_decode($user);
        ChromePhp::log("Nome: " . $usr->nome . " - Idade: " . $usr->idade);
    }

} else { ?>

<html>
<body>

    <script src="js/jquery-2.0.3.min.js"></script>
    <script type="text/javascript">
        //var newUsersObj = {};
        var newUsers = [];

        newUser = {};
        newUser['nome'] = 'alvaro';
        newUser['idade'] = '34';
        newUsers.push(JSON.stringify(newUser));

        newUser1 = {};
        newUser1['nome'] = 'bia';
        newUser1['idade'] = '7';
        newUsers.push(JSON.stringify(newUser1));

        newUser2 = {};
        newUser2['nome'] = 'alice';
        newUser2['idade'] = '2';
        newUsers.push(JSON.stringify(newUser2));

        $.ajax({
            url: "testcookie.php",
            type: "POST",
            data: {
                'newUsers[]': newUsers
            },
            success: function () {

            },
            error: function () {

            }
        });

    </script>
</body>
</html>
<?php } ?>
Thursday, December 22, 2022
 
jonvd
 
2

you problem that selector ('#services') takes only first input value. You should remove id and just serialize form as below.

If all you need to pass is all values from form you can use

data: $('form#my-form').serialize() // this code puts all of the inputs into passable and readable for PHP, way.

And then in $_POST['service'] will be an array of inputs values.

For example:

<form action="save.php" method="post" id="services">
    <input type="text" name="service[0]" value="1st Service" />
    <input type="text" name="service[1]" value="2nd Service" />
    <input type="text" name="service[2]" value="3rd Service" />
    <input type="text" name="service[..]" value=".. Service" />
    <input type="text" name="service[N]" value="N Service" />
</form>

In your JS:

$.post($('form#services').attr('action'), $('form#services').serialize(), function(response) {});

And then in save.php you can get an array in $_POST:

var_dump($_POST['service']);

Hope that's is exactly what you need.

Monday, October 31, 2022
 
r2d2
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :