Textarea not filled with all $_GET data, cuts off at quotation marks - php

I have a text area page which is launched from the previous page:
$message = $_POST['editPost'];
header("location:editPost.php?msg=$message");
The editPost.php retrieves this and fills the text area like so:
echo "<form action='index.php' method='post'>
Your Post:<br/>
<textarea name='comments' cols='100' rows='100'>".$_GET["msg"]."</textarea>
<br/>
<input type=submit value='submit'>
</FORM>";
The problem I'm getting is not all the data of 'msg' seems to get passed across, or the message gets cut of at the point where it reaches a quotation mark e.g. '
The text I want it to fill the textarea with this text:
So this is my second blog post for this assignment, I've progressed a bit since my previous post
however it only add this to the text area this:
So this is my second blog post for this assignment, I
As I say it seems to not contain any more of the string after the quotation mark is reached. Is there anyway around this so I can pass the whole message across?
EDIT
I might add, I'm aware a more simple solution would be to retrieve the message again from the MySQL database as that's what I'm using, but I'm just intrigued as to how this works.
if(isset($_POST['edit'])) {
//$_SESSION['postToEdit'] = $_POST['editPost'];
$message = htmlentities($_POST['editPost'], ENT_QUOTES);
header("location:editPost.php?msg=$message");
That is what I do with the data once it is posted from this form:
foreach($posts as $postid=>$post)
{
echo '<div class="blogPosts">'.$post;
if(isset($_SESSION['username'])) {
if($_SESSION['isAdmin'] == "true") {
echo "<br/><br/><form name='adminTools' action='index.php' method='post'>
<input type='hidden' name='editPost' value='$post'>
<input type='submit' value='Edit' name='edit'/>
<input type='hidden' name='deletePost' value='$postid'>
<input type='submit' value='Delete' name='delete' /></form>
</div>";

echo "<form action='index.php' method='post'>
Your Post:<br/>
<textarea name='comments' cols='100' rows='100'>".htmlspecialchars($_GET["msg"])."</textarea>
<br/>
<input type=submit value='submit'>
</FORM>";
textarea seems an odd container for it, but that's your call

I think I get it.
header("location:editPost.php?msg=$message"); doesn't smells good.
If you simply want to fix the problem use http://php.net/manual/en/function.urlencode.php and when getting the value http://www.php.net/manual/en/function.urldecode.php
You're getting a post request and then forwarding it using header location. That's definitely a poor way to do it. The problem probably is taking place when you pass the parameter as a query string on ?msg=$message
Since MVC and alike applications (n-layered apps) is the standard now. You should not do that spaghetti code, mixing html and php and using header as your FrontController dispatcher.
I'd suggest you to use for example, the SLIM framework. If you do not want to learn this. You should at least follow the standards described here.
E.g.:
<?php
// index.php
// load and initialize any global libraries
require_once 'model.php';
require_once 'controllers.php';
// route the request internally
$uri = $_SERVER['REQUEST_URI'];
if ($uri == '/index.php') {
list_action();
} elseif ($uri == '/index.php/show' && isset($_GET['id'])) {
show_action($_GET['id']);
} else {
header('Status: 404 Not Found');
echo '<html><body><h1>Page Not Found</h1></body></html>';
}
In short, create a FrontController file that will map to the desired actions (controllers) according to the request without using header() to execute specific actions (the exception is only if actually the headers are needed, in fact).

Use addslashes($message) before passing it in querystring and stripslashe($_GET['msg']) function before displaying it.
Click addslashes() and stripslashes() for more references.

Related

how do i clear the screen of a calculator in php

i'm trying to find a way to erase the text screen from my calculator when you press the +/- button so it acts more like a real calculator.
i'm pretty new to php (a week or two) so i'm still learning but i cant figure this out.
the php file returns html code where in i have a <input type='text' named result value='$value'>
(calculator screen).
and a series of submit buttons with a name and value of 1,2,3,+,...
when i press a button a char gets added to the string in result.
when i press on the '=' button it reads the string with eval();
and calculates it.
iv'e tried with a function but i cant figure it out some ideas or tips would be very much welcome.
$value = '';
if(isset($_GET['result']))
$value.=$_GET['result'];
if(isset($_GET['1']))
$value .='1';
if(isset($_GET['2']))
$value .='2';
//and so on ...
if(isset($_GET['+']))
$value .='+';
if(isset($_GET['-']))
$value .='-';
if(isset($_GET['='])){
eval('$result = '.$value.';');
$value = $result;}
return "<form action='index.php' method='get'>
<br><input type='text' name='result' value='$value'><br>
<input type='submit' name='1' value='1'>
<input type='submit' name='2' value='2'>
//and so on ...
(the rest of the html code is loaded somewhere else)
This PHP code below will execute JavaScript code to clean the input field,
echo"<script>document.getElementsByName('result')[0].value = '';</script>
This should resolve your question but i have to tell you that PHP isn't suitable choice to make a web calculator, You need to use JavaScript to make a real calculator without refreshing the page.
Please copy this code below to an HTML file and try it so you can get an idea how it works.
First Number
<input type="text" id="firstNumber" value="6" /><br>
Second Number
<input type="text" id="secondNumber" value="5" /><br>
Result
<input type="text" id="result" /><br>
<button type="button" onclick="Calculate();">Calculate</button>
<script>
function Calculate(){
let firstNumber;
let secondNumber;
firstNumber = document.getElementById("firstNumber").value;
secondNumber = document.getElementById("secondNumber").value;
document.getElementById("result").value = Number(firstNumber) + Number(secondNumber);
// "Number" is to tell javascript its a number not a string/text,
// without "Number" it will concat both numbers
}
</script>
You should use brackets for your conditions, especially if you nest them:
if(isset($_GET['result'])) {
$value .= $_GET['result'];
}
And your indentation makes it look like Python, someone reading it would mistake it for a condition nested in another condition and believe that your if(isset($_GET['2'])) is true only if if(isset($_GET['1'])) is also true.
A switch is a better solution here (and even better for the operators since you probably only have four of them), but in any case, you should assign each term of your operation in a single variable.

Is there a way to send only relevant data when a form has multiple similar fields?

I'm trying to recreate a "commentary section" on my project. I have a loop to display many "posts" and each post has it's own comment section, all inside the same Form. because of that I end up having multiple fields with similar names (i.e button1, button2, button3, etc etc etc, one for each post). Is it possible to have one isset($post['button']) that fits all and only brings the info related to the one I clicked?
I could have each post be a separate Form but then I would have do re-do a good portion of what I did so far. I was hoping there's a way around it
that's what I have:
input type='text' name='post_comment".$k."' class='form-control' value=''
input type='submit' class='btn' name='postComment".$k."' value='Comment'
...
if(isset($post['postComment'])){
//do something
}
If I hardcode $k into my isset (i.e isset($post['postComment34'])) everything works as I wanted. Is it possible to have my isset accept any 'postCommentxx'?
You really need to use an array with matching indexes:
<input type='text' name='post_comment[$k]' . . .
<input type='submit' name='postComment[$k]' . . .
Then you can use the index of the submit to get the text:
if(isset($post['postComment'])){
$key = key($_POST['postComment']);
$text = $_POST['post_comment'][$key];
}
I would change postComment to something like submit as it is confusing to have variables of the same name just slightly different.
Welcome to Stackoverflow!
So, if I understand your question right, you want to just use 1 form and not using multiple forms for sending comments on posts. Try using <button type='submit'> instead of <input type='submit>'. Look at my example:
<input type='text' name='post_comment".$k."' class='form-control' value=''>
<button type='submit' class='btn' name='postComment' value='".$k."'>Comment</button>
Now you can go on with your php code;
if (isset($_POST['postComment'])) {
$postid = $_POST['postComment'];
$comment = $_POST['post_comment'.$postid];
// Do something
}
That's it! (I hope I helped you :P)

Input from PHP generated form is not being recorded

The issue I have been experiencing deals with input from HTML which was generated using PHP echo statements. Here is the function I have that outputs the form:
function confirm_recipients()
{
echo "<form action = ' ' name ='email_options' method='post'>";
echo "<input type='submit' name='sendRecipients' value='Yes, I want to send the emails' >";
echo "</form>";
}
Later on in the same PHP page, I call the function and then check to see if the submit button was set.
confirm_recipients();
if (isset($_POST['sendRecipients']))
{
//perform actions
}
However, this code is not functional seeing as even when the submit button is set (clicked by the user), the if statement block is never executed. Perhaps there is an issue with posting from the same file I want to "read in" from? Thanks for any advice.
Updates
Thank you for such immediate response. Sadly none of the suggestions have worked (removing the space in the action value or the suggestion made by user623952). No errors have been reported, the button is just failing to be set. I am looking for other places in the file that might have errors, perhaps in the order I call the function.
This works fine for me:
<?php
print "<pre>".print_r($_POST,true)."</pre>";
confirm_recipients();
function confirm_recipients() {
echo "<form action = ' ' name ='email_options' method='post'>";
echo "<input type='submit' name='sendRecipients' value='Yes, I want to send the emails' >";
echo "</form>";
}
if (isset($_POST['sendRecipients']))
{
print "<br/>sendRecipients is set!<br/>";
}
?>
I think your problem might be somewhere else in the code.
It's okay to POST the form data to the same script that contains the form. Change the action attribute to the URL of the script, do not set it to whitespace, which is what you did.
I don't think the value of a submit input is sent as part of the POST. Try using an input type="hidden" with the name 'sendRecipients'.

Changing content using $_POST

im trying to change the value of a field in $_POST so that it will load the correct content for whatever has been posted.
When a user goes onto my website $_POST is initially set as
$_POST['order_page_content'] = 'list'
I then have a form in my view.content.php file that looks like this:
<td><form action='http://localhost/magento_soap_client/fulfilment' method='POST'>
<input type='hidden' name='order_page_content' value='info'/>
<input type='hidden' name='order_id' value='$order_info->order_id'/>
<input type='submit' value='View'/>
</form></td></tr>";
So the value of $_POST['order_page_content'] should change to info when the button is clicked. Therefore the content of the page should change...
Once the button is clicked, i can see via firebug that the value has changed in the post, its just my page isn't getting set to it
So how do i make the content change based on the post value that got submitted? Or what is going wrong?
EDIT
Ok guys ive tried what you said, but its still not happening, i dont know if i have put it in the wrong place or what... i think the easiest way is to show you my code.
The script to decide which content it is is here:
link
and the script that calls it is here:
public function get_html() {
$m_html = null;
if($_POST['order_page_content'] == 'list'){
$m_obj_html = new content();
$m_html = $m_obj_html->get_page_content('list');
}
else if($_POST['order_page_content'] == 'info'){
$m_obj_html = new content();
$m_html = $m_obj_html->get_page_content('info');
}
return $m_html;
}
The post['order_page_content'] is getting set in the bootstrap initially to list
Thanks
<input type='hidden' name='order_id' value='<?php echo $order_info->order_id; ?>'/>
if it's already in php use
<input type='hidden' name='order_id' value='". $order_info->order_id ."'/>
$_POST is set globally from PHP.
If you have something in your script like:
$_POST['order_page_content'] = 'list';
So your variable is on each page load overwritten.
Try to check for it before you set it.
if (!array_key_exists('order_page_content', $_POST)
{
$_POST['order_page_content'] = 'list';
}
$_POST is set with post data sent from the client when they access the site. You can access it if they send any. Your form looks like it should work. What I do see though is that you make them send this against a page on the localhost. Is localhost running an apache or similar server with PHP? If it is, then on that page add this:
if(!empty($_POST['order_id'])){
print_r($_POST);
}
If that form is on the page you send them to, and you want the value to be remembered (still be there after they send in the form/post data, then do this:
<input type='hidden' name='order_id' value='<?php if(!empty($_POST['order_id'])) echo $_POST['order_id']; ?>' />
Tom You can check it with the use of if condition
if($_POST['order_page_content']=="info"){some code} else { some code}
Seems like the problem was this line:
<form action='http://localhost/magento_soap_client/fulfilment' method='POST'>
The url in action was resetting everything, so i have now taken it out and left action blank and works fine
Thanks for your help though guys

Approved method to navigate between pages on same website

I have researched many places to find an answer to this question, but they never quite answer my real question: What is the best/approved way to move to a new page within the same website? I have read that it is bad to use window.location because search engines will think you are hiding something. But, when I don't want to open a new window (window.open), then I don't know how else to do it. I use href anchors in links and form actions, where appropriate. But when I have menus or buttons with onclick, then I need something else.
Here's an snippet of my code:
my javascript: (with one option commented)
function gotoCat() {
var catcdF = document.catSelect.catcd.value;
<?php
echo "window.location.href='http://www.mysite.org".$pgmdir."services/busMenu.php?catF='+catcdF; ";
/*
echo "window.open('http://www.mysite.org".$pgmdir."services/busMenu.php?catF='+catcdF,'','resizable=1,scrollbars=1,toolbar=1,top=50,left=300,width=950,height=800,location=0'); ";
*/
?>
}
My dynamic SELECT list in a form (within PHP):
echo " <select name='catcd' id='catcd' size='8' onclick=gotoCat() > \n";
// display list of categories
if ($numcats == 0) { // print message text only
echo "<option value='0' >".$catMsg."</option> \n";
}
else {
for ($i=1; $i<=$numcats; $i++) {
$catcd_db = $catAry[$i][1];
$catName_db = $catAry[$i][2];
echo "<option value='".$catcd_db."'> ".$catName_db." </option> \n";
}
}
echo "</select>";
So, as you can see, I just want a method to allow the user a choice and then automatically go to the correct web page once selected. This is not always in a select list. Often it's when they want to exit or get an error:
if (mysqli_connect_errno()) {
echo "<br/> <p style='text-align:center;'> <button type='button'
class='buttonStyle' style='padding: 4px 20px;' value='Exit' ";
echo "onClick=\"window.location.href='http://www.mysite.org/services/catSelbus.php?rc=1&func=Rev'\" > ";
echo "Exit </button></p> ";
}
I cannot use "go back" because they need to go to a prior page, not the form they came from.
So, unless my navigation methods are really off-the-mark, I guess I need to know the acceptable method for using javascript onClick to move to the next page in the same website. Is window.location okay, or should I use something else?
Any opinions or suggestions are welcome!
To navigate to another page using Javascript, use:
window.location.href = "url";
That's how it's done and there's nothing wrong about it.
For the sake of argument, you could create a hidden link and simulate a click on it, but as I said, there's really no need.
You can use php header('location') instead:
<form action="submit.php">
<input type="hidden" value="test" name="hidden1" />
<input type="submit" Value="Exit" ... />
submit.php
<?php
if (isset($_POST['hidden1'])
{
header('Location: http://www.mysite.org/services/catSelbus.php?rc=1&func=Rev');
exit;
}
?>
More info about header('Location ...');:
http://php.net/manual/en/function.header.php
Instead of a hidden, you use your select's value and get it via the $_POST variable.

Categories