/    Sign up×
Community /Pin to ProfileBookmark

PHP/mySQL query with AJAX

Hi,

I have this PHP/mySQL query that populates a dropdown

[code=php]

<p class=”textbox”><label for=”company”>company</label><select name=”company” id=”company”>
<?php
$r_company = mysql_query(“SELECT id,company_name,company_url FROM companies”);
echo(mysql_error());
while($r_company = mysql_fetch_assoc($r_company))
{
echo(“<option value=”” . $r_company[‘id’] . “”>” . $r_company[‘company_name’] . “</option>”.”n”);
}
?>
</select></p>
<p class=”textbox”><label for=”url”>Url</label><input type=”text” name=”url” id=”url” value=”<?php /* url from select*/ ?>” /></p>

[/code]

Is there a way, Im asuming with Javascript / AJAX that depending on the selection from the drop down that i can populate the text input “url”??

Thanks

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@chazzyMay 17.2006 — you don't need ajax.

<i>
</i>var company = document.getElementById('company');//gets the company select list
company.onchange=function(){
document.getElementById('url').value=company.value;
}


Should work, didn't test it.
Copy linkTweet thisAlerts:
@SheldonauthorMay 18.2006 — I dont understand how or where to implement it, sorry
Copy linkTweet thisAlerts:
@chazzyMay 18.2006 — your file could look as simple as this:

<i>
</i>&lt;html&gt;&lt;head&gt;&lt;title&gt;Sheldon's page&lt;/title&gt;
&lt;script language="javascript"&gt;
window.onload=function(){
var company = document.getElementById('company');//gets the company select list
company.onchange=function(){
document.getElementById('url').value=company.value;
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p class="textbox"&gt;&lt;label for="company"&gt;company&lt;/label&gt;&lt;select name="company" id="company"&gt;
&lt;?php
$r_company = mysql_query("SELECT id,company_name,company_url FROM companies");
echo(mysql_error());
while($r_company = mysql_fetch_assoc($r_company))
{
echo("&lt;option value="" . $r_company['id'] . ""&gt;" . $r_company['company_name'] . "&lt;/option&gt;"."n");
}
?&gt;
&lt;/select&gt;&lt;/p&gt;
&lt;p class="textbox"&gt;&lt;label for="url"&gt;Url&lt;/label&gt;&lt;input type="text" name="url" id="url" value="" /&gt;&lt;/p&gt;
&lt;/body&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@SheldonauthorMay 18.2006 — Got it, Thanks Chazzy
Copy linkTweet thisAlerts:
@chazzyMay 18.2006 — Also, you can create an array in javascript and store each company's url in that array, with keys associating with each company_id.
×

Success!

Help @Sheldon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...