Results 1 to 9 of 9

Thread: [RESOLVED] JavaScript Array to PHP post

  1. #1

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Resolved [RESOLVED] JavaScript Array to PHP post

    I have this java array:
    Code:
        j_cdb = Array({"cid":"43","pid":"2","name":"ATV","srch":"ATV"},{"cid":"44","pid":"8","name":"AU Jobs","srch":"AU Jobs"},{"cid":"20","pid":"1","name":"Accommodation","srch":"Accommodation"},{"cid":"21","pid":"1","name":"Accounting","srch":"Accounting"},{"cid":"22","pid":"8","name":"Accounting & Finance Jobs","srch":"Accounting & Finance Jobs"},{"cid":"23","pid":"1","name":"Advertising & Design","srch":"Advertising & Design"},{"cid":"24","pid":"1","name":"Advertising & Marketing","srch":"Advertising & Marketing"}, etc...)
    
    document.getElementById("theA").value=j_cdb; //this isn't working
    I want to post it and process it in PHP. I have a hidden field (theA) and the submit button. When this code executes:
    PHP Code:
    if (isset($_POST['theA'])) {
        
    var_dump($_POST['theA']);

    I get:
    Code:
    string(8719) "[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object], etc....
    I have tried json.stringfy / json decode. I am very new at this (java / php) so any guidance would be appreciated.
    Last edited by dbasnett; Oct 13th, 2012 at 10:09 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  2. #2

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Java Array to PHP post

    I thought a class might help me see what the problem is:

    Code:
             // category class
        function catg (a) {
            this.cid = a['cid'];
            this.pid = a['pid'];
            this.name = a['name'];
            this.srch = a['srch'];
            catg.prototype.opt = function () {
    
            }
            catg.prototype.toPHP = function () {
                return '{"cid":"' + this.cid + '","pid":"' + this.pid + '","name":"' + this.name + '","srch":' + this.srch +'"},';
            }
        }
    
        newcat = Array();
    
        function allCats(but) {
            for (i = 0; i < j_cdb.length; ++i) {
                foo = new catg(j_cdb[i]);
                newcat.push(foo);
            }
            test = "";
            for (i = 0; i < newcat.length; ++i) {
                foo = newcat[i];
                test +=  foo.toPHP;
            }
            // test = test.substring(0,test.length-1);
            document.getElementById("test").innerHTML=test;
            // document.getElementById("theA").value=  test;
        }
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Java Array to PHP post

    Try this way:
    PHP Code:
    <?php
      
    if(isset($_POST['submit']))
      {
        
    $str json_decode($_POST['str'], true);
        
    var_dump($str);
      }
     
    ?>
    <html>
    <head>
      <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
      <script>
        var j_cdb = Array({"cid":"43","pid":"2","name":"ATV","srch":"ATV"},{"cid":"44","pid":"8","name":"AU Jobs","srch":"AU Jobs"},{"cid":"20","pid":"1","name":"Accommodation","srch":"Accommodation"},{"cid":"21","pid":"1","name":"Accounting","srch":"Accounting"},{"cid":"22","pid":"8","name":"Accounting & Finance Jobs","srch":"Accounting & Finance Jobs"},{"cid":"23","pid":"1","name":"Advertising & Design","srch":"Advertising & Design"},{"cid":"24","pid":"1","name":"Advertising & Marketing","srch":"Advertising & Marketing"});
        
        $(document).ready(function(){
          $('#prepare').click(function(){ // prepare button inserts the JSON string in the hidden element
            $('#str').val(JSON.stringify(j_cdb));
          });
        });
        
      </script>
    </head>
    <body>

      <button id="prepare">Prepare</button>
      
      <form method="post">
        <input type="hidden" id="str" name="str" value="" />
        <input type="submit" name="submit" value="Submit" />
      </form>
        
    </body>
    </html>
    Hope it helps

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Java Array to PHP post

    This works. All of this is about manipulating a one to many relationship (adjacency model). Now that I can get the array into java, and back, I can work on moving things from one list to another.

    PHP Code:
    <?php
    if (isset($_POST['theA'])) {
        
    $temp str_replace('\\'''$_POST['theA']); // get rid of \
        
    $temp str_replace(']['''$temp); // get rid of trailing ][
        
    $theA json_decode($temptrue); // create array
        
    print_r($theA);
        foreach (
    $theA as $value) {
            echo 
    $value['name'] . '</br>';
        }
    }
    ?>
    <script type='text/javascript'>
    <?php
        $j_cdb 
    $jcats->toJson($jcats->catsdb);
            
    // create java array
        
    $j_cdb=str_replace('[','',$j_cdb);
        
    $j_cdb=str_replace(']','',$j_cdb);
        echo 
    "j_cdb = Array("$j_cdb ");\n";
    ?>
        newcat = Array();

        function allCats(but) {
            for (i = 0; i < j_cdb.length; ++i) {
                foo = new catg(j_cdb[i]);
                newcat.push(foo);
            }
            test = "";
            for (i = 0; i < newcat.length; ++i) {
                foo = newcat[i];
                test +=  foo.toPHP;
            }
            $('#theA').val(JSON.stringify(j_cdb)); 
        }

    </script>
    <div data-role="content">
      <h1>jcmo Category Management</h1>
      <div id="test">*</div>
      <table border="1" cellpadding="2">
        <tr>
          <td>Main</td>
          <td>Level 1</td>
          <td>Level 2</td>
          <td>Level 3</td>
          <td>Level 4</td>
        </tr>
        <tr>
          <td></td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td><button id="" data-inline="true" onClick=";">Move</button></td>
          <td><button id="" data-inline="true" onClick=";">Move</button></td>
          <td><button id="" data-inline="true" onClick=";">Move</button></td>
          <td><button id="" data-inline="true" onClick=";">Move</button></td>
          <td><button id="" data-inline="true" onClick=";">Move</button></td>
        </tr>
      </table>
      <form action="" method="post">
        <input name="" type="submit" value="Submit" data-inline="true">
        <input id="theA" name="theA" type="hidden" value="">
      </form>
      <div id="test"></div>
    </div>
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: JavaScript Array to PHP post

    So I thought that this(lvl0) would work:

    Code:
    j_cdb = Array({"cid":"43","pid":"2","name":"ATV","srch":"ATV"},{"cid":"44","pid":"8","name":"AU Jobs","srch":"AU Jobs"},{"cid":"20","pid":"1","name":"Accommodation","srch":"Accommodation"},{"cid":"21","pid":"1","name":"Accounting","srch":"Accounting"},{"cid":"22","pid":"8","name":"Accounting & Finance Jobs","srch":"Accounting & Finance Jobs"},{"cid":"23","pid":"1","name":"Advertising & Design","srch":"Advertising & Design"},{"cid":"24","pid":"1","name":"Advertising & Marketing","srch":"Advertising & Marketing"}, etc...)
    
    liproto = '<li id="_CID_" onClick="">_NAME_</li>' + '\n';
        
    function allCats() {
        toJstring();
    }
    function toJstring(){
        $('#theA').val(JSON.stringify(j_cdb)); 
    }
    function lvl0(){
    	// build main level categories
        temp = '';
        for (i = 0; i < j_cdb.length; ++i) {
            if (j_cdb[i]['cid'] == 0){
    			foo = liproto.replace('_CID_', j_cdb[i]['cid']);
    			foo = liproto.replace('_NAME', j_cdb[i]['name']);
                temp += foo;
            }
        }
        document.getElementById("l0").innerHTML = temp;
    }
    
    $(document).ready(function() {
        allCats();
        lvl0();
    });
    The function lvl0 doesn't appear to see the elements inside of j_cdb.
    Last edited by dbasnett; Oct 11th, 2012 at 06:11 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Java Array to PHP post

    Code:
        var j_cdb = Array({"cid":"43","pid":"2","name":"ATV","srch":"ATV"},{"cid":"44","pid":"8","name":"AU Jobs","srch":"AU Jobs"},{"cid":"20","pid":"1","name":"Accommodation","srch":"Accommodation"},{"cid":"21","pid":"1","name":"Accounting","srch":"Accounting"},{"cid":"22","pid":"8","name":"Accounting & Finance Jobs","srch":"Accounting & Finance Jobs"},{"cid":"23","pid":"1","name":"Advertising & Design","srch":"Advertising & Design"},{"cid":"24","pid":"1","name":"Advertising & Marketing","srch":"Advertising & Marketing"});
    	
        $(document).ready(function(){
    
    	  lvl0(); //call the function
    	  
    	  function lvl0(){
    		//console.log(j_cdb);
    		
    		temp = '';
    		for (i = 0; i < j_cdb.length; ++i) {
              if (j_cdb[i]['cid'] == '43'){
    		    temp += j_cdb[i]['name'] + ',';
    		  }
    		}
    		$('#txt').html(temp); //display it
    	  }
    	});
    I just tried it like above and it worked. Each field is a string. So, the cid might also be a string value.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Java Array to PHP post

    I haven't tried this yet, but the clarity of the morning makes me want to ask myself why am I so blind. The cid should never be 0! That is a big DUH! to me. Thanks for pointing the obvious!!!! Thanks for your patience.

    I would up vote you again if I could.
    Last edited by dbasnett; Oct 12th, 2012 at 07:56 AM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8

    Thread Starter
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Lightbulb Re: JavaScript Array to PHP post

    So here is a near finished version. This allows me to work on the categories without server intervention.

    PHP Code:
    liproto '<li class="catmgmtli" id="_CID_" onClick="filllvl(this.id)" onDblClick="mvthisdbl(this.id)" >_NAME_</li>' '\n';

    function 
    toJstring() {
       $(
    '#theA').val(JSON.stringify(j_cdb));
    }
    var 
    lstcid '0'//last cid selected
    var mv2cid '0'//move to cid
    var mv2ok true;
    var 
    mvtcid = Array(); // move this cid

    function mvthisdbl(cidlvl) {
       
    filllvl(cidlvl);
       
    mvthis();
    }
    function 
    setmv2() {
       if (
    mv2ok) {
          
    document.getElementById("mv2").innerHTML document.getElementById("lastitem").innerHTML;
          
    mv2cid lstcid;
          
    document.getElementById("lastitem").innerHTML '';
       }
    }
    function 
    mvthis() {
       if (
    mv2cid != lstcid) {
          
    mvtcid.push(lstcid);
          
    document.getElementById("mvt").innerHTML += "&bull;" document.getElementById("lastitem").innerHTML "\n";
          
    document.getElementById("lastitem").innerHTML '';
       }
    }
    function 
    moveit(clr) {
       if (
    clr == 1) {
          
    //do move
          
    while (mvtcid.length 0) {
             
    foo mvtcid.pop()
             for (
    0j_cdb.length; ++i) {
                if (
    j_cdb[i]['cid'] == foo) { // look for move this
                   
    j_cdb[i]['pid'] = mv2cid;
                   break;
                }
             }
          }
       }
       
    toJstring();
       
    //refresh
       
    filllvl('0.0'); //set main level items
       
    lstcid '0'//last cid selected
       
    mv2cid '0'//move to cid
       
    mv2ok true;
       
    mvtcid = Array(); // move this cid
       
    document.getElementById("lastitem").innerHTML '';
       
    document.getElementById("mv2").innerHTML ''
       
    document.getElementById("mvt").innerHTML '';
    }
    function 
    filllvl(cidlvl) {
       
    // build main level categories
       // lvl = 'pid.level'
       
    var cl cidlvl.split('.');
       var 
    pid cl[0];
       
    cl[1] = parseInt(cl[1]);
       var 
    lvl cl[1] + 1//next level
       
    temp '';
       if (
    cl[1] <= 4) {
          
    mv2ok true;
       } else {
          
    mv2ok false;
       }
       for (
    0j_cdb.length; ++i) {
          if (
    j_cdb[i]['cid'] == pid) { // show last item selected
             
    document.getElementById("lastitem").innerHTML j_cdb[i]['name'];
             
    lstcid j_cdb[i]['cid']; // remember the cid
          
    }
          if (
    cl[1] <= 4) {
             if (
    j_cdb[i]['pid'] == pid) {
                
    foo liproto.replace('_CID_'j_cdb[i]['cid'] + '.' lvl);
                
    foo foo.replace('_NAME_'j_cdb[i]['name']);
                
    temp += foo;
             }
          }
       }
       var 
    "l" cl[1];
       
    document.getElementById(l).innerHTML temp;
       for (
    cl[1] + 15; ++i) {
          var 
    "l" i;
          
    document.getElementById(l).innerHTML '';
       }
    }

    $(
    document).ready(function() {
        
    toJstring();
        
    filllvl('0.0'); //set main level items
    });

    </
    script>
    <
    div data-role="content">
      <
    table border="0" cellpadding="1">
        <
    tr>
          <
    td width="20%" align="left" valign="top"><strong>jcmo Category Management</strong></td>
          <
    td width="14%" align="center" valign="top"><form action="" method="post">
              <
    input name="" data-role="none" type="submit" value="Submit Changes" data-inline="true">
              <
    input id="theA" name="theA" type="hidden" value="">
            </
    form></td>
          <
    td width="62%" align="left" valign="top" bgcolor="#FFFFFF" class="catmgmtli">DirectionsSelect the category to move sub-categories TO and click 'Set move to'Select the sub-categories to move and click 'Set move these' (you can select more than 1), or just double click the nameWhen done selecting sub-categories click 'Move'To save changes to the Data Base click 'Submit Changes'. Do not refresh the page or changes not submitted will be lost!</td>
        </
    tr>
    </
    table>
      <
    table border="0" cellpadding="1">
        <
    tr>
          <
    td width="20%" align="right" class="catmgmtli">Selected Category:&emsp;</td>
          <
    td width="30%" class="catmgmtli" id ="lastitem">&nbsp;</td>
          <
    td width="18%" align="center"><button id="" data-role="none" data-inline="true">Edit Selected</button></td>
          <
    td width="18%" align="center"><button id="" data-role="none" data-inline="true">New Category</button></td>
        </
    tr>
        <
    tr>
          <
    td><button id="" data-role="none" data-inline="true" onClick="setmv2()">Set move to</button></td>
          <
    td class="catmgmtli" id="mv2"></td>
        </
    tr>
        <
    tr>
          <
    td><button id="" data-role="none" data-inline="true" onClick="mvthis()">Set move these</button></td>
          <
    td class="catmgmtli"><textarea cols="20" readonly class="catmgmtli" id="mvt" data-role="none"></textarea></td>
          <
    td><button id="" data-role="none" data-inline="true" onClick="moveit(0)">Clear Move's</button></td>
          <td></td>
          <td><button id="" data-role="none" data-inline="true" onClick="moveit(1)">Move</button></td>
        </tr>

      </table>
      <table border="1" cellpadding="1">
        <tr valign="top">
          <td>Main</td>
          <td>Level 1</td>
          <td>Level 2</td>
          <td>Level 3</td>
          <td>Level 4</td>
        </tr>
        <tr valign="top">
          <td width="20%"><ul id="l0">
            </ul></td>
          <td width="20%"><ul id="l1">
            </ul></td>
          <td width="20%"><ul id="l2">
            </ul></td>
          <td width="20%"><ul id="l3">
            </ul></td>
          <td width="20%"><ul id="l4">
            </ul></td>
        </tr>
      </table>
    </div> 
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: [RESOLVED] JavaScript Array to PHP post

    Glad to know that it is sorted out.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width