/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] header() php function "Content-Type"

Can someone please explain to me how to use header(“Content-Type: text/javascript”)? I want to have code on another page that has <script src=”” /> and I want to be able to access variables defined in the php.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@HuevoosJun 15.2006 — I think you can do <script src="script.php" type="text/javascript">
Copy linkTweet thisAlerts:
@shane_carrauthorJun 15.2006 — That doesn't answer my question. How do I access the variables, using either method?
Copy linkTweet thisAlerts:
@NogDogJun 15.2006 — What I was thinking of would go something like this:

On your PHP server, create a file which we'll call combo.php for this example:
[code=php]
<?php
header("Content-Type: text/javascript");
// 1. connect to database
// 2. create query (for this example assume it's called $query
$options = mysql_query($query);
$text = "<select name='combo'>";
while($row = mysql_fetch_array($options))
{
$text .= "<option>{$row[0]}</option>";
}
$text .= "</select>";
echo "document.write("$text");";
?>
[/code]

In your HTML document within the form, you would access it via the script tag:
[code=html]
<form>
<!-- various inputs go here, then for your javascript select element, do this -->
<script type="text/javascript" src="http://www.php.server/combo.php"></script>
<noscript><p>ERROR: This form requires that Javascript be enabled.</p></noscript>
<!-- rest of form follows here -->
</form>
[/code]

This solution has the obvious drawback that JavaScript is required for your form to be complet; thus the better solution is to get everything hosted on a PHP-enabled host.
Copy linkTweet thisAlerts:
@shane_carrauthorJun 15.2006 — :eek: It actually worked :eek:


Brilliant!!!!!!!!!!!!!!! Absolutely brilliant!!!!!!!!!!!!!!! It actually worked!!!!!!!!!!!!!!!!!! It's wonderful!!!!!!!!!!!!!! :rolleyes: I really like it!!!!!!!!!!!!!!!!!! ??????? NogDog, you are a genius!!!!!!!!!!!!!!!!!!!!!! Your answer was phenomenal!!!!!!!!!!! I made a few edits to make it fit my needs. Brilliant!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Excellent, outstanding, marvelous, magnificent, superlative, supernatural, exceptional, terrific, tremendous, super, amazing, fantastic, sensational, out of this world; What is the right adjective?

OMG, NogDog, your answer is brilliant!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! You have fixed my two-week long problem!

You know what? That is just amazing! Thank you!
Copy linkTweet thisAlerts:
@NogDogJun 15.2006 — Yeah, but did you like it? ?
×

Success!

Help @shane_carr 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.15,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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