I have this code in a foreach that lists uniquecode links:
<a id="<?php echo $id_to ?>" href="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" >
<?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?>
</a>
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send"></input>
</form>
this is what I get when I view the page source:
<a href="http://www-rainbowcode-mobi/messageSent.php?id=36" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" >
KUZELJA<span class="pink_text">000</span>RC
</a>
<form id="message_area" style="display:none" method="post" action="http://www-rainbowcode-mobi/messageSent.php?id=36">
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send"></input>
</form>
<a href="http://www-rainbowcode-mobi/messageSent.php?id=38" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" >
ALANZIM<span class="pink_text">000</span>RC
</a>
<form id="message_area" style="display:none" method="post" action="http://www-rainbowcode-mobi/messageSent.php?id=38">
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send"></input>
</form>
the problem is when the action fires and page goes to messageSent and I view page source again $id_to is NOT the id of the link I clicked on. It takes the first link's id regardless of which link I click on?
here the messageSent page source (I clicked on link with id 38 NOT 36):
where I have a print_r($_REQUEST) and it gives:
Array
(
[id] => 36
[message] => bj,nbgj,
[Submit] => Send
)
.
function showMessageArea(link)
{
var message_area = document.getElementById('message_area');
message_area.parentNode.removeChild(message_area);
link.parentNode.insertBefore(message_area, link.nextSibling);
message_area.style.display="block";
}
The problem is indeed the non-unique ids.
Try appending $to_id to the form ids, so that they are unique (e.g. <form id="message_area_<?php echo $to_id; ?>" ...).
And then update showMessageArea function to do this:
var message_area = document.getElementById('message_area_'+this.id);
This way you will be operating on the desired form element.
As a refactoring suggestion, I would suggest using a single form instead and make id parameter to be <input type='hidden' name='id' id='message_id' value=''> and set it's value from the showMessageArea(...) like this:
document.getElementById('message_id').value = this.id;
Shouldn't you have semi-colons after the php statements?
EDIT:
I think the problem is that there are two elements with the id "message_area", so it always refers to the first one. Try only having one form, and setting the action of that form when one of the links is clicked.
I don't know if this will fix it because I can't see an issue but could you try the following code:
<a id="<?php echo $id_to ?>" href="<?php echo ADDRESS; ?>messageSent.php class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" >
<?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?>
</a>
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<textarea name="message" rows="10" cols="20"></textarea>
<input type="hidden" name="id" value="<?php echo $id_to ?>" />
<input name="Submit" type="submit" value="Send" />
</form>
And then try print_r($_POST)
EDIT:
Okay, following your comment Helloise, I believe the following may work:
<a id="messageid_<?php echo $id_to ?>" href="javascript:void(0);" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this,'<?php echo $id_to ?>'); return false;" >
<?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?>
</a>
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php">
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send" />
<input type="hidden" id="message_id" name="id" value="" />
</form>
<script type="text/javascript">
function showMessageArea(link,messageId)
{
document.getElementById('message_id').value=messageId;
message_area.parentNode.removeChild(message_area);
link.parentNode.insertBefore(message_area, link.nextSibling);
document.getElementById('message_area').style.display="block";
}
</script>
So, here the main point is to have one form as you have stated but to pass the id we are using into the javascript function. From there, you can set the value of the hidden input which will then be included in the POST values.
Try something like this:
after quick peruse...
your missing $ before ADDRESS. It should be $ADDRESS. I assume thats a variable (ignore this, overlooked its just a constant)
your missing semi-colon after many lines of php.
<a id="<?php echo $id_to; ?>"
href="<?php echo $ADDRESS;?>messageSent.php?id=<?php echo $id_to; ?>"
class="charcoal_link"
style="line-height: 20px;"
onclick="showMessageArea(this); return false;"
<?php echo $uniqueCode1;?>
<span class="pink_text">
<?php echo $uniqueCode2?>
</span>
<?php echo $uniqueCode3;?>
></a>
<form id="message_area" style="display:none" method="post" action="<?php echo $ADDRESS; ?>messageSent.php?id=<?php echo $id_to; ?>">
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send"></input>
</form>
Related
I am quite new to PHP so I need some help with my project.
I have a form on the page where the user writes its content and name, like this:
<div class="white shadow padding-10 margin-bottom">
<form class="form xl relative" method="POST" action="
<?php echo $_SERVER['PHP_SELF']; ?>
">
<div class="col-1 col-persist -margin">
<img class="pull-left width-100 round" src="images/logo.gif" alt = "logo"/>
</div>
<div class="col-9 col-persist gutter-h-10 padding-top-5 -margin">
<textarea placeholder="What is new?" name="content"></textarea>
<input type="text" name="name" placeholder="What is your name?">
</div>
<div class="col-2 col-persist -margin">
<input type="button" name="submit" value="Send">
</div>
</form>
</div>
PHP_SELF I use to stay on the same page
In php part I have this:
$userName = "";
$userInput = "";
if($_SERVER["REQUEST_METHOD"] === "POST"){
$userName = $_POST['name'];
$userInput = $_POST["content"];
}
if(isset($_POST)){
echo '<div class="white shadow padding-10 margin-bottom">';
echo '<form class="form xl relative" method="POST">';
echo '<div class="col-1 col-persist -margin">';
echo '<img class="pull-left width-100 round" src="images/logo.gif" alt = "logo"/>';
echo '</div>';
echo '<div class="col-9 col-persist gutter-h-10 padding-top-5 -margin">';
echo '<textarea placeholder="What's new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What's your name?">'.$userName;
echo '</div>';
echo '</form>';
echo '</div>';
}
However, this code does not work at all. When debugging, the browser shows some problem in these lines:
echo '<textarea placeholder="What's new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What's your name?">'.$userName;
What is the problem? I guess it is something in syntax, but maybe more in code logic itself?
Thanks in advance!!!
First your form needs a submit button, so change <input type="button" name="submit" value="Send"> to <input type="submit" name="submit" value="Send">
Also, you don't need to use PHP_SELF, simply leaving the action attribute as action="" will keep it on the same page.
As for the error in the php, you have single quote marks that are not escaped. This should work:
echo '<textarea placeholder="What\'s new?" name="content">'.$userInput.'</textarea>';
echo '</div>';
echo '<input type="text" name="name" placeholder="What\'s your name?">'.$userName;
You need to change <input type='submit' name='submit' value='send'> in your html and not button.
I am new to PHP and I am trying to get information from a form to another page, but the data won't transfer over when I hit submit. What am I doing wrong? Should I be trying to use GET instead of POST? What is the best way to debug something like this?
The path to information.php is definitely correct.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label name="tempID"><?php echo $number; ?></label>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
This file is in a different page (information.php)
if (isset($_POST["tempID"]))
{
$infoID = $_POST['tempID'];
}
echo $infoID;
<input type="hidden" name="tempID" value="<?php echo $number; ?>" />
Add this next to your original echo, post variables can't be stored in a label. Also remove the name value from the label
You need a submit input instead of a button with the name submit. Change the button's html to:
<input type='submit' value='More Details'>
Change Label in input
<form action="information.php" method="post">
<div class="row" style="padding-bottom: 20px;">
<input name="tempID" value="<?php echo htmlentities($number); ?>"/>
<button class="btn" name="submit" type="submit">More Details</button>
</div>
</form>
labels are only to display information. they are not submitted during form submission.
<form action="information.php" method="post" type="post">
<div class="row" style="padding-bottom: 20px;">
<label>Number:</label>
<input name="tempID" value="<?php echo $number; ?>"/>
<input class="btn" id="submit" name="submit" type="submit" value="More Detail" />
</div>
</form>
I want to check if either of my search fields are empty, and if they are i write out a failure message. For some reason the following code doesn't work when I try to submit empty values in both search fields, or leaving the q2 search field empty. It only writes out the failure message when I only insert a value into the first field. Hope you guys can help.
<div id="searchField">
<form name="searchField" action="" method="get">
<input type="search" name="q" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="search" name="q2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>
<input type="hidden" id="v" name="v" value="2" />
<input type="hidden" id="b" name="b" value="" />
<button type="submit" id="loop" style="border:0px; background:transparent; vertical-align:middle; margin-left:-85px; margin-top:-3px;">
<img src="images/loop.png" width="75" height="75" alt="submit" />
</button>
</form>
</div>
<div id="theData">
<?
if(str_replace(" ", "",$_GET['q'])!="" && str_replace(" ","",$_GET['q2'])!=""){
$qContent = $_GET['q'];
$succes = mysql_query("SELECT*FROM film WHERE name='".$qContent."'");
include("searchMaster.php");
if(mysql_num_rows($succes)) {
while($o = mysql_fetch_array($succes)){
echo $o['name'];
if($o['trailer']){
echo '<div id="ytplayerWrap">';
echo '<iframe id="ytplayer" type="text/html" width="640" height="390"
src="http://www.youtube.com/embed/'.$o['trailer'].'?http://amnestic.ueuo.com"
frameborder="0"/>';
echo '</div>';
}else {
echo '<span style="font-size:20px;">';
echo '<br /><br /> No trailer was found for <em>'.$_GET['q'].'</em> :(';
echo '</span>';
}
}
}else{
echo 'You did the fuckup :(';
}
}else{
echo 'You did the fuckup :(';
}
?>
The name and id of your input fields must be same
Like this
<input type="text" name="submitQuery" id="submitQuery" autofocus="autofocus" value="<?= $_GET['q']; ?>" placeholder="enter one movie"/>
<input type="text" name="submitQuery2" id="submitQuery2" value="<?= $_GET['q2']; ?>" placeholder="and then another one"/>
I have a foreach loop listing the unique codes as links:
<a href="#" class="charcoal_link" value="<?php echo $id_to ?>" style="line-height: 20px;" onclick="showMessageArea(this); return false;">
<?php echo $uniqueCode1?>
<span class="pink_text"><?php echo $uniqueCode2?></span>
<?php echo $uniqueCode3?>
</a>
<input type="hidden" name="id" value="<?php echo $id_to ?>" />
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<tr>
<td>
<input name="message" type="textarea" rows="10" cols="20" value="<?php echo $message ?>" />
</td>
<td>
<input name="Submit" type="submit" value="Send" />
</td>
</tr>
</form>
the above is also in another form tag: <form name = "contacts" method="post">
the action part works fine but in messageSent.php i do:
var_dump($_POST);
var_dump($_GET);
and the output gives me:
NULL
array(1) { ["id"]=> string(2) "36" }
Where is "message"?
"id" contains the wrong id. It is not the id of the unique code I clicked on.
Your HTML is invalid. Among the many errors you have made: You can't have a form inside a table but around a tr. The browser is error correcting by moving the form element to somewhere where it is allowed, and leaving the inputs alone (since inputs can be inside td elements).
Validate. Validate. Validate.
There are so many problems with that fragment of HTML that you have shared, that you would probably benefit from a beginner's guide to authoring webpages.
<a href="#" class="charcoal_link" value="<?php echo $id_to ?>" style="line-height: 20px;" onclick="showMessageArea(this); return false;">
<?php echo $uniqueCode1?>
<span class="pink_text"><?php echo $uniqueCode2?></span>
<?php echo $uniqueCode3?>
</a>
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<input type="hidden" name="id" value="<?php echo $id_to ?>" />
<table>
<tr>
<td>
<textarea name="message" rows="10" cols="20"><?php echo $message ?></textarea>
</td>
<td>
<input name="Submit" type="submit" value="Send" />
</td>
</tr>
</table>
</form>
You cannot have a form inside a form.. you must do them seperate or else in the same form....
I fixed the HTML markup for you
i have the following which is within a foreach loop and lists the unique code as a link:
<a href="" class="charcoal_link" style="line-height: 20px;" onclick="showMessageArea(this); return false;" >
<?php echo $uniqueCode1?><span class="pink_text"><?php echo $uniqueCode2?></span><?php echo $uniqueCode3?>
</a>
<form id="message_area" style="display:none" enctype="multipart/form-data" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
<textarea name="message" rows="10" cols="20"></textarea>
<input name="Submit" type="submit" value="Send"></input>
</form>
i want to pass the id of the uniquecode i clicked on but $id_to = the first link's id i also tried to pass $id_to as a hidden field inside the form tag but still it takes the id of the first uniquecode link regardless of which link i click on
please help?
thank you
You messed up with ".
Try with:
<form id="message_area" style="display:none" method="post" action="<?php echo ADDRESS; ?>messageSent.php?id=<?php echo $id_to ?>">
The form action can be another PHP script which can read POSTed variables. Add id as a hidden field to POST.