I'm running this locally, the page that features this code has this address:
search.php?page=content
<?php if (isset($_GET['page'])) {
$id=$_GET['page']; if ($id=='content') echo "
<div id='content'>
<form action='search.php?page=generated' method='POST' name='value'>
<input type='text'>
<input type='submit' value='Send'>
</form>
</div>";} else echo "";
?>
I want the text inputted in this form to be sent to
search.php?page=generated
I thought the right way to do this was:
#$temp=$_POST['value'];
echo $temp;
But nothing ever gets sent to 'value'. What am I doing wrong?
Related
I am able to create a textarea box that will accept text and store that text to the $_POST super global, but I can't get the text to "come back" to the box once I submit it. (The form is self submitting). If I run a simple echo on the submitted data, however, it displays fine (as shown toward the end of the script below.
<!DOCTYPE html> <body> <?php require("Connection_to_WS.php");
echo ("<form action='Edit_Thread_Description.php' method='post'>");
IF (ISSET($_POST['revised_thread_descr'])) {
$revised_thread_descr=($_POST['revised_thread_descr']);
ECHO "Edit the Revised_Thread_Description here: <br> <textarea name='revised_thread_descr' rows='5' cols='50' value= $_POST[revised_thread_descr]"; // Fails to return any text on Submit.
?><p></textarea></p><br><?php
}
ELSE {$revised_thread_descr= '[some default]';
ECHO "Edit the Revised_Thread_Description here: <br> <textarea name= 'revised_thread_descr' rows='5' cols='50' value= $revised_thread_descr";
?><p></textarea></p><br><?php
}
ECHO '<br>';
echo $_POST['revised_thread_descr']; // Succeeds in returning POST text from the textarea box upon Submit (but outside of the textarea box).
ECHO '<br>';
echo "Click 'Submit': <input type='Submit' name='submit' value='Submit'/>";
echo '<br>';
mysqli_close($connection);
?>
</body> </html>
Doing the same sort of thing was a breeze using "<input type", but I've sunk hours into getting <textarea to cooperate. I I'd be grateful for any assistance.
As Ann Sophie said, there is no "value" property on the textarea element
(https://www.w3schools.com/tags/tag_textarea.asp)
if you want to dynamically append content to it, you can use :
<?php if (isset($_POST['revised_thread_descr'])): ?>
<textarea><?= $_POST['revised_thread_descr'] ?></textarea>
<?php else: ?>
//
Note that you have to echo it, in my example I used alternative syntax,
(http://php.net/manual/fr/control-structures.alternative-syntax.php)
which I think is much more cleaner when you works with PHP + HTML
<?= XXX ?> is short for <?php echo XXX; ?>
I botched a response via 'comment', above. This continues that comment:
This code works, but won't repopulate the box with remarks submitted back to the script via the POST super global.
IF(ISSET($_POST['revised_thread_descr'])):
$revised_thread_descr=($_POST['revised_thread_descr']); ?>
<p> Revised_thread_descr - Edit here:</p><textarea
name='revised_thread_descr' rows='5' cols='50'
<p></textarea></p><br>
<?php
ELSE:
$revised_thread_descr= '[some default]'; ?>
<p> Revised_thread_descr - Edit here:</p><textarea name= 'revised_thread_descr' rows='5' cols='50'
<p></textarea></p><br>
<?php
ENDIF;
echo "Click 'Submit': <input type='Submit' name='submit' value='Submit'/>";
echo '<br>';
mysqli_close($connection);
?>
</body>
</html>
This code, with just a slightly different placement of the <p> tags, gobbles up and displays all html material that comes after the closing </textarea> tag.
IF(ISSET($_POST['revised_thread_descr'])):
$revised_thread_descr=($_POST['revised_thread_descr']); ?>
<p> Revised_thread_descr - EDIT HERE:</p><p><textarea name= 'revised_thread_descr' rows='5' cols='50'
</textarea></p><br>
<?php
As in the screenshot of the browser rendering, below.
Thanks, btw, for getting me to try that alternate syntax! Less confusing.
Bless you! I'd given up and was going for a workaround. I put in that tag caret as you suggested and it all worked. Here is the gist of it, with everything working, and the textarea box populating correctly. Thank you so much for your patience and persistence. Tell me it gets easier... .
<!DOCTYPE html> <body>
<?php
echo ("<form action='Textarea_Example.php' method='post'>");
// The first IF only executes after the script has run once and created a POST value. On the second run, the first IF executes and successfully populates the textarea box with the latest POSTed value
IF (ISSET($_POST['revised_thread_descr'])): ?>
<p>Edit current thread description:<p>
<textarea name= 'revised_thread_descr' rows='5' cols='50'>
<?php echo $_POST['revised_thread_descr'] ?>
</textarea>
<?php ELSE:
$revised_thread_descr = 'some default'; ?>
<p>Edit current thread description:<p>
<p><textarea name= 'revised_thread_descr' rows='5' cols='50'>
The textarea box opens with this in it, but only on the first run. Then it successfully switches to the value typed to the textarea box and saved to POST
</textarea>
<?php ENDIF; ?>
</p>
<?php
// here's the submit button
echo "Click 'Submit': <input type='Submit' name='submit' value='Submit'/>";
?>
</body> </html>
So i got this code, at the moment it is repeating everything , and i just wanted it to repeat the echo, so i get all usernames from it, if i leave it as it is it will also repeat the form when i press a username. Every time i tried to ajust it, it just gave me syntax errors
<?php do { ?>
<?php
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br><br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
}
?>
<?php } while ($row_mensagens = mysql_fetch_assoc($mensagens)); ?>
that do { } while() will always repeat as many as the number of records come from database.
You can do it this way:
<?php
if(isset($_GET['user']) && !empty($_GET['user'])){
?>
<form>
<input type="hidden" name="user" value="<?php echo $_GET['user']; ?>" /> <!-- hidden field so you can process to who -->
Introduz mensagem : <br>
<textarea name='message' rows='7' cols='60'></textarea>
<br>
<br>
<input type='submit' value="Send Message" />
</form>
<?php
} else {
do {
$username = $row_mensagens['username'];
$user = $row_mensagens['id'];
echo "<p><a href='mensagens.php?user=$user'>$username</a></p>";
} while ($row_mensagens = mysql_fetch_assoc($mensagens));
}
?>
Move do { inside else and show the form only if you have a $_GET['user']
I have also added for you a hidden field, so you know who to send message.
Hope you understand how this works. Documentation on Control Structures: do-while
I also suggest to make that form a post form, as by default it is a get form, and since you have a textarea you are more likely to bump into errors if the message is too long.
LE: Another suggestion, try to move to PDO or mysqli_* functions since mysql_* functions are considered deprecated as of PHP 5.5 and have some good chances to be removed.
I have built a inquiry form on my website, the idea is instead of mailing me each time a user submits a query it is added to my database which I can then go and view via my backend system
Each query will be listed one by one with a text-area contact form below it allowing me to reply to each query individually
So far I have this (sorry it's a bit messy)
foreach ($listings as $row){
$loop.= "<h3 class='text-center'>".$row['question']."</h3>";
$loop.= "<p>".$row['message']."</p>";
$loop.= "Name: <b>".$row['name']."</b>";
$loop.= "<span class='pull-right'>Email: <b>".$row['email']."</b><br></span>";
$loop.= "<div class='clearfix'></div>";
if(isset($row['website'])){ $loop.="Website: <b>".$row['website']."</b>"; }
$loop.= "<span class='pull-right'>Date: <b>".$row['date']."</b></span>";
$loop.= "<form name='submit-response' method='POST'><fieldset>";
$loop.= "<div class='form-group'> <label for='Message".$counter."'>Your Message</label> <textarea id='Message".$counter."' name='Message".$counter."' class='form-control' rows='5'></textarea> </div>";
$loop.= "<button type='submit' name='submit".$counter."' class='btn btn-default btn-block'>Reply</button>";
$loop.= "</fieldset></form>";
}
Before that is a foreach loop and the start of the oh and $counter is set to nill
What I want is for each contact form to be unique so when I click send on one of the queries it will be sent and removed so I can send another, the only issue I am having is working out how I will work out if a submit has been hit, and which submit has been hit
The code will need to workout which button has been hit and depending on which button it will then mail() to the recipient
I'm quite stuck on this one and I'm not sure of the best course of action so any advice is really appreciated
Luke
If you click a submit button inside a <form> tag, then only that form will be submitted.
You could include a hidden field with the ID of the row in it. That way you could get rid of the $counter variables altogether.
Also if you plan on just echoing out the $loop html, I wouldn't recommend storing the HTML in a PHP variable.
<?php
foreach ($listings as $row)
{
?>
<h3 class="text-center"><?php echo $row['question']; ?></h3>
<p><?php echo $row['message']; ?></p>
Name: <b><?php echo $row['name']; ?></b>
<span class="pull-right">Email: <b><?php echo $row['email']; ?></b><br></span>
<div class="clearfix"></div>
<?php
if(isset($row['website']))
{
?>
Website: <b><?php echo $row['website']; ?></b>
<?php
}
?>
<span class="pull-right">Date: <b><?php echo $row['date']; ?></b></span>
<form action="" name="submit-response" method="POST">
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<fieldset>
<div class="form-group">
<label>Your Message</label>
<textarea name="Message" class="form-control" rows="5"></textarea>
</div>
<button type="submit" class="btn btn-default btn-block">Reply</button>
</fieldset>
</form>
<?php
}
?>
add a unique id to your database table, and put it in a hidden input.
Give each form a id, and possibly each submit button a unique name. This way you can easily determine which submit button was hit, or which form was submitted, and remove it or process it via javascript.
$('form').each(function() {
$(this).submit(function(event) {
event.preventDefault();
// Add AJAX code here
$(this).remove();
});
});
Of course that was pseudo-code.
I'm trying to use AJAX to make my website a little slicker than it currently is.
I have a block of code that shows the following
if($guess == 0){
echo "Enter a guess:;
*AJAX form* ---> inserts a MySQL database record with the users guess
<div to show results of script for form> - not necessarily needed
}else{
echo "Update your guess:";
*SAME AJAX form* ---> updates the MySQL database record with the users new guess
<div to show results of script for form> - - not necessarily needed
}
The problem I'm having is that I want part of the webpage to show:
Your latest guess is: £x.xx
However because a user will start off with 0 guesses, and AJAX sends the form "behind" the scenes, I'm struggling in knowing how to show the above line once the user has made a guess.
So also that way when they revisit the page at a later date, it shows the last guess they had.
Those are the only 2 elements of the page I want to be able to refresh, the rest of the information doesn't have to refresh.
Some more code here:
<?php
if($guessess_open == 1){
echo "<h2>guesses are CLOSED</h2>";
}
else{
//it's this part that I want to have always shown on the page, but when I visit the page fresh it doesn't show me any results.
<div id="results"></div>
<form name="myform" id="myform" action="" method="POST">
<!-- The all important guess field -->
<label for="guess" id="guess_label">Guess<br></label>
<input type="text" name="guess" id="guess" size="10" value=""/>
<?php
echo "<input type='hidden' name='user_id' id='user_id' size='10' value='$user'/>";
echo "<input type='hidden' name='item_id' id='item_id' size='10' value='$itemID'/>";
echo "<input type='hidden' name='title' id='title' size='100' value='$title2'/>";
echo "<input type='hidden' name='owner_id' id='owner_id' size='10' value='$ownerid'/>";
echo "<input type='hidden' name='guesses_open' id='guesses_open' size='10' value='$guesses_open'/>";
echo "<input type='hidden' name='exist' id='exist' size='10' value='$exist'/>";
?>
<!-- The Submit button -->
<br>
<input type="image" name="submit" src="URL" width="150px" height="100px">
</form>
The only way I can get it to show something in that part of the page is to have some code in which checks a database for a guess and if it exists display the result, but then when I submit a new form, because it only updates the results it doesn't update the part of code for example could include
else{
include("URL.php?item_id=" . $itemID. "&user=" .$user. "");?>
//it's this part that I want to have always shown on the page, but when I visit the page fresh it doesn't show me any results.
<div id="results"></div>
which shows the latest guess when a user visits the page, but then when a new guess is made, this doesn't update... I don't have to have my forms php up display the result if the above part of code would update each time I make a guess... I hope this is clearer?
it has error because $_POST['sub1'] can't be accessed
is there any approach or solution to echo the value of $_POST['sub1']? or impossible? no way? even with another arrays?
i had question about my code nobody solved it! then I decide to tell it in simple way.
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
I think I know what is wrong here.
When you submit the form the second time (for sub2) you are no longer posting the value of sub1 along with it, just sub2.
This should fix it:
<html>
<form method="post">
<input type='submit' name='sub1' value='sub1'>
<?php
if(array_key_exists('sub1',$_POST))
{
echo"<input type='hidden' name='sub1' value='" . htmlentities($_POST['sub1']) . "'>";
echo"<input type='submit' name='sub2' value='sub2'>";
}
if(array_key_exists('sub2',$_POST))
{
echo $_POST['sub1'];
}
?>
</form>
</html>
You're using submit buttons. Only the button that you actually click will have its name/value pair sent to the server. When you click the sub2 button, only sub2=sub2 is sent, so sub1 won't exist in the $_POST array.
followup:
$_POST is created for you by PHP based on what's sent from the browser. The way you've built your form makes it impossible for 'sub1' to exist when you click the 'sub2' button. In other words, you need to use the SAME name= for BOTH buttons, and change the value= as appropriate:
html:
<input type="submit" name="submit" value="sub1" />
<input type="submit" name="submit" value="sub2" />
php:
if (isset($_POST['submit'])) {
echo "You clicked the {$_POST['submit']} button";
}