Web Analytics Made Easy -
StatCounter Javasript variables into PHP form - CodingForum

Announcement

Collapse
No announcement yet.

Javasript variables into PHP form

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolved Javasript variables into PHP form

    I have a php form that submits to a MySQL database. One hidden field gets it value from a PHP variable but the other three fields must get the values from Javascript variables. I can't figure out how to get the Javascript lat, lng and zoom variables into the form for submission. Any ideas?


    PHP Code:
    <script type="text/javascript">
      lat = map.getCenter().lat();
      lng = map.getCenter().lng();
      zoom = map.getZoom();
    </script>

    <?php
      $mapId 
    $_POST['map_id'];
      
    $lat $_POST['lat'];
      
    $lng $_POST['lng'];
      
    $zoom $_POST['zoom'];
    ?>


    <form id ="save_map" method="post" action="<?php echo $PHP_SELF;?>">
      <input type="hidden" name="map_id" value="<?php echo "$padding$number"?>" />
      <input type="hidden" name="lat" value="" />
      <input type="hidden" name="lng" value="" />
      <input type="hidden" name="zoom" value="" />
      <input type="submit" name="save_this_map" value="Save Map ID:" />
    </form>


    --
    Last edited by Len Whistler; Apr 3, 2009, 09:18 PM.
    Leonard Whistler

  • #2
    Just use document.getElementById.
    PHP Code:
    <script type="text/javascript">
      latval = map.getCenter().lat();
      lngval = map.getCenter().lng();
      zoomval = map.getZoom();
    window.onload = function()
    {
    document.getElementById('lat').value = latval;
    document.getElementById('lng').value = lngval;
    document.getElementById('zoom').value = zoomval;
    }
    </script>

    <?php
      $mapId 
    $_POST['map_id'];
      
    $lat $_POST['lat'];
      
    $lng $_POST['lng'];
      
    $zoom $_POST['zoom'];
    ?>


    <form id ="save_map" method="post" action="<?php echo $PHP_SELF;?>">
      <input type="hidden" name="map_id" value="<?php echo "$padding$number"?>" />
      <input type="hidden" name="lat" id="lat" value="" />
      <input type="hidden" name="lng" id="lng" value="" />
      <input type="hidden" name="zoom" id="zoom" value="" />
      <input type="submit" name="save_this_map" value="Save Map ID:" />
    </form>
    ||||If you are getting paid to do a job, don't ask for help on it!||||

    Comment


    • #3
      what he said^^

      or if you need to get it right before submission...

      if you have the php figured out, but need the js... this should probably go in the js forum.

      anyway...

      Code:
      <script type="text/javascript">
      function insertvar(){
      
      document.save_map.lat.value = map.getCenter().lat();
      document.save_map.lng.value = map.getCenter().lng();
      document.save_map.zoom.value = map.getZoom();
      
      document.save_map.submit()
      }
      </script>
      would be called by...
      Code:
      <form id ="save_map" method="post" action="<?php echo $PHP_SELF;?>">
        <input type="hidden" name="map_id" value="<?php echo "$padding$number"; ?>" />
        <input type="hidden" name="lat" value="" />
        <input type="hidden" name="lng" value="" />
        <input type="hidden" name="zoom" value="" />
        <input type="button" name="save_this_map" value="Save Map ID:" onclick="insertvar();"/>
      </form>
      dbl check that logic, but when you cilck save it'll get the values and set the form values.... then submit the form

      Comment


      • #4
        I don't like the idea of making the "submit" button a button then using js. Now the button won't work with JS disabled. A modification to the above solution if you need them before you submit. Also for the JS above to work you need to have name="save_map" on the form or if you use the id then again use document.getElementById.
        PHP Code:
        <script type="text/javascript">
        function insertvar(){

        document.getElementById('save_map').lat.value = map.getCenter().lat();
        document.getElementById('save_map').lng.value = map.getCenter().lng();
        document.getElementById('save_map').zoom.value = map.getZoom();
        }
        </script>
        <form id ="save_map" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" onsubmit="insertvar();">
          <input type="hidden" name="map_id" value="<?php echo "$padding$number"?>" />
          <input type="hidden" name="lat" value="" />
          <input type="hidden" name="lng" value="" />
          <input type="hidden" name="zoom" value="" />
          <input type="submit" name="save_this_map" value="Save Map ID:" />
        </form>
        Also for security reasons I suggest you use htmlentities on PHP_SELF. Its explained more here: http://seancoates.com/xss-woes
        Last edited by _Aerospace_Eng_; Apr 2, 2009, 01:50 PM.
        ||||If you are getting paid to do a job, don't ask for help on it!||||

        Comment


        • #5
          understood, and i agree with the onsubmit approach. however, if js is disabled it still won't really do what he wants because those values will be null.

          Comment


          • #6
            Originally posted by ohgod View Post
            understood, and i agree with the onsubmit approach. however, if js is disabled it still won't really do what he wants because those values will be null.
            True which is why there should always be some default values to fall back on so the app doesn't "break".
            ||||If you are getting paid to do a job, don't ask for help on it!||||

            Comment


            • #7
              Originally posted by _Aerospace_Eng_ View Post
              True which is why there should always be some default values to fall back on so the app doesn't "break".
              does that approach have a name? error tracing or something. i saw excellent way of error reporting inside functions. i mean when you have bunch of procedures that return 0 or 1, and if you use just those 1's for retrieving content or whatsoever, then sometimes you don't know what's causing the mess. i saw someone make an array of errors and just fill it wherever function can return some kind of NULL value that just isn't supposed to be there. at the end of the script just using simple loop through that array you can see all possible errors. if someone want's an example i can put a link, i just have high will save to look for it now
              PHP Idea Factory

              Comment


              • #8
                _Aerospace_Eng_

                Why did you cross post this? We've posted solutions. Don't waste our time.
                The Javascript post was made 7 minutes before the first reply to the original and primary post, which is this thread. The Javascript post was a brief outline with a link to this thread, since this is also a Javascript question I felt posting a secondary post would be fine in case JavaScript progammer knew the solution but didn't pay much attention to the PHP threads.

                Below is a definition of cross posting from Wikipedia. I don't feel my JavaScript post http://www.codingforum.net/showthread.php?t=162913 fits the description.

                Crossposting
                From Wikipedia, the free encyclopedia
                Jump to: navigation, search

                Crossposting is the act of posting the same message to multiple forums, mailing lists, or newsgroups. This is distinct from multiposting, which involves posting multiple identical messages, each to a single forum, newsgroup, or topic area.

                Crossposting can be helpful if the message is of interest to a larger audience. However, crossposting to groups that are irrelevant to the message posted could be considered spamming. Moreover, excessive crossposting is generally considered bad form because it multiplies traffic without adding any new content. In the extreme case, if all messages were crossposted to every group, then every group would look exactly the same. A crossposter can minimize this problem by specifying that all responses be directed to a single group.


                Thanks for all the replies. I will be posting back in a few hours when I have time to take a closer look at some of the solutions.
                Leonard Whistler

                Comment


                • #9
                  You have the same titles for both of your posts and you aren't asking a thing about the php. You are asking how to get the JS files in the form.
                  ||||If you are getting paid to do a job, don't ask for help on it!||||

                  Comment


                  • #10
                    Finally got it work. For some reason I couldn't get the above examples working with the map.getCenter().lat(), map.getCenter().lng() and map.getZoom() variables, but I could pass the data to a cookie set with Javascript and retrieve the results with PHP to input into the database. Below is what I ended up '

                    PHP Code:
                    // All Javascript variables entered into a cookie and form submitted

                    <form id ="save_map" method="post" action="save_map.php">
                      <
                    input type="submit" value="Save Map ID" name="save_this_map">
                    </
                    form
                    save_map.php
                    PHP Code:
                    <?php
                      $data
                    [1] = $_COOKIE['trans_canada_test'];  //Cookie set with Javascript

                      
                    $array explode ('|'$data[1]);

                      
                    $lat $array[0];  
                      
                    $lng $array[1];
                      
                    $map_zoom $array[2];
                      
                    $map_type $array[3];

                       
                    //Data entered into MySQL database
                    ?>

                    -----
                    Last edited by Len Whistler; Apr 3, 2009, 09:23 PM.
                    Leonard Whistler

                    Comment

                    Working...
                    X