I have a php file that contains a form (which contains 2 input boxes and a submit button) for updating a contact. I managed to fill the fields with the contact's data, but I can't detect if the submit button is clicked
form looks like this
echo "<form action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";
this code should output a "clicked" message if the button is clicked
if (isset($_POST['UpdateContact']))
{
echo "<p>clicked";
}
else
{
echo "<p>not clicked";
}
can anyone help me out or tell me what i've done wrong
(I want from the same php file to fill the contact's data in a from and to update the database)
The default method for a form is GET, so either set the form's method attribute to "post", or change your $_POST in PHP to $_GET.
echo "<form method=post action=Contact.php><table>".
"<tr><td>First Name</td><td><input type=text size=75% name=FirstName value='".$row['FirstName']."'></td></tr>".
"<tr><td>Last Name</td><td><input type=text size=75% name=LastName value='".$row['LastName']."'></td></tr>".
"<tr><td colspan=2><input type=submit name=UpdateContact value=Update></td></tr>".
"</table></form>";
or
if (isset($_GET['UpdateContact']))
{
echo "<p>clicked";
}
else
{
echo "<p>not clicked";
}
are you sure your code is reaching the php file. If yes, then the first think to check is the request type.. using $_SERVER['REQUEST_METHOD']
it will give POST in case the button was click and GET in case it the page was accessed using URL like.. google.com/Contact.php.
also, u might ned to add METHOD ="POST" in the first line of form action "form action=Contact.php
Related
I am making a webpage that allows you to add friends. For the confirm part I made a confirm button that made a post redirect to the confirm page:
while($rs=$friendsToConfirm->fetch_row()) {
echo "Friends $count to confirm's ID: $rs[0]";
echo "
<form name=\"confirm\" method=\"post\" action=\"confirmfriend.php\">
<input type=\"hidden\" name=\"userID\" value=\"$userID\">
<input type=\"hidden\" name=\"friendID\" value=\"$rs[0]\">
<input type=\"submit\" value=\"Confirm\">
</form>
";
echo "<br>";
$count++;
}
The $friendToConfirm variable is all the friends needed to confirm and is retrieved from MySQL. When I hit the button, I was expected to receive the userID and friendID from confirmfriend.php, but I did not receive anything using $_POST['userID'];. Is there another way to do this or an I doing something wrong.
This is because all of your forms have the same name [confirm]. You can try this:
$sl = 1;
while($rs=$friendsToConfirm->fetch_row()) {
echo "Friends $count to confirm's ID: $rs[0]";
echo "
<form name=\"confirm{$sl}\" method=\"post\" action=\"confirmfriend.php\">
<input type=\"hidden\" name=\"userID\" value=\"$userID\">
<input type=\"hidden\" name=\"friendID\" value=\"$rs[0]\">
<input type=\"submit\" value=\"Confirm\">
</form>
";
echo "<br>";
$count++;
$sl++;
}
Hope this will work.
Both of your hidden input fields are called "userId". I imagine the 2nd one needs to be renamed to "friendId".
All of the forms will also have the same name. It is usually preferred to ensure form names are unique.
If $_POST ['userId'] exists but is blank, check that $userId isn't blank.
To debug you can just var_dump the post array.
I have a few Radio Buttons which shell send their value in the post method, when the submit button is clicked. I am pretty sure it is easy, but for any reason it does not work. It sends the calue of the submit button instead. Please help me. This is my code for now(this code is inside a PHP script):
echo "<form action=\"nutzerverwaltung.php\" method=\"post\">";
echo "<table [...]";
while ($row = $alluser->fetch_assoc())
{
echo "<tr>
HERE---> <td><input type=\"radio\" name=\"select_to_delete\" value=".$row["id"]."></td>
<td>".ucwords(strtolower(str_replace(".", " ", $row["username"])))."</td>
<td>".$row["username"]."#via-ev.de</td>
<td>".$row["permissionlevel"]."</td>
</tr>";
}
echo "</table><br />";
echo "<input type=\"submit\" name=\"select_to_delete\" class=\"nv_button\" />
</form>";
Putting my comments to an answer, since it was clearly the issue. As posted 17 mins. prior to my answer.
Both your radio and submit form elements hold the same name attribute.
I would call that a collision/conflict. Rename one, mainly your submit button.
You can have radio buttons holding the same name attribute as an array, but not the submit button. Elements of the same "group" can have the same name attribute.
your submit input have the same name attribute just remove that
echo "<input type=\"submit\" class=\"nv_button\" />
in html forms if you have inputs with same name the last one value will be send as your value
<?php
require "../db/dbconfig.php";
$gal=mysql_query("select * from gallery");
$numrows=mysql_num_rows($gal);
if($numrows>=1)
{
echo "<form action='delete.php' method='post' name='f2' id='f2'>";
echo '<table id="rqst" style="display:block;" border="0" cellpadding="12" cellspacing="3" width="500px">';
echo "<tr><th>Tick to select</th><th>Images in Gallery</th></tr>";
while($row=mysql_fetch_array($gal)
{
$imgfile=$row['ImgName'];
$Image="<img src=gallery/".$imgfile." width='230px' height='150px'/>";
$img_name=$imgfile;
echo "<tr>";
echo "<td><input type='checkbox' name='imgs[]' value='$img_name'></td><td>".$Image."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='3' align='right'>";
echo "<input type='submit' value='Delete' name='del'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
This is my code....This will display images from gallery and checkboxes associated with them. When I click delete button with unchecked checkboxes an alert should come like this "Please check at least one checkbox"..How to do that??
My next problem is,,when I click delete button after checked checkbox, alert should come like this=" Do you want to delete?? "...If clicked Ok,the image must be deleted else do nothing...Please help ...Thanks in advance....
check this below link for validation using jquery:
http://jsfiddle.net/susheel61/U3Unk/2/
<form id="form">
First name: <input type="checkbox" name="firstname" class="check"><br>
<button>test</button>
</form>
$("button").on("click",function(e){
var status = $(".check").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
});
Yes, you can do it by javascript or jquery to validate whether your atleast one checkbox is select or not. So, for that you need to give a common class for all checkbox as example
<input type="checkbox" name="firstname" class="addchk">
Now in your submit button call a javascript function which validate the matter.
<input type='button' value='Delete' name='del' onclick='delete_checked()' />
Now write a function to validate whether any checkbox is selected.
function delete_checked()
{
var status = $(".addchk").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
else
{
// SUbmit yout form
}
}
I have a PHP code which generates a dynamic list inside a form like the following, note that the list is built dynamically from database:
echo '<form name="List" action="checkList.php" method="post">';
while($rows=mysqli_fetch_array($sql))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='hidden' name='SessionID' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
What I need is to POST the data corresponding to the user choice when he clicks on the button for that row to another page.
If we use hyperlinks with query strings there will be no problem as I'll receive the data from the other page using a GET request and the hyperlinks would be static when showed to the user.
Also I need to obtain the user input from a textbox which is only possible with POST request.
Simply from the other page (checkList.php) I need these data for further processing:
$SessionID=$_POST['SessionID'];
$Code=$_POST['code'];
As I have a while loop that generates the fields, I always receive the last entry form the database and not the one corresponding to the line (row) that the user chosed from the LIST.
I'm going to recommend that you clean up the names of variables so that your code can
at least tell us what it's supposed to do. It should be rare that someone looks at your code
and has a lot of trouble trying to see what you're trying to accomplish :P, ESPECIALLY when you need help with something ;]. I'm going to try some things and hope that it makes doing what you want easier to comprehend and perhaps get you your answer.
It's good to try your best to not echo large amounts of HTML unnecessarily within a script , so firstly I'm going to remove the
echos from where they are not necessary.
Secondly, I'm going to use a mysql function that returns an easier to process result.
$user = mysqli_fetch_assoc($sql)
Third, I don't know if form having a name actually does anything for the backend or frontend of php, so I'm
just going to remove some of the extra crust that you have floating around that is either invalid HTML
or just doesn't add any value to what you're trying to do as you've presented it to us.
And yes, we "note" that you're building something from the database because the code looks like it does =P.
I'm also sooo sad seeing no recommendations from the other answers in regard to coding style or anything in regard to echoing html like this :(.
<?php while($user = mysqli_fetch_assoc($sql)): ?>
<form action="checkList.php" method="post">
<input type='password' name='code' value='<?php echo $user['code'] ?>' />
<input type='hidden' name='SessionID' value='<?php echo $user['id'] //Whatever you named the field that goes here ?>' />
<input type='submit' value='Take Survey' />
</form>
<?php endwhile; ?>
i not sure this is correct
echo '<form name="List" method="post">';
while($rows=mysqli_fetch_array($result))
{
echo "<input type='password' name='code' id='code'>";
echo "<input type='button' value='Take Survey' onclick=show($rows[0])>";
echo "<br>";
}
and javascript
<script>
function show(id)
{
alert(id);
window.location="checkList.php?id="+id;
}
</script>
On checkList.php
$id=$_GET['id'];
echo $id;
You can just check in checkList.php whether $_POST['code'] exists and if exists retrieve $_POST['SessionID'] which will be generated from database. But one thing, if You have all hidden fields in one form, they all will be sent, so You need to think how to correct that - maybe seperate forms for each hidden field, submit button and maybe other POST fields.
And afterwards, You will be able to get data in the way You need - $SessionID=$_POST['SessionID'];
I suppose it is the easiest way to solve that.
You can try something like this:
while($rows=mysqli_fetch_array($sql))
{
$index = 1;
echo "<input type='password' name='code' id='code'>";
//Attach $index with SessionID
echo "<input type='hidden' name='SessionID_$index' id='SessionID' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
}
On checkList.php
<?php
$num = explode('_', $_POST['SessionID']);
$num = $num[1];
//in $num you will get the number of row where you can perform action
?>
$form = 1;
while($rows=mysqli_fetch_array($sql))
{
echo '<form name="List_$form" action="checkList.php" method="post">';
echo "<input type='password' name='code' id='code_$form'>";
echo "<input type='hidden' name='SessionID' id='SessionID_$form' value='$rows[0]' />";
echo "<input type='submit' value='Take Survey'>";
echo '</form>';
$form++;
}
I want to refresh the same page and display the entered value in text box on the same page after clicking the link or button.
I have the following code:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo " <input type='text' name='myTextField'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$studen_no;
}
?>
Please guide me how to achieve the goal and whats the error my code.
<?php
echo '<h3>Fee Payment</h3>';
if(isset($_POST['myTextField']))
$value=$_POST['myTextField'];
else
$value='';
echo "<form id='myFormId' name='myFormName' method='post'>";
echo " <input type='text' name='myTextField' value='$value'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
You also need a submit button in the form, and a form close tag.
What you want is AJAX. I am just providing you an example of how you can proceed by using jQuery, but you can use any other library / framework or any other way also. You can check this article also for more usability details.
In the main page:-
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo "<input type='text' name='myTextField' id='myTextField' />";
echo 'Search Student';
echo "</form>";
?>
<script type="text/javascript">
$('#inline_submit_a').click(function(evt){
$.ajax({
type: "GET",
url: "handler.php",
data: {text:$('#myTextField').val()}
});
evt.preventDefault();
return false;
});
</script>
In the "handler.php" page:-
<?php
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$student_no;
}
?>
You can write all the logic related to the database in the "handler.php" page.
Hope it helps.
You're using a link to submit the form, but you should be using a submit button.
<input type="submit" value="Search student" />
You didn't close the <form> tag.
You're using $_GET[student_no] which will make PHP look for a definition of student_no. Since it's a string, express it as one. $_GET['student_no'] is better.
Why can't you submit the form? Do you want to have a link for searching instead of a button?
You could configure the link to submit the form via javascript and change the form action to GET like this:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'
action='{$_SERVER['PHP_SELF']}' method='GET'>";
echo " <input type='text' name='student_no'>";
echo "<a href='javscript:document.forms.myFormName.submit()'>
Search Student</a>";
...