Help me solve the problem

Hello there Guys, I’ve made a website :
So the website works with php and html . The php does not actually stores data in database , it just gives the input result . So , i want to clear the form inputs after submission by pressing clear button . But , the problem is that it doesn’t clear the input after submission . How do i resolve this problem ? I don’t wanna use javascript here .

I’m afraid your page is as clear a mud. What exactly are you trying to do here?

Uhh , do i need to explain the whole working ???

Just think of submitting a form(here by pressing divulge) but after submission you want to clear the input by pressing clear , so in this case it doesnt … thats my problem.

Note that the “reset” button will reset the form to its default values, not clear all entries in a form. If you drew the form from your PHP with values in any fields, those are the values that will be drawn when you press “clear”.

If I fill in the form on the left and submit it, then copy the text from the bottom box into the right hand form and press “divulge”, that draws the page again but with my message as the default value in the right hand box. Then, if I press “clear”, that message is still there because that’s the default value for the form field.

It didn’t hide the private message for me, or if it did, it didn’t reveal it.

Right, so lets… get a little more specific, which i think is what Gandalf is alluding to.

Clear which inputs? you’ve got 2 forms on the page, but only 1 clear button. Even ignoring the problem droopsnoot outlined above, a reset button will by default only operate on the form that it is part of.

Why would it clear the input after submission? What does your PHP code look like for these text boxes?

Use the right tool for the job. Whacking on a screw with a hammer might get it done, but boy would you find a screwdriver faster and more efficient.

1 Like

The concept is not that hard but you need some way to save data across different form submissions so I suggest saving the messages to session.

To make it easy to clear the form I would have 1 primary array KEY called ‘message’ and 2 secondary KEYs ‘public’ and ‘private’ to save your messages to.

Define session start and your message array with a condition checking for the primary KEY like shown below. Note: I also added a ‘reset’ condition, which will unset() the message array.


//Reset Message
if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['reset'])):

	$_SESSION['message']['public'] = '';
	$_SESSION['message']['private'] = '';

Now its a matter of setting those POST values to session. Again we look for the POST condition and the specific post KEY of ‘public’ to contain this section of code. Note I added unset() to the post values here to clear any residual post values.

if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['public'])):	
	$_SESSION['message']['public'] = $_POST['public'];
	$_SESSION['message']['private'] = $_POST['private'];

Now as I understand it, the “Reveal” form on the right is a textarea named “encoded” where a user would enter a quote “Public Message” and it is magically going to reveal the hidden “Private Message” in the textarea below called “Message Hidden” when they click the “Divulge”. Oh how secret.

SO we are looking for the POST key ‘encoded’ as the condition and compare that to the “Public” message for a match. If a match is found we can set that private message to a variable and if not found we set the variable as empty. I called it $showprivate and defined the condition like so.

$showprivate = ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['encoded']) && $_POST['encoded'] == $_SESSION['message']['public'] ? $_SESSION['message']['private'] : '');

As submitting the form would normally remove the value from the right textarea I defined $showpublic with the same condition. Also I was intrigued my your Checkmark so I added the condition to show it IF there is a “Private Message”. So those all 3 of those variables would look like this.

$showpublic = ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['encoded']) && $_POST['encoded'] == $_SESSION['message']['public'] ? $_SESSION['message']['public'] : '');
$showprivate = ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['encoded']) && $_POST['encoded'] == $_SESSION['message']['public'] ? $_SESSION['message']['private'] : '');
$checkmark = (!empty($_SESSION['message']['private']) ? " ✓" : '');

Some small edits to the “View Source” html code to echo variables looks like this.

	<div style="display: grid; grid-auto-rows: 1fr; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); grid-gap: 2em;">
		<form action="?" method="post">
				<div class="group">
					<label for="public">Public message</label>
					<textarea name="public"><?php echo $_SESSION['message']['public'];?></textarea>
				<div class="group">
					<label for="private">Private message</label>
					<textarea name="private"></textarea>
				<p><button type="submit"><i class="fas fa-pencil-alt"></i>Conceal</button></p>
		<form action="?" method="post">
				<div class="group">
					<label for="encoded">Public message</label>
					<textarea name="encoded" style="height: 12.0em;"><?php echo $showpublic;?></textarea>
				<p><button type="submit" name="Divulge"><i class="fas fa-eye"></i>Divulge</button>
				<input type="submit" name="reset" value="reset"></p>

<section class="notice">
	<h2>Message Hidden<?php echo $checkmark;?></h2> 
	<textarea name="showprivate" style="height: 5em;"><?php echo $showprivate;?></textarea>
	<p>Copy the text above, and your private message will come along for the ride.</p></div>


Gives me a final result of this, showing the private message when the correct message is entered into the form…

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.