Page 1 of 2
How do you use Javascript within echoed PHP code?
Posted: Wed Aug 05, 2009 4:00 pm
by simonmlewis
Code: Select all
if ($cookietype == "user" || $cookietype == "admin") {
echo "
<form method='post' name='myform' action='index.php?page=vehicleadded&menu=a_products' enctype='multipart/form-data'>
<input type='hidden' name='userid' value='$cookieid'>
<div class='edittitle'>Advert Heading:</div>
<input type='text' class='textbox' name='headline'><br/>
<div class='edittitle'>Advert Text:</div>
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown='limitText(this.form.description,this.form.countdown,80);'
onKeyUp='limitText(this.form.description,this.form.countdown,80);'></textarea>
You have <input readonly type='text' name='countdown' size='3' value='80'> characters left.</font>
<br/>
<div class='edittitle'>Advert Image [110px by 60px]</div>
<input name='photo' type='file' />
<br/>
<input type='submit' value='create advert' class='submit' onclick=\"javascript:return confirm('Click OK to confirm. Thank you.');\">
</form>
";
The "onKeyUp" here isn't working. I'm limiting how much text can be placed in the box.
The Javascript function is at the top of the page outside the <?php tag. How do I make this work?
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 3:10 am
by simonmlewis
Code: Select all
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
This is before <?php...
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 4:30 am
by turbolemon
Code: Select all
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown='limitText(this.form.description,this.form.countdown,80);'
onKeyUp='limitText(this.form.description,this.form.countdown,80);'>
</textarea>
The keyword "this" relates to the current element in it's own context. The textarea doesn't have a child form node, nor is a child of itself; you need to either pass the form from the document context, or access it directly with getElementById(). Assume you assign the form an ID of 'textentry':
Code: Select all
function limitText(formname,limitNum) {
form = document.getElementById(formname);
if (form.description.value.length > limitNum) {
form.description.value = form.description.value.substring(0, limitNum);
} else {
form.countdown.value = limitNum - form.description.value.length;
}
}
...
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown='limitText("textentry",80);'>
</textarea>
...
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 4:45 am
by simonmlewis
What about the countdown part, how will that work with the code changed?
Code: Select all
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown='limitText(this.form.description,this.form.countdown,80);'
onKeyUp='limitText(this.form.description,this.form.countdown,80);'></textarea>
You have <input readonly type='text' name='countdown' size='3' value='80'> characters left.</font>
<br/>
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 5:00 am
by turbolemon
Exactly the same, except it will actually work! The only thing changed is that instead of trying to pass two DOM element references via the function parameters you are passing one text string (the id of the form), which is selected from within the function.
I didn't test it. I have a feeling that you may need to swap the element name for the element ID (i.e. document.description becomes document.elm1) in the function.
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 5:13 am
by simonmlewis
This....
Code: Select all
<script language="javascript" type="text/javascript">
function limitText(formname,limitNum) {
form = document.getElementById(formname);
if (form.description.value.length > limitNum) {
form.description.value = form.description.value.substring(0, limitNum);
} else {
form.countdown.value = limitNum - form.description.value.length;
}
}
</script>
with this....
Code: Select all
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown='limitText('textentry',80);'
onKeyUp='limitText('textentry',80);'></textarea>
You have <input readonly type='text' name='countdown' size='3' value='80'> characters left.</font>
<br/>
.... doesn't do anything.
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 5:44 am
by turbolemon
Yes it does:
http://www.turbolemonmedia.co.uk/#form-test/
You have used the wrong quotations in your events on the textarea. You must either escape the inner quotes or use double quotes inside the single quotes. I swapped textarea for input but it makes no difference.
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 5:47 am
by simonmlewis
Code: Select all
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown=\"limitText('textentry',80);\"
onKeyUp=\"limitText('textentry',80);\"></textarea>
You have <input readonly type='text' name='countdown' size='3' value='80'> characters left.</font>
This is what I tend to do, but it's not working.
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 5:54 am
by turbolemon
Code: Select all
<p>
<script type="text/javascript">// <![CDATA[
function limitText(formname,limitNum) {
form = document.getElementById(formname);
if (form.description.value.length > limitNum) {
form.description.value = form.description.value.substring(0, limitNum);
} else {
form.countdown.value = limitNum - form.description.value.length;
}
}
// ]]>
</script>
</p>
<form id="textentry" action="index.php?page=vehicleadded&menu=a_products" enctype="multipart/form-data" method="post"> <input name="userid" type="hidden" value="$cookieid" />
<div class="edittitle">Advert Heading:</div>
<input class="textbox" name="headline" type="text" /><br />
<div class="edittitle">Advert Text:</div>
<input id="elm1" onkeydown="limitText('textentry',80);" onkeyup="limitText('textentry',80);" name="description" /> You have <input id="count" name="countdown" size="3" type="text" value="80" /> characters left. <br />
<div class="edittitle">Advert Image [110px by 60px]</div>
<input name="photo" type="file" /> <br /> <input class="submit" onclick="\" type="submit" value="create advert" /> </form>
Thats the complete code I put on the page. It's probably a case of copy-paste to make it work.
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 5:56 am
by simonmlewis
That won't work as u have " on it's own. If I put that in PHP it'll error, as " marks the end of an echoed line.
This is my whole problem really - for me, it doesn't like escaping it as \".
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 6:10 am
by turbolemon
You can use a heredoc:
Code: Select all
$form = <<<FRM
<p>
<script type="text/javascript">// <![CDATA[
function limitText(formname,limitNum) {
form = document.getElementById(formname);
if (form.description.value.length > limitNum) {
form.description.value = form.description.value.substring(0, limitNum);
} else {
form.countdown.value = limitNum - form.description.value.length;
}
}
// ]]>
</script>
</p>
<form id="textentry" action="index.php?page=vehicleadded&menu=a_products" enctype="multipart/form-data" method="post"> <input name="userid" type="hidden" value="{$cookieid}" />
<div class="edittitle">Advert Heading:</div>
<input class="textbox" name="headline" type="text" /><br />
<div class="edittitle">Advert Text:</div>
<input id="elm1" onkeydown="limitText('textentry',80);" onkeyup="limitText('textentry',80);" name="description" /> You have <input id="count" name="countdown" size="3" type="text" value="80" /> characters left. <br />
<div class="edittitle">Advert Image [110px by 60px]</div>
<input name="photo" type="file" /> <br /> <input class="submit" onclick="\" type="submit" value="create advert" /> </form>
FRM;
echo $form;
Bear in mind, you can have nothing after <<<FRM on the same line and nothing before or after the FRM; .. not even whitespace.
http://uk2.php.net/manual/en/language.t ... ax.heredoc
Hope that solves your problem!
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 6:15 am
by simonmlewis
Thanks - but there is a problem, as the page at the end redirects the user if they are not permitted to see it. Here is the entire Include file:
Code: Select all
<script type="text/javascript">
function limitText(formname,limitNum) {
form = document.getElementById(formname);
if (form.description.value.length > limitNum) {
form.description.value = form.description.value.substring(0, limitNum);
} else {
form.countdown.value = limitNum - form.description.value.length;
}
}
</script>
<!-- TinyMCE -->
<script type="text/javascript" src="js/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript">
tinyMCE.init({
mode : "textareas",
theme : "simple"
});
</script>
<!-- /TinyMCE -->
<script language="JavaScript">
var mikExp = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\'\`\~\=\"\,\|]/;
function charactercheck(val) {
var strPass = val.value;
var strLength = strPass.length;
var lchar = val.value.charAt((strLength) - 1);
if(lchar.search(mikExp) != -1) {
var tst = val.value.substring(0, (strLength) - 1);
val.value = tst;
}
}
</script>
<?php
$cookietype = $_COOKIE['type'];
$cookieid = $_COOKIE['userid'];
if ($cookietype == "user" || $cookietype == "admin") {
echo "
<form method='post' name='myform' action='index.php?page=vehicleadded&menu=a_products' enctype='multipart/form-data'>
<input type='hidden' name='userid' value='$cookieid'>
<div class='edittitle'>Advert Heading:</div>
<input type='text' class='textbox' name='headline'><br/>
<div class='edittitle'>Advert Text:</div>
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown=\"limitText('textentry',80);\"
onKeyUp=\"limitText('textentry',80);\"></textarea>
You have <input readonly type='text' name='countdown' size='3' value='80'> characters left.</font>
<br/>
<div class='edittitle'>Advert Image [110px by 60px]</div>
<input name='photo' type='file' />
<br/>
<input type='submit' value='create advert' class='submit' onclick=\"javascript:return confirm('Click OK. Thank you.');\">
</form>
";
}
else
{
echo "<meta http-equiv='Refresh' content='0 ;URL=index.html'>";
}
?>
They will be only for advertisers in the final version.
Note that other scripts, ie the onclick script, does work.
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 6:17 am
by Benjamin
I copied your code into a test file in order to debug it and it works fine:
Code: Select all
<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
if (limitField.value.length > limitNum) {
limitField.value = limitField.value.substring(0, limitNum);
} else {
limitCount.value = limitNum - limitField.value.length;
}
}
</script>
<?php
$cookieid = 'foo';
$cookietype = 'user';
if ($cookietype == "user" || $cookietype == "admin") {
echo "
<form method='post' name='myform' action='index.php?page=vehicleadded&menu=a_products' enctype='multipart/form-data'>
<input type='hidden' name='userid' value='$cookieid'>
<div class='edittitle'>Advert Heading:</div>
<input type='text' class='textbox' name='headline'><br/>
<div class='edittitle'>Advert Text:</div>
<textarea id='elm1' name='description' rows='6' cols='40'
onKeyDown='limitText(this.form.description,this.form.countdown,80);'
onKeyUp='limitText(this.form.description,this.form.countdown,80);'></textarea>
You have <input readonly type='text' name='countdown' size='3' value='80'> characters left.</font>
<br/>
<div class='edittitle'>Advert Image [110px by 60px]</div>
<input name='photo' type='file' />
<br/>
<input type='submit' value='create advert' class='submit' onclick=\"javascript:return confirm('Click OK to confirm. Thank you.');\">
</form>
";
}
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 6:21 am
by simonmlewis
Well yes, if you don't want the characters restricted beyond 80.
But I do. And that isn't working here.
Re: How do you use Javascript within echoed PHP code?
Posted: Thu Aug 06, 2009 6:25 am
by Benjamin
As I said, works for me. This has become a client side issue. What browser are you using?