Select Field Won't Insert Name Into Text Box

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

I can't get the select field to insert in the title name when you select a title and I don't know why it won't.

Code: Select all

 
<html>
<head>
<script src="selectitle.js"></script>
</head>
 
 
<?php
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());
 
if (!mysql_select_db("?", $link)) {
echo 'Could not select database';
exit;
}
?>
<center><table><tr><td>Title:</td><td>
<?php
echo '<select onchange="showTitle(this.value)">';
$data = mysql_query("SELECT titlename FROM titles");
while($row = mysql_fetch_assoc($data)) {
    echo '<option value="'.$row['titlename'].'">'.$row['titlename'].'</option>';
}
echo '</select>'; 
?></td></tr></table></center>
<?php
$q=$_GET["q"];
 
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());
 
if (!mysql_select_db("?", $link)) {
echo 'Could not select database';
exit;
}
 
$sql="SELECT * FROM titles WHERE id = '".$q."'";
 
$result = mysql_query($sql);
?>
<center><table border=1 cellpadding=5 cellspacing=0 width=350>
<font color="#CC0000"><h2><center>Edit a Title</h2></center></font>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<tr><td>Title Name:</td><td>
<input type="text" name="titlename" maxlength="60" value=" <?php echo $row['titlename']; ?>">
</td></tr>
 
</html>
 
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Select Field Won't Insert Name Into Text Box

Post by Chalks »

you have references to:
showTitle()
and
selectitle.js

Where is the showTitle() function?
What is the selectitle.js file?



Easiest way to change the title with javascript is "document.title = 'Your title here';"
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

Here is selecttitle.js file:

Code: Select all

var xmlHttp function showtitle(str){ xmlHttp=GetXmlHttpObject();if (xmlHttp==null)  {  alert ("Your browser does not support AJAX!");  return;  } var url="edittitle.php";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);} function stateChanged() { if (xmlHttp.readyState==4){ document.getElementById("txtHint").innerHTML=xmlHttp.responseText;}} function GetXmlHttpObject(){var xmlHttp=null;try  {  // Firefox, Opera 8.0+, Safari  xmlHttp=new XMLHttpRequest();  }catch (e)  {  // Internet Explorer  try    {    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");    }  catch (e)    {    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");    }  }return xmlHttp;}
Last edited by RobertGonzalez on Tue Aug 05, 2008 4:47 pm, edited 1 time in total.
Reason: It helps everyone when you follow the rules and use code tags when posting code
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Select Field Won't Insert Name Into Text Box

Post by Chalks »

I don't see what this is referencing:
"document.getElementById("txtHint").innerHTML=xmlHttp.responseText;"

Where is the element with id "txtHint"?

Also, where specifically are you trying to place this information?
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

Okay to summarize what I'm trying to do is when the admin selects a title in the select field it'll go to my database table and grab that info and will post it in the text fields. I'm just trying to get it to post the name for now and then I'll work on the other parts of it.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Select Field Won't Insert Name Into Text Box

Post by Chalks »

CoolAsCarlito wrote:Okay to summarize what I'm trying to do is when the admin selects a title in the select field it'll go to my database table and grab that info and will post it in the text fields. I'm just trying to get it to post the name for now and then I'll work on the other parts of it.
Here are a few things:
the variable $q _needs_ to be escaped. That's a serious security hole. look into using at the very least mysql_real_escape_string().

Second, you seem to be mixing ajax along with your regular scripting, and it's making things confusing. I can't really tell from looking at your script what is supposed to be happening when.

Finally, when the user selects a title, the js function showtitle() is called. This function changes the element labled "txtHint", but I don't see that element _anywhere_.
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

Well as for the txtHint what I did was take what I found on this site: http://w3schools.com/php/php_ajax_database.asp and didn't know what to change.
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Select Field Won't Insert Name Into Text Box

Post by Chalks »

Try this:

Code: Select all

 <html>
<head>
<script src="selectitle.js"></script>
</head>
<body>
 
<?php
// Connects to your Database
$link = mysql_connect("?", "?", "?") or die(mysql_error());
mysql_select_db("?",$link) or die(mysql_error());
 
if (!mysql_select_db("?", $link)) {
echo 'Could not select database';
exit;
}
?>
<center><table><tr><td>Title:</td><td>
<?php
echo '<select onchange="showTitle(this.value)">';
$data = mysql_query("SELECT titlename FROM titles");
while($row = mysql_fetch_assoc($data)) {
    echo '<option value="'.$row['titlename'].'">'.$row['titlename'].'</option>';
}
echo '</select>'; 
?></td></tr></table></center>
 
 
<center><table border=1 cellpadding=5 cellspacing=0 width=350>
<font color="#CC0000"><h2><center>Edit a Title</h2></center></font>
<tr>
 
<td>Title Name:</td>
<td>
  <form name="change" action="Your/Script/Location/FileName.php" method="post">
    <input type="text" name="titlename" maxlength="60" value="" /><br />
    <input type="submit" value="submit" />
  </form>
</td>
 
</tr>
</table>
 
</body>
</html>
 
Additionaly, change the line

Code: Select all

document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
to

Code: Select all

document.forms['change'].titlename.value = xmlHttp.responseText;
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

It still isn't putting the titlename into the text field.

Find it now at kansasoutlawwrestling.com/edittitle3.php
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Select Field Won't Insert Name Into Text Box

Post by Chalks »

change the onchange value to this:

Code: Select all

onchange="showtitle(this.options[this.selectedIndex].value)"
edit: showTitle. capitalize the T. Don't know if it matters, but might as well make sure it's exactly right.
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

bummer for some reason it's just not working
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Select Field Won't Insert Name Into Text Box

Post by Chalks »

selectitle.js isn't in that directory. It needs to be.
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

What directory?
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Select Field Won't Insert Name Into Text Box

Post by Chalks »

CoolAsCarlito wrote:What directory?
http://kansasoutlawwrestling.com/selectitle.js is a 404 error. However, edittitle3.php expects it to exist.

Alternatively, replace

Code: Select all

<script src="selectitle.js"></script>
with

Code: Select all

<script type="text/javascript">
//all your javascript code goes here.
</script>
CoolAsCarlito
Forum Contributor
Posts: 192
Joined: Sat May 31, 2008 3:27 pm
Contact:

Re: Select Field Won't Insert Name Into Text Box

Post by CoolAsCarlito »

Post Reply