Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question defining variable from INPUT field data

    What I'm using:
    Code:
    <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?

  • #2
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, except it would be a numerical string, not a number.
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    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>
    Last edited by h8ids; 10-16-2006 at 10:39 PM.

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    ok, after replying to your post elsewhere, I see you found this forum...

    with the code:
    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:
    Code:
    <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.

  • #5
    Senior Coder
    Join Date
    Feb 2006
    Location
    USA
    Posts
    1,013
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions
    Java != JavaScript && JScript != JavaScript
    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

  • #6
    Regular Coder
    Join Date
    Jan 2006
    Posts
    251
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    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
    Code:
    http://localhost/DBEditMajor.php
    Cleared the cache. Used different browser to check the link. Same result.

    Posted code from both pages.
    Sending page:
    Code:
    <!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:
    Code:
    <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>


  •  

    Posting Permissions

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