Jump to content

Archived

This topic is now archived and is closed to further replies.

shadiadiph

How to automatically fill a value once another has been selected

Recommended Posts

Hi this must be a common question in my coding below how do i make the input data nameused automatically change to the name associated with the email above in the first row once it has been selected?

 

<tr><td id="t_subject">EMAIL<br /></td></tr>
<tr> 
<td>
<select name="aid" >
<option> Select Email Account</option>
<?
$sql = "select intNameID, email,fname, lname, place from tblpersondetails where place='A' order by intNameID ASC";
$temps01=$DB_site->query($sql);
while ($row=$DB_site->fetch_array($temps01))
{
?>
<option value="<?=$row["intNameID"]?>" <? if($nameid==$row["intNameID"]) { ?>selected<? } ?>> 
<?=$row["email"]?>
</option>
<? 
}
?>
</select>
</td>
</tr>
<tr><td id="t_subject">FROM</td></tr>
<tr><td><input type="text" name="nameused" size="35" value="<?=$row[fname]?><?=$row[lname]?>"/></td></tr>

Share this post


Link to post
Share on other sites

1. Put this at the very TOP of the PHP page (it's always best to separate the programming from the layout).

<?php
  $sql = "select intNameID, email,fname, lname, place from tblpersondetails where place='A' order by intNameID ASC";
  $temps01=$DB_site->query($sql);
  while ($row=$DB_site->fetch_array($temps01))
  {
    $selected = () ? ' selected="selected"' : '';
    $email_options .= "      <option value=\"{$row['intNameID']}\"$selected>{$row['email']}</option>\n";
    $js_names_array .= "names[{$row['intNameID']}] = '{$row['fname']} {$row['lname']}';\n";
  }
?>

 

2. Put this into the HEAD of the HTML code

<script type="text/javascript">>

var names = new Array();
<?php echo $js_names_array; ?>

function changeName(selObj, inputID)
{
    nameID = selObj[selObj.selectedIndex].value;
    document.getElementById(inputID).value = (names[nameID]) ? names[nameID] : '';
    return;
}

</script>

 

3. Change the code you posted above to this:

  <tr><td id="t_subject">EMAIL<br /></td></tr>
  <tr> 
    <td>
      <select name="aid" id="aid" onchange="changeName(this, 'nameused');">
        <option> Select Email Account</option>
<?php echo $email_options; ?>
      </select>
    </td>
  </tr>
  <tr><td id="t_subject">FROM</td></tr>
  <tr><td><input type="text" name="nameused" id="nameused" size="35" value="" /></td></tr>

Share this post


Link to post
Share on other sites

mmm i tried that keep getting the following runtime error

 

Parse error: syntax error, unexpected ')' in /home/asia/public_html/office/emailsend.php on line 9

 

<?php
include("../checkadminlogin.php");
include("../../global/connection.php");

  $sql = "select intNameID, email,fname, lname, place from tblpersondetails where place='A' order by intNameID ASC";
  $temps01=$DB_site->query($sql);
  while ($row=$DB_site->fetch_array($temps01))
  {
    $selected = () ? ' selected="selected"' : '';
    $email_options .= "      <option value=\"{$row['intNameID']}\"$selected>{$row['email']}</option>\n";
    $js_names_array .= "names[{$row['intNameID']}] = '{$row['fname']} {$row['lname']}';\n";
  }
?>

Share this post


Link to post
Share on other sites

Ah, that's what happens when you decide to make a last minute change before posting!

 

    $selected = ($nameid==$row["intNameID"]) ? ' selected="selected"' : '';

Share this post


Link to post
Share on other sites

mm thanks almost right

 

now I am getting the error message wont display name little warning sign bottom left of the browser with exlamation mark says

 

Line 110

Char: 1

Error: object expected

 

this error is in this area could be in the <script> or the table tags somethinng very small but i think it is in the <script tags>

 

 

Share this post


Link to post
Share on other sites

thanks for the help here javascript really isn't my thing I have been looking through this played around with the names a bit but can't get it to work. But looking at it nameID $nameid $aid don't make sense to me if you could please have another quick look when you get a chance thanks for the help.

Share this post


Link to post
Share on other sites

I don't have your database and data to test against. The code orked for me in a mock page I put together. If there is an error on a particular line it would be helpful for you to provide the code arount that particular line (and indicating the exact line).

 

But looking at it nameID $nameid $aid don't make sense to me...

 

Where does $aid appear in the code? As for the nameID variables, I'm not sure what the confusion is. nameID is the javascript variable that is set according to the value of the select field (which is generated from the PHP variable $row['intNameID']. As for $nameid, that variable appeared in your original code. But, the code provided didn't show how/where it was set. I assumed it was set via a POST or GET variable in order to set one of the Select field options as th4e default selected value.

 

For further help, please provide the current PHP code AND post the HTML output of that code and the errors you are getting.

Share this post


Link to post
Share on other sites

Hi here is the current code also the database sql the page loads fine when I select an emal account nothing happens doesn't fill the  from input field and the little javascript error box appears bottom left of the browser yellow triangle with exclamation mark when I click on it the error box pops up and says error the $aid i was refering to was in the table script you posted on line  <select name="aid" id="aid" onchange="changeName(this, 'nameused');">

 

Line: 109

Char: 38

Error: Expected Identifier

Code: 0

 

Here is the database code

 

-- 
-- Table structure for table `tblpersondetails`
-- 

CREATE TABLE `tblpersondetails` (
  `intNameID` int(11) NOT NULL auto_increment,
  `email` varchar(255) default NULL,
  `fname` varchar(255) default NULL,
  `lname` varchar(255) default NULL,
  `place` varchar(255) default NULL,
  `dtAddedOn` datetime default NULL,
  `dtUpdatedOn` datetime default NULL,
  PRIMARY KEY  (`intNameID`)
) ENGINE=MyISAM  DEFAULT CHARSET=tis620 AUTO_INCREMENT=1 ;

-- 
-- Dumping data for table `tblpersondetails`
-- 


-- --------------------------------------------------------

 

Here is the full page code

 

<?PHP
include("../checkadminlogin.php");
include("../../global/connection.php");

  $sql = "select intNameID, email,fname, lname, place from tblpersondetails where place='A' order by intNameID ASC";
  $temps01=$DB_site->query($sql);
  while ($row=$DB_site->fetch_array($temps01))
  {
    $selected = ($nameid==$row["intNameID"]) ? ' selected="selected"' : '';
    $email_options .= "      <option value=\"{$row['intNameID']}\"$selected>{$row['email']}</option>\n";
    $js_names_array .= "names[{$row['intNameID']}] = '{$row['fname']} {$row['lname']}';\n";
  }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>APA</title>
<link rel="stylesheet" type="text/css" href="office.css" />
<script type="text/javascript">>

var names = new Array();
<?php echo $js_names_array; ?>

function changeName(selObj, inputID)
{
    nameID = selObj[selObj.selectedIndex].value;
    document.getElementById(inputID).value = (names[nameID]) ? names[nameID] : '';
    return;
}

</script>

</head>

<body>
<div id="wrapper">
         <div id="logoheader">
         </div>
         
<div id="topmenu">
<div id="outside">
<ul id="navigation-1">
   <li><a href="#" title="Administration" target="_self" ><b>Administration</b></a></li>
      <ul class="navigation-2">
         <li><a href="../upprices.php" title="Update Price" target="_self" >Update Price</a></li>
         <li><a href="../upnews.php" title="Update News" target="_self" >Update News</a></li>
         <li><a href="sendemail.php" title="Send An Email" target="_self" >Send An Email</a></li>
         <li><a href="#" title="Send A Front" target="_self" >Send A Front</a></li>
      <ul class="navigation-3">
         <li><a href="sf1.php" title="sf1" target="_self" >SF1</a></li>
         <li><a href="sf2" title="sf2" target="_self" >   SF2</a></li>
      </ul>
         <li><a href="#" title="SFF" target="_self" >SFF</a></li>
      <ul class="navigation-3">
         <li><a href="sff1.php" title="1T" target="_self" >1T</a></li>
         <li><a href="sff2" title="2T" target="_self" >   2T</a></li>
      </ul>
         <li><a href="#" title="SAL" target="_self" >SAL</a></li>
      <ul class="navigation-3">
         <li><a href="Sl1.php" title="1Tr" target="_self" >1Tr</a></li>
         <li><a href="sl2" title="2Tr" target="_self" >   2Tr</a></li>
         <li><a href="sL3.php" title="3Tr" target="_self" >   3Tr</a></li>
         <li><a href="sl4" title="4Tr" target="_self" >   4Tr</a></li>
         <li><a href="sl5.php" title="5Tr" target="_self" >   5Tr</a></li>
         <li><a href="sl6" title="6Tr" target="_self" >   6Tr</a></li>
      </ul>
        <li><a href="sendnews.php" title="Send Newsletter" target="_self" >Send Newsletter</a></li>
        <li><a href="../viewfr.php" title="Update" target="_self" >View Fr</a></li>
        <li><a href="#" title="Templates" target="_self" >Templates</a></li>
      <ul class="navigation-3">
         <li><a href="../admin/updateemail.php" title="Update Email" target="_self" >Update Email</a></li>
         <li><a href="../admin/upfr1" title="Update Front 1" target="_self" >Up Fr 1</a></li>
         <li><a href="../admin/upfr2.php" title="Up Fr 2" target="_self" >Up Fr 2</a></li>
         <li><a href="../admin/upfrf.php" title="Up Fr F 1" target="_self" >Up Fr F 1</a></li>
         <li><a href="../admin/upfrf2.php" title="Up Fr F 2" target="_self" >Up Fr F 2</a></li>
         <li><a href="../admin/upl1.php" title="Up L1" target="_self" >Up L1</a></li>
         <li><a href="../admin/upl2.php" title="Up L2" target="_self" >Up L2</a></li>
         <li><a href="../admin/upl3.php" title="Up L3" target="_self" >Up L3</a></li>
         <li><a href="../admin/upl4.php" title="Up L4" target="_self" >Up L4</a></li>
      </ul>
        <li><a href="../sendnews.php" title="Update" target="_self" >Up Bank</a></li>
      </ul>
   </li>

   <li><a href="../users_view.php" title="New Acc" target="_self" ><b>New Acc</b></a>

   </li>

   <li><a href="#" title="Cl Acc" target="_self" ><b>Cl Acc</b></a>
      <ul class="navigation-2">
         <li><a href="../accounts_view.php" title="Active" target="_self" >Active</a></li>
         <li><a href="../suacc.php" title="Suspended" target="_self" >Suspended</a></li>
      </ul>
   </li>

   <li><a href="#" title="T Data" target="_self" ><b>T Data</b></a>
      <ul class="navigation-2">
         <li><a href="../tdata.php" title="Add T Data" target="_self" >Add T Data</a></li>
         <li><a href="../cur.php" title="Current " target="_self" >Current</a></li>
         <li><a href="../exp.php" title="Expired " target="_self" >Expired</a></li>
      </ul>
   </li>
   <li><a href="../logout.php" title="Logout" target="_self" ><b>Logout</b></a>
</ul>
</div>		 

		   
	 </div>





	 <div id="content">
<h1>ADMINISTRATION</h1>

<h2>SEND AN EMAIL</h2>
<table class="sendemail" >
<form name="sendemail" method="post" action="emailview.php" onsubmit="return v.exec()">
  <tr><td id="t_subject">EMAIL<br /></td></tr>
  <tr> 
    <td>
      <select name="aid" id="aid" onchange="changeName(this, 'nameused');">
        <option> Select Email Account</option>
<?php echo $email_options; ?>
      </select>
    </td>
  </tr>
  <tr><td id="t_subject">FROM</td></tr>
  <tr><td><input type="text" name="nameused" id="nameused" size="35" value="" /></td></tr>

<tr><td id="t_subject">SUBJECT<br /></td></tr>
<tr><td><input type="text" name="emailsubject" size="35" value=""/></td></tr>
<tr><td id="t_emailbody">EMAIL BODY<br /></td></tr>
<tr><td><textarea type="text" name="emailbody" /></textarea></td></tr>
<tr><td></td></tr>
<tr>  <td colspan="2">
<input class="button" name="submit" type="submit"  value="Preview Email" /> 
<input class="button" type="reset" name="reset" value="Reset Form" />
<br /></td>
</tr>
</table>
</form> 



<br />
<br />
</div> <!--END CONTENT-->






	 <div id="footer">
	       
		  <center> <p>©APA 2008  </p></center>		
		    
     </div>   </div>
   
</body>
</html>

 

 

 

 

Share this post


Link to post
Share on other sites

Hi here is the current code also the database sql the page loads fine when I select an emal account nothing happens doesn't fill the  from input field and the little javascript error box appears bottom left of the browser yellow triangle with exclamation mark when I click on it the error box pops up and says error the $aid i was refering to was in the table script you posted on line  <select name="aid" id="aid" onchange="changeName(this, 'nameused');">

 

Line: 109

Char: 38

Error: Expected Identifier

Code: 0

 

Um, ok. A little punctuation would really help.

 

1) I also asked "... AND post the HTML output of that code ..." The error you posted is only helpful if I can see the HTML code. Line 109 means nothing if I can't see where 109 is inthe HTML output.

 

2) You asked about $aid (which would be a PHP variable, and that is not in the code I posted. There is the Select field with the name of "aid", but that was in the code you posted, so I kept it that way.

 

I don't see any obvious error inthe PHP code. So post the HTML output (or provide a link to the page) and I can help furhter.

Share this post


Link to post
Share on other sites

hi sorry i didnt undertand i do now here is the html output code from view source

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>APA</title>
<link rel="stylesheet" type="text/css" href="office.css" />
<script type="text/javascript">>

var names = new Array();
names[2] = 'Paul Morris';
names[3] = 'James Smith';

function changeName(selObj, inputID)
{
    nameID = selObj[selObj.selectedIndex].value;
    document.getElementById(inputID).value = (names[nameID]) ? names[nameID] : '';
    return;
}

</script>

</head>

<body>
<div id="wrapper">
         <div id="logoheader">
         </div>
         
<div id="topmenu">
<div id="outside">
<ul id="navigation-1">
   <li><a href="#" title="Administration" target="_self" ><b>Administration</b></a></li>
      <ul class="navigation-2">
         <li><a href="../upprices.php" title="Update Price" target="_self" >Update Price</a></li>
         <li><a href="../upnews.php" title="Update News" target="_self" >Update News</a></li>
         <li><a href="sendemail.php" title="Send An Email" target="_self" >Send An Email</a></li>
         <li><a href="#" title="Send A Front" target="_self" >Send A Front</a></li>
      <ul class="navigation-3">
         <li><a href="sf1.php" title="sf1" target="_self" >SF1</a></li>
         <li><a href="sf2" title="sf2" target="_self" >   SF2</a></li>
      </ul>
         <li><a href="#" title="SFF" target="_self" >SFF</a></li>
      <ul class="navigation-3">
         <li><a href="sff1.php" title="1T" target="_self" >1T</a></li>
         <li><a href="sff2" title="2T" target="_self" >   2T</a></li>
      </ul>
         <li><a href="#" title="SAL" target="_self" >SAL</a></li>
      <ul class="navigation-3">
         <li><a href="Sl1.php" title="1Tr" target="_self" >1Tr</a></li>
         <li><a href="sl2" title="2Tr" target="_self" >   2Tr</a></li>
         <li><a href="sL3.php" title="3Tr" target="_self" >   3Tr</a></li>
         <li><a href="sl4" title="4Tr" target="_self" >   4Tr</a></li>
         <li><a href="sl5.php" title="5Tr" target="_self" >   5Tr</a></li>
         <li><a href="sl6" title="6Tr" target="_self" >   6Tr</a></li>
      </ul>
        <li><a href="sendnews.php" title="Send Newsletter" target="_self" >Send Newsletter</a></li>
        <li><a href="../viewfr.php" title="Update" target="_self" >View Fr</a></li>
        <li><a href="#" title="Templates" target="_self" >Templates</a></li>
      <ul class="navigation-3">
         <li><a href="../admin/updateemail.php" title="Update Email" target="_self" >Update Email</a></li>
         <li><a href="../admin/upfr1" title="Update Front 1" target="_self" >Up Fr 1</a></li>
         <li><a href="../admin/upfr2.php" title="Up Fr 2" target="_self" >Up Fr 2</a></li>
         <li><a href="../admin/upfrf.php" title="Up Fr F 1" target="_self" >Up Fr F 1</a></li>
         <li><a href="../admin/upfrf2.php" title="Up Fr F 2" target="_self" >Up Fr F 2</a></li>
         <li><a href="../admin/upl1.php" title="Up L1" target="_self" >Up L1</a></li>
         <li><a href="../admin/upl2.php" title="Up L2" target="_self" >Up L2</a></li>
         <li><a href="../admin/upl3.php" title="Up L3" target="_self" >Up L3</a></li>
         <li><a href="../admin/upl4.php" title="Up L4" target="_self" >Up L4</a></li>
      </ul>
        <li><a href="../sendnews.php" title="Update" target="_self" >Up Bank</a></li>
      </ul>
   </li>

   <li><a href="../users_view.php" title="New Acc" target="_self" ><b>New Acc</b></a>

   </li>

   <li><a href="#" title="Cl Acc" target="_self" ><b>Cl Acc</b></a>
      <ul class="navigation-2">
         <li><a href="../accounts_view.php" title="Active" target="_self" >Active</a></li>
         <li><a href="../suacc.php" title="Suspended" target="_self" >Suspended</a></li>
      </ul>
   </li>

   <li><a href="#" title="T Data" target="_self" ><b>T Data</b></a>
      <ul class="navigation-2">
         <li><a href="../tdata.php" title="Add T Data" target="_self" >Add T Data</a></li>
         <li><a href="../cur.php" title="Current " target="_self" >Current</a></li>
         <li><a href="../exp.php" title="Expired " target="_self" >Expired</a></li>
      </ul>
   </li>
   <li><a href="../logout.php" title="Logout" target="_self" ><b>Logout</b></a>
</ul>
</div>		

		   
	</div>





	<div id="content">
<h1>ADMINISTRATION</h1>

<h2>SEND AN EMAIL</h2>
<table class="sendemail" >
<form name="sendemail" method="post" action="emailview.php" onsubmit="return v.exec()">
  <tr><td id="t_subject">EMAIL<br /></td></tr>
  <tr> 
    <td>
      <select name="aid" id="aid" onchange="changeName(this, 'nameused');">
        <option> Select Email Account</option>
      <option value="2">paul.morris@yahoo.com</option>
      <option value="3">james.smith@hotmail.com</option>
      </select>
    </td>
  </tr>
  <tr><td id="t_subject">FROM</td></tr>
  <tr><td><input type="text" name="nameused" id="nameused" size="35" value="" /></td></tr>

<tr><td id="t_subject">SUBJECT<br /></td></tr>
<tr><td><input type="text" name="emailsubject" size="35" value=""/></td></tr>
<tr><td id="t_emailbody">EMAIL BODY<br /></td></tr>
<tr><td><textarea type="text" name="emailbody" /></textarea></td></tr>
<tr><td></td></tr>
<tr>  <td colspan="2">
<input class="button" name="submit" type="submit"  value="Preview Email" /> 
<input class="button" type="reset" name="reset" value="Reset Form" />
<br /></td>
</tr>
</table>
</form> 



<br />
<br />
</div> <!--END CONTENT-->






	<div id="footer">
	       
		  <center> <p>©APA 2008  </p></center>		
		    
     </div>   </div>
   
</body>
</html>

Share this post


Link to post
Share on other sites

hi I removed the onsubmit="return v.exec()" on line 109

 

now it says there is the same problem on line 112 which would be this line says object expected.

 

<select name="aid" id="aid" onchange="changeName(this, 'nameused');">

Share this post


Link to post
Share on other sites

on loading the page it seems fine with no errors it is when you select the email account it produced the error on line 112

Share this post


Link to post
Share on other sites

Almost working now using the following code on select the email address it returns a value in the required name field below but it keeps showing all the names returned by the echo php not just one?

 

header code

 

<?PHP
include("../checkadminlogin.php");
include("../../global/connection.php");

  $sql = "select intNameID, email,fname, lname, place from tbladvisorsdetails where place='A' order by intNameID ASC";
  $temps01=$DB_site->query($sql);
  while ($row=$DB_site->fetch_array($temps01))
  {
    $selected = ($nameid==$row["intNameID"]) ? ' selected="selected"' : '';
    $email_options .= "      <option value=\"{$row['intNameID']}\"$selected>{$row['email']}</option>\n";
   $js_names_array .= "names[{$row['intNameID']}] = '{$row['fname']} {$row['lname']}';\n";
  }
?>

 

javascript code

 

<script type="text/javascript">
var names = new Array();
<?php echo $js_names_array; ?>


  function changeName(value) {
  document.getElementById('USEDname').value = (names);
}
</script>

 

form code

<form name="sendemail" method="post" action="emailview.php" ">
<tr><td id="t_subject">EMAIL<br /></td></tr>
  <tr> 
    <td>
   <select name="email" id="SBoxEmail" onchange="changeName(this.value)">
        <option> Select Email Account</option>
<?php echo $email_options; ?>
      </select>
    </td>
  </tr>
  <tr><td id="t_subject">FROM</td></tr>
  <tr><td><input type="text" name="nameused" id="USEDname" size="35" value="" /></td></tr>

 

 

Share this post


Link to post
Share on other sites

OK, there was a simple typo in your code. I'm not sure of eveything you've been changing, but if you go back to the "full page code" you posted in Reply #8, you just need to make one small change.

 

On line 21 change this

<script type="text/javascript">>

 

To this

<script type="text/javascript">

 

Note there was an extra closing bracket '>'. That was the problem.

Share this post


Link to post
Share on other sites

thank you wow such a small thing that typo actually came from reply one but not to worry. :) Thanks alot for your patience and help much appreciated

Share this post


Link to post
Share on other sites

As my sig states:

I do not always test the code I provide, so there may be some syntax errors.

 

As long as we got it worked out, that's all that matters!

Share this post


Link to post
Share on other sites

having one other samll problem with this. when i submit and try to retrieve the value of email to display it is only diplaying the inNameID number 2 or 3 not the email address if i change the script around the script stops working??

Share this post


Link to post
Share on other sites

Well the "value" of those select elements is the id. The text between the OPTION tags is just the text/label associated with the value. But,since you know the ID you can simply query the database for the email address. In fact, if this page will be externally accessible I would suggest removing the actual email address from the page entirely - otherwise spambots will pick them up and innundate those email boxes.

 

 

Share this post


Link to post
Share on other sites

hi i already tried this by doing this it returns nothing

 

<h2>PREVIEW EMAIL</h2>

<table class="users" >

<?

$sql = "select intNameID, email, place from tblpersondetails where intNameID='email'";

$temps01=$DB_site->query($sql);

while ($row=$DB_site->fetch_array($temps01))

?>

<form name="email" method="post" action="emailsub.php" onsubmit="return v.exec()">

<tr><td id="t_subject">FROM:  <?=$row["email"]?></td></tr>

 

 

As far as SPAMBOTS or any type of BOT are concerned they do not have permission to access this part of the site so that will not be a problem

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.