1

Im hoping you can point me in the right direction.

I have a php page, that includes some HTML markup and some JS/jQuery routines to build an array of 'user choices' based on the 'user input' (checkboxes..etc).

my question is, how can I pass off this (multidimensional) array to PHP, that is in the same page? (ultimately I want to save this data/array to my PHP session)

While looking around, I read about using another (external) .php script to do,, which is NOT what Im after, I'm hoping to do this to the SAME PAGE I'm in... WITHOUT A REFRESH.

will $.post() do this for me? without a page refresh (if we suppress the event or whatever)... and -not- using an external .php script?

I understand PHP runs/executes FIRST... then everything else..
I'm not really trying to get PHP to do anything with the data being sent from JS/AJAX.. outside of save it to the SESSION array..

Ajax seems like it will be needed?

To summarize:

  • 1.) PHP and JS are in/on same page (file)
  • 2.) No page refresh
  • 3.) No external PHP script to do 'anything'.
  • 4.) Trying to get (multidimensional) array to PHP session in same page.
  • 5.) I am trying to 'update' the PHP SESSION array each time a user 'clicks' on a checkbox.

I have read a little on using AJAX to post to the same page with the URL var left empty/blank?

edit:

to show the data, I want to pass...heres a snippet of the code.

its an array of objects.. where 1 of the poperties of each object is another array

example:

var somePicks = [];

somePicks.push({casename:caseName, fullname:fullName, trialdate:trialDate, citystate:cityState, plaintiff:plaintiff, itemsordered:itemsOrdered=[{name:itemOrdered, price:itemPrice}]});

when from all the checkboxes.. I update the 'sub-array' (push or splice..etc)

somePicks[i].itemsordered.push({name:itemOrdered, price:itemPrice}); 

'this' is the array/data I want to get into my PHP session from JS using whatever I can AJAX most likely.

4 Answers 4

0

You can sort of do that, but in essence it won't be any different than using an external PHP file. The PHP code gets executed on the server before ever being sent to the browser. You won't be able to update the PHP SESSION array without reconnecting with the server.

If you really want to use post to call the current page (I don't think you can just leave the url blank, but you can provide the current file name), you can just have the PHP handler code at the top of the page. However, this would be the exact same as just putting that handler code in an external file and calling it.

Either way, the page will not refresh and will look exactly the same to the user.

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

Comments

0

You can use $.ajax function with $(#formid).serializearray (). And use url as ur form action in $.ajax function.

I hope it will work for you

    <form id="formId" action="post.php" methor="post">
            <input type="checkbox" name="test1" value="testvalue1">TestValue1
            <input type="checkbox" name="test2" value="testvalue2">TestValue2
            <input type="button" id="buttonSubmit" value="click here" />
        </form>
        <script>
          $("document").ready(function () 
          {
            $("#buttonSubmit").click(function () }
            {
                var serializedata=$("#formId").serializeArray();
                $.ajax(
                {
                    type:"post",
                    url:$("#formId").attr("action"),
                    data:{"data":serializedata},
                    success:function()
                    {
                        alert("yes");
                    }
                });    
            });

         });
      </script>
<?php
    if(isset($_POST))
    {
        session_start();
        $_SESSION["data"]=$_POST["data"];
    }
?>

8 Comments

hmm.. can you expand a little for me? or provide a code sample better yet? :) I'm hoping to try: 1.) omitting the URL all together as suggested in the link/post above to post to the same page 2.) try the url/page name of the same page the PHP/Ajax call is in 3.) If all else fails..I'll try a external.php script to do this.. (which seems such a waste and rather silly).. What I would like (if anyone has the time/experience) is to get some sample code for options #1 & #2 first...
(too long for 1 comment I guess) However, I'm not sure how I would formulate the Ajax call to update a session var *or passin my JS multi-dimensional array to my session
HI.. thanks for the reply. I'll try to pick it apart and see if I can use it for my project. The difference is.. I am 'not' using a form, with a single submit button/action Background Summary: I query database and get some' results'.. these results are displayed as 'panels'. (1 for each result/record returned).. on each panel.. I have a 'group' of check boxes.... Whenever user clicks on a checkbox (adding an aspect of the item).. I add it to a MAIN array.. (called: userPicks[])..
...continued... This userPicks[] array is an array of objects.. in each of these objects pushed into the array.. one of their properties is another array. I want to (each time the user clicks a checkbox) UPDATED my array (which Im doing).. and pushed/update my SESSION variable with this array.... so if I go to next page.. I can grab the updated session data.. (does not need to be used on the SAME PAGE) Will serializeArray() effect the complicated userPicks[] array I want to pass/save?
...continued... So there is no 'form' (so to speak)... just check boxes in each 'panel'.. that updates an array.. I want to push this MAIN (JS) ARRAY to my (PHP) SESSION
|
0

I suggest to use the .post method of Jquery, to call a PHP file, sending the array and processing in the PHP called.

Can find the jquery documentation about .post() here: http://api.jquery.com/jquery.post/

Edited:

I used this case some time ago:

document.getElementById("promotion_heart_big").onclick = function(e){
                $.post("' . URL_SITE . 'admin/querys/front.make_love.php", 
                    {
                        id_element: ' . $business["promotion"]["id"] . ', 
                        type: \'promotion\', 
                        value: $("#field_heart").val()
                    },
                    function(data) {
                        if (data.result) {
                            //some long code....
                            }
                        }
                    },
                    "json"
                );

2 Comments

Thanks... although Im not sure if you read the question properly? (without the use of an external .php script).. I want to 'post' (lack of better term) to the SAME page.. Maybe this will work: stackoverflow.com/questions/7561569/…
CAN YOU POST AN EXAMPLE?? Of using an external php script then? But most important is saving the complicated data (object array with array as object property as well) not sure how to save such complicated data to the session?
0

from some preliminary testing..

this does NOT seem to be working, (will do more test tomorrow)

$.ajax({
    type : 'POST',
    //url : 'sessionSetter.php',
    data: {
        userPicks : userPicks,
    },
    success : function(data){
        //console.log(data);
    },
    error : function(XMLHttpRequest, textStatus, errorThrown) {

});

It was mentioned that posting to external .php script -or- posting the same page would produce the same results..

  • no page refresh
  • $_SESSION would update for future pages

Does anyone have an y example for that?

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.