/    Sign up×
Community /Pin to ProfileBookmark

Ajax: Handling a response that’s in the form of javascript code

Two questions on this subject:

First, I want the PHP to send this code for example:

document.getElementById(input).innerHTML =”<input type=”hidden” value =”something”>”

Notice that I already have the characters “, but if I echo that in php it will echo a simple “. How do I bypass that? ” ?

Let’s say I get that working:

How do I handle the response? How do I actually “run” that bit of javascript once i receive it?

Thanks.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 27.2006 — [code=php]<?php
echo <<<END
document.getElementById(input).innerHTML = '<input type="hidden" value="something">';
END;
?>[/code]

When received via XMLHttpRequest, you must use [b]eval()[/b] to execute that code.
Copy linkTweet thisAlerts:
@MonsieurRenardauthorJun 27.2006 — Many thanks as always.
Copy linkTweet thisAlerts:
@phpnoviceJun 27.2006 — You're welcome.

Cheers.
Copy linkTweet thisAlerts:
@MonsieurRenardauthorJun 27.2006 — I'm getting an error:

if (strEq($answers[$j][4], "Y")){echo &lt;&lt;&lt;END # error on this line
document.getElementById('message_$j').innerHTML = 'Correct';
END;}


parse error, expecting '," or ';"

What might be causing this?
Copy linkTweet thisAlerts:
@phpnoviceJun 28.2006 — Don't try to compress your code so much:
[code=php]if (strEq($answers[$j][4], "Y")){
echo <<<END
document.getElementById('message_$j').innerHTML = 'Correct';
END;
}[/code]
Copy linkTweet thisAlerts:
@MonsieurRenardauthorJun 28.2006 — Still has the same error on this line: echo <<<END

It doesn't seem to like this format. Is there any reason why it's doing this? Is there an alternative solution?
Copy linkTweet thisAlerts:
@phpnoviceJun 28.2006 — Do you have it inside of PHP script tags?
Copy linkTweet thisAlerts:
@MonsieurRenardauthorJun 28.2006 — Yeah, the rest of the code works fine.

It also works fine if I put it in simple echo "stuff" format but later I will need to echo longer stuff and characters like "
Copy linkTweet thisAlerts:
@phpnoviceJun 28.2006 — Well, that [b]echo[/b] statement format comes right out of the PHP manual.

So, I don't know what to tell you. What version of PHP do you run?
Copy linkTweet thisAlerts:
@netbuddyJun 28.2006 — It would be easier if you simply had a text file that you call and then pass into the browser interpreter.

By the looks of it, your request is returing something that the intrepreter in the browser does not understand, namely php when its expecting javascript.
×

Success!

Help @MonsieurRenard 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 4.27,
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,
)...