setting textarea to empty yet it reloads variable data on submission - php

If I set my text area in my edit page to blank and I hit submit, it reloads the page with an error as its supposed to but it also loads the summary text from my php variable. instead it should stay blank.
Here's what I've tried:
<textarea name="summarytxt" cols="52" rows="7">
<?php
if ( isset($_POST['updatebtn']) AND (!empty($POST['summarytxt'])) ){
echo #$POST['summarytxt'];
}
elseif ( isset($_POST['updatebtn']) AND (isset($POST['summarytxt'])) ){
echo #$POST['summarytxt'];
}
else{
$summary=stripslashes($data['summary']);
echo $summary;
}
?>
</textarea>
I was hoping the elseif condition will fix it but it doesn't seem to work:
elseif ( isset($_POST['updatebtn']) AND (isset($POST['summarytxt'])) )
The above to me meant if the button is clicked and the text area is set echo whatever is in the text area.
Any suggestions on what I'm doing wrong or how I can fix it?
Edit:
I basically want to know the following:
if say i have field summarytxt and an array called data['summary'] i need:
1. on page load, load from data['summary']
2. if the user clears the summarytxt, on page submit, it should be blank inside summarytxt
i have tried the following which seems to work...
<?php
if (!isset($_POST['updatebtn'])){
$summary=stripslashes($data['summary']);
echo $summary;
}else{
echo $_POST['summarytxt'];
}
?>
Now i'm having some trouble with my drop down box:
1. when i change it from a product to 'select' which is what you get in the add form, and i update the page. instead of sticking to select its loading the product it had when i opened the form.
Can somebody please show me a simple way of this? thanks!

So I would do this similar to (remember about php and html separation):
<?php
$summary = "";
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$summary = stripslashes($_POST['summarytxt']);
}
?>
<textarea name="summarytxt" cols="52" rows="7"><?php echo $summary; ?></textarea>

Related

How do you validate a html unicode in php

I am passing values from HTML form to a php file for processing
$to_do = $_POST['action'];
Then i can say
<?php
if( $to_do == "delete") {
echo "i will delete for you";
}
?>
Difficulty i am having is when the HTML value is unicode &#10008 for a delete symbol.
instead of the value "delete".
In php i cannot tell how to test it.
<?php
if( $to_do == "&#10008") {
echo "i will delete for you";
}
?>
is not working.
Any one to help me out?
How are you sending this value to begin with - via something like <input type="submit" value="✘">?
In that case, I would recommend you switch to a button element - that can have a separate submission value and display text. So you can keep sending delete, and show ✘ to the user at the same time.
<button type="submit" value="delete">✘</button>
More details on the button element can be found in the MDN, https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

post isset($_POST['next'] is not set after submit /refresh page. Need to be press twice for it to be set how to fix?

<script>
function refreshPage()
{
document.forms[0].submit();
}
</script>
<?php
foreach($values as $value)
{
?>
<input type = "checkbox" name = "food[]" value = "<?php echo($value['dinner']);?>"
<?php if(isset($_POST['food'])) echo "checked='checked'"; ?> > // note this is still part of
input
<?php
}
?>
<button name = "pass" onClick = "refreshPage()">refresh</button>
<?php
if(isset($_POST['pass'])
{
// do a lot more stuff but I have this for temp info
echo("hello");
// I am printing all the check box values in here I do not
// have the code for it yet but I think ik how to do it
// but since i do not have code for it now it is just empty
}
?>
hi so everytime I click on the button refresh. the isset($_POST['pass']) does not work. I have to click on it a second for it to be set which would then print the hello and table of check items part.
I want it so that if you click on it once it will print the hello. Not twice. How do i fix my code? FYI I know you could do isset($_POST['food']) for it to work. But that will break other parts of my code.
I need the button to set the isset($_POST['pass']) to be True (after one click) after I press the refresh button one time.

Five unique pages lead to one page. Can I change the <h1> according to the page they came from?

I have five unique forms each on a page of HTML. They then go to a PHP file to send the e-mail of data. Next they go to the HTML thank you page. I am hoping to change the heading according to which form they just submitted.
For example, if they submit a review, the should read "Thank you for your review" etc.
Technically all of these are saved as php files but only the e-mail page has php items.
Like <?php echo("<p>". $mail->getMessage()."</p>"); ?>
You should redirect to another php file and pass a parameter on url. Example:
sendemail.php
<?php
/** After send the email, check what kind form is (I don't know how do you check this).
This example is just to show you: */
if ($formType == 'review') {
$type = 'review';
} else if ($formType == 'anothertype') {
$type = 'anothertype';
}
header('Location: /thankspage.php?type=' . $type);
?>
thankspage.php
<?php
$type = $_GET['type'];
if ($type == 'review') {
echo '<h1>Thanks for your review</h1>';
} else if($type == 'anothertype') {
echo '<h1>Thanks for your anothertype</h1>';
}
?>
One way put a hidden field in your forms that'll get passed with the other form data. Then put an if statement on the thank you page and echo the appropriate message.
However, that'll only work either if you change the thank you page to php or change the page that receives and processes the form data to echo the thank you message as well

Moving to a part of webpage after page refresh

I have a list of comments for a given article and I have a form underneath the comments for a user to add their own comments.
I'm using php to do some form validation.
This is the process:
User fills out form (or not) and hits submit button. page refreshes.
PHP validates user input and either submits comments if no errors or
generates a list of errors.
If errors exist display the errors.
The problem is that I want the errors to display underneath the comments before the form which it does but when th epage refreshes, the top of the page is displayed and i need it to go straight to the errors and form (much like a page anchor)
Is this possible?
This is called after the submit button is clicked
if(empty($errors)){
$result = post_comment('event',$event_id, $sendername, $senderemail, $userurl, $comment);
if ($result == 'Correct') {
//header('Location: /'.$_SERVER['REQUEST_URI']);
header('Location: '.$_SERVER['REQUEST_URI']);
}
else {
$send_error = $result;
and this is near the comments and form where i want to page to go to if errors exist
// If there was an error sending the email, display the error message
if (isset($send_error)) {
echo "<a name=\"commentsform\"></a>";
echo "There was an error: ".$send_error;
}
/**
* If there are errors and the number of errors is greater than zero,
* display a warning message to the user with a list of errors
*/
if ( isset($errors) && count($errors) > 0 ) {
echo ( "<h2 class='errorhead'>There has been an error:</h2><p><span class='bold'>You forgot to enter the following field(s)</span></p>" );
echo ( "<ul id='validation'>\n" );
foreach ( $errors as $error ) {
echo ( "<li>".$error."</li>\n" );
}
echo ( "</ul>\n" );
}
}
}
Give the form an ID which can be jumped to via the URL:
<div id="submitComment">
<!-- Comment form here -->
</div>
And then redirect the user back to the same URL with the appropriate hash tag:
header('Location: http://www.example.com#submitComment');
Find your form tag, it will look something like this
<form action='yourpage.php'>
Put a hash tag after the URL along with the anchor it will go to upon submission-
<form action='yourpage.php#commentsform'>
Using page anchors you can jump the user to any part of the page by changing the hash in the url.
Make the form send the user to to anchor like so:
<form action='yourpage.php#comments'>
And make an anchor where you want your user to end up:
<a name="comments"></a>

use php to change a html elements inner text

I have a basic form, which i need to put some validation into, I have a span area and I want on pressing of the submit button, for a predefined message to show in that box if a field is empty.
Something like
if ($mytextfield = null) {
//My custom error text to appear in the spcificed #logggingerror field
}
I know i can do this with jquery (document.getElementbyId('#errorlogging').innerHTML = "Text Here"), but how can I do this with PHP?
Bit of a new thing for me with php, any help greatly appreciated :)
Thanks
You could do it it a couple of ways. You can create a $error variable. Make it so that the $error is always created (even if everything checks out OK) but it needs to be empty if there is no error, or else the value must be the error.
Do it like this:
<?php
if(isset($_POST['submit'])){
if(empty($_POST['somevar'])){
$error = "Somevar was empty!";
}
}
?>
<h2>FORM</h2>
<form method="post">
<input type="text" name="somevar" />
<?php
if(isset($error) && !empty($error)){
?>
<span class="error"><?= $error; ?></span>
<?php
}
?>
</form>
If you want change it dynamically in client-side, there is no way but ajax. PHP works at server-side and you have to use post/get requests.
Form fields sent to php in a $_REQUEST, $_GET or $_POST variables...
For validate the field param you may write like this:
if(strlen($_REQUEST['username']) < 6){
echo 'false';
}
else{
echo 'true';
}
You can't do anything client-side with PHP. You need Javascript for that. If you really need PHP (for instance to do a check to the database or something), you can use Javascript to do an Ajax call, and put the return value inside a div on the page.

Categories