PDA

View Full Version : JS variable passed to PHP variable


h8ids
10-13-2006, 04:30 PM
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:

...
<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:
<?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

Fumigator
10-13-2006, 04:58 PM
The $_GET variable you are setting up via that link is $_GET['postion'].

h8ids
10-13-2006, 05:05 PM
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

Fumigator
10-14-2006, 03:04 AM
You have a typo: postion or position?

h8ids
10-16-2006, 03:41 PM
Corrected the typo.
Still getting the same result when I use echo: [object HTMLInputElement]
:confused:

_Aerospace_Eng_
10-16-2006, 04:44 PM
Its doing what its supposed to. You need to use
var MajorRecords = document.getElementById("MajorEdit3").value;
not
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.

h8ids
10-16-2006, 09:39 PM
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:

<!-- 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:

<?php
session_start();
echo $_GET['MajorRecords'];
?>

_Aerospace_Eng_
10-16-2006, 09:49 PM
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
<script>document.write("<a href='DBEditMajor.php?postion="+MajorRecords+"\'>
So it could be that the href is getting
DBEditMajor.php?postion=3\
instead of just
DBEditMajor.php?postion=3
These typos seem to be causing your issues. Take better care of how you type.

h8ids
10-16-2006, 09:57 PM
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.

GJay
10-16-2006, 11:11 PM
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?

CFMaBiSmAd
10-17-2006, 12:40 AM
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 -
<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 -
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.

h8ids
10-17-2006, 04:01 PM
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.

h8ids
10-17-2006, 04:09 PM
I am trying to capture the data that's typed or automatically placed in the box.

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?

CFMaBiSmAd
10-17-2006, 05:13 PM
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.

h8ids
10-17-2006, 05:45 PM
Thanks