I want to insert some programming code along with the descriptions like a tutorial site.
This is the sample code for HTML code:
$code = "
<p>This is introduction to HTML </p>
[code]
<html>
<head>
<title>This is sample descriptions</title>
</head>
<body>
<form method='post' action='index.php'>
<input type='text' name='username' value=''>
<input type='password' name='password' value=''>
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>
[/code]
<p> This is sample for PHP </p>
[code]
<?php
echo "Hi, This is PHP";
?
[/code]
";
$code = mysql_real_escape_string($code);
mysql_query("INSERT INTO tutorials SET tutorial='$code'");
To display I am retrieving the content from a database and using htmlspecialchars like,
echo htmlspecialchars($code);
For highlighting the codes, I am using google-code-prettify, that requires that the code be in between pre tag with prettyprint class,
<pre class='prettyprint'>
echo htmlspecialchars($code);
</pre>
Where ever the tags, [code] and [/code] are replaced with <pre class='prettyprint'>"; and </pre> like,
$code = str_replace("[code]", "<pre class='prettyprint'>", $code);
$code = str_replace("[/code]", "</pre>", $code);
When I echo,
echo htmlspecialchars($code);
only plain text is displayed like:
<html> <head> <title>This is sample descriptions</title> </head> <body> <form method='post' action='index.php'> <input type='text' name='username' value=''> <input type='password' name='password' value=''> <input type='submit' name='submit' value='Submit'> </form> </body> </html> </pre> <h5> 2. This is sample code for paragraph </h5> <pre class='prettyprint'> <html> <head> <title>This is sample
You're calling htmlspecialchars after doing the replacements so the <pre> tags are escaped as well (and won't be rendered as HTML). Reversing the order should do the trick:
$code = htmlspecialchars($code);
$code = str_replace("[code]", "<pre class='prettyprint'>", $code);
$code = str_replace("[/code]", "</pre>", $code);
echo $code;
Further, have a look at highlight_string for highlighting source code.
There are <pre class='prettyprint'> and </pre> statements in your code too. This may make the code not work as expected.
You may try by changing < and > in your code to < and > respectively.
Related
I got this code and I want to modify it a bit. It is a form that shows the number of words in a text. I want to change the input (
I have tried many things to use a textarea but either there is no output or the original text disappears. Changing the name into id didn't work, same for the 'normal way of using a textarea'.
Is there a way of doing of replacing the input with textarea? One thing that has to stay is that after a submit the text in the textarea needs to be visible.
I appreciate any help.
<?php
error_reporting(0);
$inputtext = $_POST['ipt'];
if ($_POST['clear']) {
$inputtext = "";
$output1 = "";
}
if ($_POST['send']) {
$output1 = "Number of words in this text: " . str_word_count($inputtext);
}
?>
<html>
<head>
<style>
body {
font-family:arial;
font-size:12px
}
.bigtextarea {
width:100%;
height:40%;
min-height:200px
}
textarea {
font-family:monospace;
border-color:#a9a9a9
}
</style>
</head>
<title>Form results on one page after submit</title>
<body>
<form action="" method="POST">
<h1>Form results on one page after submit</h1>
<h3>Copy and Paste text:</h3>
<input class="bigtextarea" wrap="hard" type= textarea name=ipt value="<?php echo $inputtext;?>" height="100px" size="100%">
<input type="submit" name="send" value="Ok" title="Click here to display values.">
<input type="submit" name="clear" value="Clear" title="Click here to clear text boxes.">
</form>
<?php
echo "<br><br>";
echo "<font size='4'>";
echo $output1;
echo "</font>";
?>
</body>
</html>
<input class="bigtextarea" wrap="hard" type= textarea name=ipt
value="<?php echo $inputtext;?>" height="100px" size="100%">
Replace with
<textarea name="ipt" class="bigtextarea" wrap="hard"><?php echo $inputtext ?></textarea>
i'm trying to get a value from a select but i don't get how to do it, here is my code:
<form method="post">
<select name="List">
<?php while($Adresses = mysqli_fetch_array($adress)) { ?>
<option value="<?php echo $Addresses[0];?>"><?php echo $Addresses[0]; ?> </option><?php } ?>
</select>
<input type="submit" name="Edit" value="Edit">
</form>
I need the value of the selected address here:
<?php if(isset($_POST['Edit'])):
$_SESSION['adress']= $_POST['List'];?>
<p><b>Change your adress:</b> <input type="text" name="new_adress" value= "<?php echo $_SESSION['adress']; ?> " /></p>
<input type="submit" name="save" value="Save">
<input type="submit" name="delete" value="Delete">
<?php endif; ?>
and this is the error message:
Undefined index: List
I have tried
<?php $_SESSION['adress']= $_GET['List'];
but it did not work either...
thanks for you help in advance.
PHP is a serverside language.
HTML, CSS, and JavaScript are clientside languages.
PHP is for sending out HTML, CSS, and JavaScript data. To send from HTML to PHP you need to submit your form.
In your html:
<html>
<head>
</head>
<body>
<form action='test.php' method='post'>
<?php
echo "
<input name='name' value='" . $_SESSION['name'] . "'>
<input name='address' value='" . $_SESSION['name'] . "'>
";
?>
<input type='submit' value='Update Information'>
</form>
</body>
</html>
In your 'test.php':
<?php
$_SESSION['name'] = $_POST['name'];
$_SESSION['address'] = $_POST['address'];
?>
This will save it into your session. To actually save it you need to look into SQL or XML data parsing.
The only way to process data from HTML => PHP is with form submitting. PHP is processed by the server, not your internet browser.
Use POST for getting the data.
<?php $_SESSION['adress']= $_POST['List'];
I have this working code yet upon button click, it populate the textbox with the timestamp when the page was reloaded/loaded; What I like is for the button to populate the textbox with the time upon click. Anything that can help me will be greatly appreciated, Cheers!
Code:
<html>
<body>
<?php
echo date("Y/m/d");
echo "<br>";
$time= date("h:i:s A", strtotime("now"-8));
echo "<input type=textfield name=time value='$time' size=9>";
echo "<br>";
echo "<input type=\"textfield\" id=\"textbox\" value='' size=9>";
echo "<input type=\"button\" value=\"Time\" onClick=\"document.getElementById ('textbox').value='$time'\">";
?>
<br>
<input id="date" name="curTime" value="<?php echo date('M d,Y'); ?>" size="9" maxlength="10" Required />
</body>
</html>
If you want to get the actual time when the button is clicked, you will have to do that on the client side with JavaScript.
For example,
<!DOCTYPE html>
<html>
<head>
<title>Button Click</title>
</head>
<body>
<input type="text" id="date" value="" />
<input id="button" type="button" value="Get the Date!" onclick="document.getElementById('date').value = new Date().toLocaleTimeString(navigator.language, {hour: '2-digit', minute:'2-digit'})" />
</body>
</html>
This will populate the textarea with the current date upon click. You can send that back to your server through AJAX, POST, etc.
Update
Since you said you were using IE 8 and it looks like IE 8 doesn't support formatting the toLocaleString() output, you could filter offer the seconds with a regular expression. I would highly recommend that you just not support IE 8 and not do the following unless you really have to.
<!DOCTYPE html>
<html>
<head>
<title>Button Click</title>
<script type="text/javascript">
function filter_seconds(date){
return date.replace(/(\d{1,2}\:\d{1,2})\:\d{2}/, '$1')
}
</script>
</head>
<body>
<input type="text" id="date" value="" />
<input id="button" type="button" value="Get the Date!" onclick="document.getElementById('date').value = filter_seconds(new Date().toLocaleTimeString(navigator.language))" />
</body>
</html>
I have an echo statement that is supposed to run a specific amount of times, i.e 'n' times, right now the function abc() is empty, for testing purposes, what I'm trying to to is this:-
echo "
<form method=\"post\" action=\"<?php abc(); ?>\" >
<input type='text' name='comment' style='width: 80%;height: 70px; margin-left: 60px' /> <br/>
<input type='submit' value='submit comment' style='margin-left:60px' />
</form>
";
but every time I click the button to submit the form I get the error
Forbidden
You don't have permission to access /< on this server.
If what I am trying to do isn't possible, is there an alternative way?
What I want to do is; create a button which, when clicked, calls a php function (which may or may not reload the page, doesn't really matter). There will be multiple functions created via a loop and for each loop iteration the values passed to the function will be different. By values, I don't mean variable types, I mean variable values will be different. I know there aren't any variables passed to the abc function at the moment, but like I said, abc function is for testing only to try to get past the forbidden error.
What I am actually trying to do is this..
$doubleinverted='"';
echo "
<form action=".$doubleinverted."<?php f_comment(".$row['ScrapId'].",'".$row1['Email']."');?>".$doubleinverted." method='post'>
<input type='text' name='comment' style='width: 80%;height: 70px; margin-left: 60px' /><br/>
<input type='submit' value='submit comment' style='margin-left:60px' />
</form>
";
I know you can add inverted commas like \", but I just found that out.
Also, this echo statement will be in a loop and for each iteration, the values passed to the function will be different
You cannot use PHP blocks inside of echo statements.
If f_comment echoes out a string, you should be doing something along the lines of:
echo "blah blah blah";
f_comment(...);
echo "more blah blah";
If it returns a value, store it in a variable or concatenate the string:
$string = "blah blah blah";
$string .= f_comment(...);
$string .= "more blah blah";
echo $string;
echo "
<form method=\"post\" action=\"<?php abc(); ?>\" >
<input type='text' name='comment' style='width: 80%;height: 70px; margin-left: 60px' /> <br/>
<input type='submit' value='submit comment' style='margin-left:60px' />
</form>
";
The action of the form is <?php abc(); ?> while you are already in PHP mode. I'm afraid I can't let you do that Dave!
Change the form to <form method=\"post\" action=\"' . abc() . '\" >
While you are at it, get rid of the confusing quote escaping. This reads more clearly...
<?php
echo '<form method="post" action="' . abc() . '">
<input type="text" name="comment" style="width: 80%;height: 70px; margin-left: 60px" /> <br/>
<input type="submit" value="submit comment" style="margin-left:60px" />
</form>';
?>
I'm trying to learn PHP using NetBeans although I've come up against a problem with the interpreter and I can't tell how to fix it.
It's to do with the notation <<<_END. It should, from what I'm learning wrap everything into a variable until it's ended with _END
However, if I plug in the following example:
<?php
echo <<<_END
<html><head><title>PHP form upload</title></head><body><form method='post' action='upload.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload'/>
</form>
_END
if ($_FILES)
{
$name = $_FILES ['filename']['name'];
move_uploaded_file($_FILES ['filename'][tmp_none], $name);
echo "Uploaded image '$name' <br/> <img src='$name'/>";
}
echo "</body></html>";
?>
I get the following error message
Parse error: syntax error, unexpected T_SL in script.php on line 13, where line 13 is the code that says 'echo <<<_END'.
Can anyone help me, please?
There must be no space/tab/indentation before ending _END like this:
echo <<<_END
<html><head><title>PHP form upload</title></head><body><form method='post' action='upload.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload'/>
</form>
_END;
Don't forget that it is not allowed to
indent the closing tag if you do so
you will get a parsing error.
http://www.phpf1.com/tutorial/php-heredoc-syntax.html
Missing semicolon after _END
You can't have any indentation before _END