...

View Full Version : defining variable from INPUT field data



h8ids
10-16-2006, 10:21 PM
What I'm using:


<input type="text" name="MajorEdit3" id="MajorEdit3" size="3">
<script>
var MajorRecords = document.getElementById("MajorEdit3").value;
</script>


This is a small part of my project. Just want to be certain that I'm defining a variable correctly by use of Javascript.

Say the data in the field is the number 55; shouldn't the JS variable MajorRecords adopt that value?

Kravvitz
10-16-2006, 10:25 PM
Yes, except it would be a numerical string, not a number.

h8ids
10-16-2006, 10:33 PM
Thanks Kravvitz.

Been trying to pass the field value to PHP.
I have to use JS because the field value changes based upon a users' listbox selection.

The next step; add the field value to a URL for PHP to acquire.

Does this look correct?

<script>document.write("<a href='DBEditMajor.php?position="+MajorRecords+"'><input type='button' value='Save revisions' name='MajorRev' id='MajorRev' title='Store Major revisions'></a>");</script>

GJay
10-16-2006, 11:23 PM
ok, after replying to your post elsewhere, I see you found this forum...

with the code:


<input type="text" name="MajorEdit3" id="MajorEdit3" size="3">
<script>
var MajorRecords = document.getElementById("MajorEdit3").value;
</script>

MajorRecords will be empty, as at the time the line is run (during the page load) the input has no value.

you probably want:


<input type="text" name="MajorEdit3" id="MajorEdit3" size="3" onblur="MajorRecords=this.value;">
<a href="DBEditMajor.php" onclick="this.href+='?position='+MajorRecords;"><input type='button' value='Save revisions' name='MajorRev' id='MajorRev' title='Store Major revisions'></a>



Whenever the input loses focus, MajorRecords will take its value.
When you click the link, the href of the link will be changed to include the current value of MajorRecords, and then the link will fire as normal.

Kravvitz
10-17-2006, 12:05 AM
Keep in mind that some Internet users use a browser that doesn't have JavaScript enabled. (http://www.thecounter.com/stats/2006/September/javas.php)

I suggest you read these:
Graded Browser Support (http://developer.yahoo.com/yui/articles/gbs/gbs.html)
Progressive Enhancement and the Future of Web Design (http://www.webmonkey.com/03/21/index3a.html)
Accessibility is seldom just up to the interface developer (http://www.robertnyman.com/2006/10/10/accessibility-is-seldom-just-up-to-the-interface-developer/)

h8ids
10-17-2006, 03:16 PM
Still getting the following error on the receiving page.


Notice: Undefined index: MajorRecords in C:\...\DBEditMajor.php on line 21

The status bar shows thefollowing

http://localhost/DBEditMajor.php

Cleared the cache. Used different browser to check the link. Same result.

Posted code from both pages.
Sending page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<title>Database edit page</title>
<META NAME="EXPIRES" CONTENT="Mon, 01 Jan 2000 12:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
</HEAD>

<?php
//session_start();
error_reporting(E_ALL);
error_reporting(E_ALL | 'E_STRICT');
$db = mysql_connect("localhost", "root", "");
mysql_select_db("dbNAME",$db);
?>

<body marginheight="0" onload="hidden()">

<!-- Form Border image and -->
<div style="position: absolute; left: 5%;">

<form name="f1" enctype="multipart/form-data" method="POST">

<!-- Table positioning -->
<div style="position: absolute; width: 635px; left: 40px; top: 85px; z-index:1">

<table width='631' cellspacing='0'>
<thead>
<tr><th colspan="2" bgcolor='lightyellow'>Add, Edit or Delete information</th></tr>
</thead>

<tr bgcolor='#E0E0E0'>
<td width="481">&nbsp;Majors:&nbsp;<select name="Major" id="MajorChoice"></select></td>
<td width="150">

<!-- Add Edit Delete buttons -->
&nbsp;<input type="button" value="Add" name="MajAdd" id="MajAdd">
&nbsp;<input type="button" value="Delete" name="MajorDelete" onclick="resolveData();">
</td>
</tr><tr>
<td>
&nbsp;<div name="MajorAdd" id="MajorAdd">
&nbsp;&nbsp;<b>Add</b> a Major.
<input type='text' id='Majorcode' name='MajorCode' size='4' maxlength='4'>
<input type='text' id='Majors' name='Majors' size='35' maxlength='50'><br>
&nbsp;&nbsp;<input type="submit" value="Add Major" name="Add Major" title="Add Major" onclick="this.form.action='DBAddMajor.php'"><p>
</div>
<div id="MajorEdit" name="MajorEdit" style='display: none'>
&nbsp;&nbsp;<b>Edit</b> a Major.
<input type="text" name="MajorEdit" id="MajorEdit" size="4">
<input type="text" name="MajorEdit2" size="35">

<!-- Record number -->
<input type="text" name="MajorEdit3" id="MajorEdit3" size="3" onblur="MajorRecords=this.value;">
<a href="DBEditMajor.php" onclick="this.href+='?position='+MajorRecords;"><input type='button' value='Save revisions' name='MajorRev' id='MajorRev' title='Store Major revisions'></a>
<p>
</div>
</td>
</tr>
</table>
</DIV>
</form>

<script type="text/javascript">
var empMajorChoice = [
<?php
$dbMajor = mysql_connect("localhost", "root", "");
mysql_select_db("newcourse",$dbMajor);
$resultMajor = mysql_query('SELECT Records,MajorCode,Majors FROM majors');
$count = 1;
$num = mysql_num_rows($resultMajor);
if ($num != 0) {
while ($rowMajor = mysql_fetch_array($resultMajor)) {
echo "['$rowMajor[0]','$rowMajor[1]','$rowMajor[2]']";
if($count < $num){
echo ",";
}
$count++;
}
}
$Records = $resultMajor["Records"];
mysql_free_result($resultMajor);
?>
];
//Collecting of paired data for Major presentation
for(var a=0;a<empMajorChoice.length;a++){
document.f1.MajorChoice[document.f1.MajorChoice.length] = new Option(empMajorChoice[a][2],empMajorChoice[a][1],empMajorChoice[a][0],empMajorChoice[a][3])
}
</script>
</DIV>
</BODY>
</HTML>


Receiving page:


<HTML>
<HEAD>
<title>Edit Course Details</title>
<META NAME="EXPIRES" CONTENT="Mon, 01 Jan 2000 12:00:00 GMT">
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<center><b><noscript><p><br>Please enable JavaScript within your browser so this page will function appropriately.<br>
Then press the browser's refresh button.</noscript></b></center>
</HEAD>
<body>
<script language=javascript>
write("MajorRecords");
</script>

<!--Drawing information from specified records -->
<?php
session_start();
$_GET['MajorRecords'];
$Records = $_GET['MajorRecords'];
echo $Records;

if($update)
{
$db = mysql_connect("localhost", "root","");
mysql_select_db("newcourse",$db);
$MajorCode=mysqlclean($_POST, 'MajorCode');
$Majors=mysqlclean($_POST, 'Majors');

$sql = "UPDATE majors SET MajorCode='$MajorCode', Majors='$Majors' WHERE Records=$Records";
$result = mysql_query($sql);

header("Location: dbedit.php");
exit;
}
?>
</body>
</HTML>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum