Web Analytics Made Easy -
StatCounter JS variable passed to PHP variable - CodingForum

Announcement

Collapse
No announcement yet.

JS variable passed to PHP variable

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

  • JS variable passed to PHP variable

    Going through old posts and have a loose understanding of the process.
    But I still don't have it working.

    Just trying to see the variable passed at this point

    Form page:
    Code:
    ...
    		<input type="text" name="MajorEdit3" id="MajorEdit3" size="3">
    		<script>
    		var MajorRecords = document.getElementById("MajorEdit3");
    		</script>
    		<br>
    		&nbsp;&nbsp;<script>document.write("<a href='DBEditMajor.php?postion="+MajorRecords+"\'><input type='button' value='Save revisions' name='MajorRev' id='MajorRev' title='Store Major revisions' onclick='this.form.action='DBEditMajor.php'; resolveData();'></a>");</script>
    Recieving page:
    Code:
    <?php
    session_start();
    echo $_GET['MajorRecords'];
    ?>
    Getting the following error.
    Notice: Undefined index: MajorRecords in C:\Program Files\Apache Group\Apache2\htdocs\DBEditMajor.php on line 18

  • #2
    The $_GET variable you are setting up via that link is $_GET['postion'].

    Comment


    • #3
      I know MajorRecords has a value. I can see the input box is populated. But the value isn't being passed.

      The following is appearing in the Browser URL:
      localhost/DBEditMajor.php?postion=[object%20HTMLInputElement]

      The error has changed to
      Notice: Undefined index: position in C:\Program Files\Apache Group\Apache2\htdocs\DBEditMajor.php on line 18
      Last edited by h8ids; Oct 13, 2006, 01:25 PM.

      Comment


      • #4
        You have a typo: postion or position?

        Comment


        • #5
          Corrected the typo.
          Still getting the same result when I use echo: [object HTMLInputElement]

          Comment


          • #6
            Its doing what its supposed to. You need to use
            Code:
            var MajorRecords = document.getElementById("MajorEdit3").value;
            not
            Code:
            var MajorRecords = document.getElementById("MajorEdit3");
            The second one just returns what type of object the element is which is why you get what you get. You need to get the value of the input.
            ||||If you are getting paid to do a job, don't ask for help on it!||||

            Comment


            • #7
              Still not working...
              Input box does receive a value from the database. But the receiving page echo's
              Notice: Undefined index: MajorRecords in C:\...\DBEditMajor.php on line 21


              Data capture page:
              Code:
              		<!-- Record number -->
              <input type="text" name="MajorEdit3" id="MajorEdit3" size="3">
              		<script>
              		var MajorRecords = document.getElementById("MajorEdit3").value;
              		</script>
              		<br>
              		&nbsp;&nbsp;<script>document.write("<a href='DBEditMajor.php?postion="+MajorRecords+"\'><input type='button' value='Save revisions' name='MajorRev' id='MajorRev' title='Store Major revisions' onclick='this.form.action='DBEditMajor.php'; resolveData();'></a>");</script>
              Receiving page:
              Code:
              <?php
              session_start();
              echo $_GET['MajorRecords'];
              ?>

              Comment


              • #8
                Then it means the wrong value is getting passed to the link, once its created mouseover and see what the status bar says that should tell you exactly what is being passed. I see a backslash in there that doesn't seem like it should be there
                Code:
                <script>document.write("<a href='DBEditMajor.php?postion="+MajorRecords+"[b]\[/b]'>
                So it could be that the href is getting
                Code:
                DBEditMajor.php?postion=3\
                instead of just
                Code:
                DBEditMajor.php?postion=3
                These typos seem to be causing your issues. Take better care of how you type.
                ||||If you are getting paid to do a job, don't ask for help on it!||||

                Comment


                • #9
                  Fixed the typos.
                  While mouse hovers over the submit button the status field shows:
                  localhost/DBEditMajor.php?position=
                  Data isn't being acquire from the input field.

                  Comment


                  • #10
                    the value is being appended when the code is run, which with the HTML you've given will be empty.

                    Are you expecting it to update the link when something is typed in the box?
                    You seem to be struggling with the javascript rather than PHP- this is the 3rd thread now you're asking for help with the same line of code, so perhaps ask for a mod to move it to the javascript forum?
                    My thoughts on some things: http://codemeetsmusic.com
                    And my scrapbook of cool things: http://gjones.tumblr.com

                    Comment


                    • #11
                      About all I can tell from reading this is that you want to use a clickable text link to submit your form. If so, then the following will do this -
                      Code:
                      <form name="theForm" method="get" action="yourformaction.php">
                      <input type="text" name="MajorEdit3" id="MajorEdit3" size="3">
                      <a href="javascript:document.theForm.submit();">Submit</a>
                      </form>
                      and the following code in the action= file will receive what was entered in the text input field on the form -
                      PHP Code:
                      echo $_GET['MajorEdit3']; 
                      Edit: From reading a little of the other thread on this, so long as you use the GET method, the proper url will be formed with the .../yourformaction.php?name=value&othername=value... You don't have to form or code this yourself, the HTML form processing of the browser does this for you.
                      Last edited by CFMaBiSmAd; Oct 16, 2006, 08:49 PM.
                      Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

                      Comment


                      • #12
                        Thanks guys.
                        Been really struggling with capturing JS variables and sharing with PHP.

                        The Sending page references four DB tables. And I'd like to be able to Edit, Add and Delete records from each table. Trying to grasp the languages, keep the page simple for the user and cross platform compatible have been a bit overwhelming. Sorry about posting more than once regarding my project.

                        Comment


                        • #13
                          I am trying to capture the data that's typed or automatically placed in the box.

                          Originally posted by GJay View Post
                          the value is being appended when the code is run, which with the HTML you've given will be empty.

                          Are you expecting it to update the link when something is typed in the box?
                          You seem to be struggling with the javascript rather than PHP- this is the 3rd thread now you're asking for help with the same line of code, so perhaps ask for a mod to move it to the javascript forum?

                          Comment


                          • #14
                            If something is in a form field, it does not matter how it got entered, when the form is submitted, those values will be sent to the action= target. You don't have to write any code to cause this step to happen. The browser's HTML form processing logic does this.

                            Edit: In my example posted above, if you have javascript that places something into the input field by referencing the id="MajorEdit3", all you need to do to get this to be sent to the server is submit the form. You don't have to build the url and place the values as parameters as part of the url.
                            Last edited by CFMaBiSmAd; Oct 17, 2006, 01:19 PM.
                            Finding out HOW to do something is called research, i.e. keep searching until you find the answer. After you attempt to do something and cannot solve a problem with it yourself, would be when you ask others for help.

                            Comment


                            • #15
                              Thanks

                              Comment

                              Working...
                              X