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
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Wed Aug 05, 2009 4:00 pm
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?
Last edited by
Benjamin on Thu Aug 06, 2009 6:10 am, edited 1 time in total.
Reason: Changed code type from text to php.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Thu Aug 06, 2009 3:10 am
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...
Last edited by
Benjamin on Thu Aug 06, 2009 6:11 am, edited 1 time in total.
Reason: Changed code type from text to javascript.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
turbolemon
Forum Commoner
Posts: 70 Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK
Post
by turbolemon » Thu Aug 06, 2009 4:30 am
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>
...
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Thu Aug 06, 2009 4:45 am
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/>
Last edited by
Benjamin on Thu Aug 06, 2009 6:12 am, edited 1 time in total.
Reason: Changed code type from text to html.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
turbolemon
Forum Commoner
Posts: 70 Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK
Post
by turbolemon » Thu Aug 06, 2009 5:00 am
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.
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Thu Aug 06, 2009 5:13 am
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.
Last edited by
Benjamin on Thu Aug 06, 2009 6:13 am, edited 1 time in total.
Reason: Changed code type from text to javascript.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
turbolemon
Forum Commoner
Posts: 70 Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK
Post
by turbolemon » Thu Aug 06, 2009 5:44 am
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.
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Thu Aug 06, 2009 5:47 am
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.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
turbolemon
Forum Commoner
Posts: 70 Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK
Post
by turbolemon » Thu Aug 06, 2009 5:54 am
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.
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Thu Aug 06, 2009 5:56 am
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 \".
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
turbolemon
Forum Commoner
Posts: 70 Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK
Post
by turbolemon » Thu Aug 06, 2009 6:10 am
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!
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Thu Aug 06, 2009 6:15 am
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.
Last edited by
Benjamin on Thu Aug 06, 2009 6:25 am, edited 1 time in total.
Reason: Changed code type from text to php.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Aug 06, 2009 6:17 am
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>
";
}
simonmlewis
DevNet Master
Posts: 4435 Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:
Post
by simonmlewis » Thu Aug 06, 2009 6:21 am
Well yes, if you don't want the characters restricted beyond 80.
But I do. And that isn't working here.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Thu Aug 06, 2009 6:25 am
As I said, works for me. This has become a client side issue. What browser are you using?