mike12255 Posted October 26, 2009 Share Posted October 26, 2009 Hi, im new to javascript and its not doing what I wan so i was first wondering if my script looks proper: <script type="text/javascript"> function insert_text(var number){ if (number == 1){ var newtext = <?php $one;?> } if (number == 2){ var newtext = <?php $two;?> } if (number == 3){ var newtext = <?php $one;?> } document.editorform.editordata.value = newtext; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/179015-is-this-proper/ Share on other sites More sharing options...
Psycho Posted October 26, 2009 Share Posted October 26, 2009 Here's a tip, when using PHP to generate JavaScript, always check the rendered HTML to ensure the JavaScript looks like what you think it should. In this istance your PHP is not writign anything to the page, so the Javascript is setting the value to nothing no matter what the value of number is. Also, although having separate IF statements would work when they are all testing for different values of the same variable, I doubt many would consider that proper design. Try this: <script type="text/javascript"> function insert_text(var number) { var newtext = (number==2) ? <?php echo $two; ?> : <?php echo $one; ?>; document.editorform.editordata.value = newtext; } </script> Quote Link to comment https://forums.phpfreaks.com/topic/179015-is-this-proper/#findComment-944486 Share on other sites More sharing options...
mike12255 Posted October 26, 2009 Author Share Posted October 26, 2009 Thanks for the reply, ill certinally change things, However i cant get the javascript to change the textarea for some reason here is the code im using (I have tried the commented out method in the script too) the script is down inside the body tags: (I only have the alert in there so i know if its actually running the function, which it is) <?php if (!defined('NC_BASEPATH')) exit('No direct script access allowed'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>nc-cms | HTML Editor</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <meta name="robots" content="noindex" /> <meta name="robots" content="nofollow" /> <link rel="stylesheet" type="text/css" media="screen" href="system/css/editor.css"/> <link rel="stylesheet" type="text/css" media="screen" href="system/css/editor_html.css"/> <!--[if lt IE 7]><link rel="stylesheet" type="text/css" media="screen" href="system/css/ie.css"/><![endif]--> <script type="text/javascript" src="system/modules/tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ mode : "textareas", theme : "advanced", skin : "default", relative_urls : 0, plugins : "safari,table,advimage,advlink,inlinepopups,insertdatetime,media,searchreplace,paste,directionality,fullscreen,noneditable,xhtmlxtras,pagebreak", theme_advanced_buttons1 : "newdocument,|,bold,italic,underline,strikethrough,justifyleft,justifycenter,justifyright,justifyfull,bullist,numlist,indent,outdent,sub,sup,formatselect,fontselect,fontsizeselect,forecolor,backcolor,selectall,removeformat,|,code,cleanup,fullscreen", theme_advanced_buttons2 : "undo,redo,paste,pastetext,pasteword,search,|,image,link,unlink,inserttime,insertdate,charmap,media,pagebreak,|,tablecontrols,|,visualaid", theme_advanced_buttons3 : "", theme_advanced_toolbar_location : "top", theme_advanced_toolbar_align : "left", theme_advanced_statusbar_location : "bottom", theme_advanced_resizing : true, theme_advanced_blockformats: "p,pre,h1,h2,h3,h4,h5", content_css : "css/content.css" }); // onbeforeunload() does not work correctly in certain browsers. Disable this functionality if not using Firefox/Chrome. var confirmed_exit = true; if(!navigator.appName.indexOf("Netscape")) confirmed_exit = false; window.onbeforeunload = function () { if(!confirmed_exit) return "You have not saved yet. If you continue, your work will not be saved." } function save_confirmation() { var answer = confirm("Are you sure you want to save?\nAny changes you have made to the web page will go live."); if(answer) { confirmed_exit = true; document.editorform.submit(); } } function cancel_confirmation() { var answer = confirm("Are you sure you want to cancel?\nAny changes you have made to the web page will not be saved."); if(answer) { confirmed_exit = true; this.location.href = "<?php echo $_SERVER['HTTP_REFERER']; ?>"; } } function open_file_manager() { window.open('index.php?action=file_manager','insert_file','width=640,height=460,screenX=200,left=200,screenY=200,top=200,status=yes,menubar=no'); } </script> </head> <body> <div id="wrapper"> <div align="right"> <script type="text/javascript"> function insert(){ var newtext = "test"; alert("test"); document.getElementById("editordata").value = "test"; //document.editorform.editordata.value = "test"; } </script> <?php mysql_connect(NC_DB_HOST,NC_DB_USER,NC_DB_PASSWORD); mysql_select_db(NC_DB_DATABASE); echo "<SELECT NAME=\"history\">"; $i =1; while ($i != 4){ $sql = "SELECT * FROM ".NC_DB_PREFIX."content WHERE name= '".$name.$i."'"; $res = mysql_query($sql) or die (mysql_error()); $row = mysql_fetch_assoc($res); echo "<option onclick=\"insert() \" value = \"'".$i."'\">Version $i</option>"; if($i == 1){ $one = $row['content']; } if($i == 2){ $two = $row['content']; } if($i == 3){ $three = $row['content']; } $i++; } echo "</SELECT>"; ?> </div> <div id="editor"> <h1 title="Powered by nc-cms"><?php echo NC_WEBSITE_NAME; ?> </h1> <form name="editorform" id="editorform" method="post" action="index.php?action=save&ref=<?php echo $_SERVER['HTTP_REFERER']; ?>" /> <br /> <textarea cols="102" rows="20" name="editordata" id="editordata" class="textfield"><?php echo htmlspecialchars($data); ?></textarea> <input name="name" id="name" type="hidden" value="<?php echo $name; ?>" /> <p class="tip">Remember! You can set paragraphs and headings using the Format menu. </p> <br /> <span class="button file_man"><a href="javascript:open_file_manager()"><span class="icon icon_upload">Insert a File or Image</span></a></span> <br /><br /> <br /><br /> <span class="button"><a href="javascript:save_confirmation()"><span class="icon icon_accept">Save</span></a></span> <span class="button"><a href="javascript:cancel_confirmation()"><span class="icon icon_delete">Cancel</span></a></span> </form> <div class="footer"></div> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/179015-is-this-proper/#findComment-944725 Share on other sites More sharing options...
Psycho Posted October 26, 2009 Share Posted October 26, 2009 There must be some conflict then. You should always work from rendered HTML when trying to debug javascript code (i.e. not the pre-rendered PHP code). There was a lot of JavaScript and other code that I didn't have the time to try and figure out. So, when I removed all the code except for the necessary code to run the insert() function, it worked as I would expect. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>nc-cms | HTML Editor</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> <meta name="robots" content="noindex" /> <meta name="robots" content="nofollow" /> <link rel="stylesheet" type="text/css" media="screen" href="system/css/editor.css"/> <link rel="stylesheet" type="text/css" media="screen" href="system/css/editor_html.css"/> <!--[if lt IE 7]><link rel="stylesheet" type="text/css" media="screen" href="system/css/ie.css"/><![endif]--> <script type="text/javascript"> function insert(){ var newtext = "test"; alert("test"); document.getElementById("editordata").value = "test"; //document.editorform.editordata.value = "test"; } </script> </head> <body> <div id="wrapper"> <div id="editor"> <h1 title="Powered by nc-cms"><?php echo NC_WEBSITE_NAME; ?> </h1> <form name="editorform" id="editorform" method="post" action="index.php?action=save&ref=<?php echo $_SERVER['HTTP_REFERER']; ?>" /> <br /> <textarea cols="102" rows="20" name="editordata" id="editordata" class="textfield">Initial Text</textarea> <br /> <button onclick="insert();">Insert Text</button> </form> </div> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/179015-is-this-proper/#findComment-944827 Share on other sites More sharing options...
mike12255 Posted October 27, 2009 Author Share Posted October 27, 2009 the other javascript code is for the tinymce editor mabey its conflicting with it..hmm but on a second though i dont think that would affect things because i can already preset the value Quote Link to comment https://forums.phpfreaks.com/topic/179015-is-this-proper/#findComment-945103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.