Jump to content

simple php/java script logic


squiblo

Recommended Posts

The code below is supposed to put the variable $about_me in a textarea by using javascript but i have to change "<br />" to "\n" when it goes into the textarea tags i am getting my logic all wrong trying to do this, and i think i have things in the wrong places.

 

www.squiblo.com/test.php

 

<html>
<body>



<?php $about_me = "liam<br /><br />!!!!<br />woooo"; ?>
<?php $about_me = str_replace('<br />', '\n', $about_me); ?>


<script type="text/javascript">
function toggle_visibility(id)
{	
var div = document.getElementById(id);	
var text = document.getElementById("about_me_text");		

if(div.style.display == 'block' ) 
	{		
	div.style.display = 'none';		
	text.innerHTML = "<?php $about_me = str_replace('\n', '<br />', $about_me); ?><?php $about_me = str_replace('<br>', '<br />', $about_me); ?><?php echo $about_me; ?>";

	}	
	else 
	{		
	div.style.display = 'block';
	text.innerHTML = "<?php $about_me = str_replace('<br />', '\n', $about_me); ?><?php $about_me = str_replace('<br />', '\n', $about_me); ?><form action='' method='POST'><textarea cols='23' rows='6' name='textarea_about' id='textarea_about' style='font-family:arial;font-size:13px;overflow:hidden'><?php echo $about_me;?></textarea><br><input type='submit' name='submit_about' value='Save'>   <input type='button' name='cancel_about' value='Cancel' onClick=\"window.location.href='#'\"></form>";	
	}
}
</script>	


<a href='#' onclick="toggle_visibility('bob');"><font color='CornflowerBlue'>Edit</font></a>


<div id='about_me_test'>
<?php echo $about_me; ?>
</div>





</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/189474-simple-phpjava-script-logic/
Share on other sites

ok this is the strangest thing ive ever seen....

 

this following code works....

 

<?php $about me = "text<br />text"; ?>
<?php $about_me = str_replace('<br />', '<br>', $about_me); ?>
<script type="text/javascript">
function toggle_visibility(id)
{	
var div = document.getElementById(id);	
var text = document.getElementById("about_me_text");		

if(div.style.display == 'none' ) 
	{		
	div.style.display = 'block';		
	text.innerHTML = "<?php $about_me = str_replace('<br>', '\n', $about_me); ?><?php echo $about_error; ?><form action='' method='POST'><textarea cols='23' rows='6' name='textarea_about' id='textarea_about' style='font-family:arial;font-size:13px;overflow:hidden'><?php echo "$about_me";?></textarea><br><input type='submit' name='submit_about' value='Save'>   <input type='button' name='cancel_about' value='Cancel' onClick=\"window.location.href='content.php?content=myhub&myhub=mypage&mypage=mypage&cont=<?php echo $cont;?>'\"></form>";	
	}	
	else 
	{		
	div.style.display = 'none';
	text.innerHTML = "<?php $about_me = str_replace('\n', '<br />', $about_me); ?><?php echo $nothing.$about_me; ?>";
	}
}
</script>

 

 

but this does not work....

<?php
$about_me_query = mysql_query("SELECT * FROM page_set_data WHERE username='$username'");

while ($row = mysql_fetch_assoc($about_me_query))
{
$about_me = $row['about_me'];
}

if (!$about_me)
{
$about_me = "";
$nothing = "Write about yourself";
}
?>

<?php $about_me = str_replace('<br />', '<br>', $about_me); ?>
<script type="text/javascript">
function toggle_visibility(id)
{	
var div = document.getElementById(id);	
var text = document.getElementById("about_me_text");		

if(div.style.display == 'none' ) 
	{		
	div.style.display = 'block';		
	text.innerHTML = "<?php $about_me = str_replace('<br>', '\n', $about_me); ?><?php echo $about_error; ?><form action='' method='POST'><textarea cols='23' rows='6' name='textarea_about' id='textarea_about' style='font-family:arial;font-size:13px;overflow:hidden'><?php echo "$about_me";?></textarea><br><input type='submit' name='submit_about' value='Save'>   <input type='button' name='cancel_about' value='Cancel' onClick=\"window.location.href='content.php?content=myhub&myhub=mypage&mypage=mypage&cont=<?php echo $cont;?>'\"></form>";	
	}	
	else 
	{		
	div.style.display = 'none';
	text.innerHTML = "<?php $about_me = str_replace('\n', '<br />', $about_me); ?><?php echo $nothing.$about_me; ?>";
	}
}
</script>	

 

and the data in the database is exactly the same, i have also connected to the db further up in the page..

 

 

sometime u might have <br /> with line break, or with css style, and this wont get u work

<br style="line-height:100px;" /> << this wont work

 

so, sometime, people just save the raw textarea content into database

without converting them into html format

so, when u retrieve it out, they convert it to html format

because convert raw content to html is much easier than the reverse way

because html, u might have css, or html tag's property, like width=100%

u need to consider this, although ur sever might runs faster this way, cause users view posts more than modify, insert them

but u need to spend more time to program the reg expression, this spends time, and money

 

 

ok, i got what u did wrong

u have this

'\n' << the line break escape character

should be "\n"  << with double quote

because single quote wont fetch the escape characters

greatstar00 i changed the quotes to double quotes but then they both didnt work. What do you mean about the css  and uploading raw textarea because i dont quite understand? could anyone try and describe it differently please?

 

if this is needed, this is how i uploaded the data to the db...

if (isset($_POST['submit_about']))
{
if (isset($_POST['textarea_about']))
{
$textarea_about = nl2br(strip_tags($_POST['textarea_about']));

	if (strlen($textarea_about) < 301)
	{
		mysql_query("UPDATE page_set_data SET about_me='$textarea_about' WHERE username='$username'");
		header("location:http://www.squiblo.com/content.php?content=myhub&myhub=mypage&mypage=mypage&cont=$cont");
	}
	else
	$about_error = "<ul><li>Maximum of 300 characters.</li></ul>";

}

}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.