When I try to do
Search in Form
i get in address bar the following entry
check.php?go_btn=&s=banana
Why do I get the name of the button "go_btn"
I should get the value
check.php?s=banana
Why this value
name = "go_btn"
Appears in the address bar
simple_search.html
<form action="check.php" method="get" id="my_form">
<input name="go_btn" type="submit" id="go_btn" form="my_form" formaction="check.php"
formmethod="get"
style="background-image: url(images/go.png); border: solid 0px #000000; width:
71px; height: 55px;" value="">
<input type="text" name="s" id="s">
</form>
check.php
<?php
if (isset($_GET['go_btn'])) {
$s_find = $_GET['s'];
if ($s_find == "banana") {
print ("you find banana");
}
else {
print ("blablabla....");
}
}
?>
You get it because you asked for it:
<input name="go_btn" [..snip...] value="" />
^^^^^^^^^^^
Any <input> field with a name attribute will get submitted along with the rest of the form. If you don't want go_btn being submitted, then take away the name attribute.
Well, In your PHP instead of asking about go_btn ask for s as follows:
<?php
if (isset($_GET['s'])) {
$s_find = $_GET['s'];
if ($s_find == "banana") {
print ("you find banana");
}
else{
print ("blablabla....");
}
}
?>
And then remove the attribute name from your form's element go_btn as follows:
<form action="check.php" method="get" id="my_form">
<input type="submit" id="go_btn" form="my_form" formaction="check.php"
...
You will get it because you asked for it. Your form method is GET and the GET method takes all values with it in the URL. If you would like not to see those values in the URL, you may use the POST method.
Moreover, you shouldn't be having ANY problem with that term in the URL, simple because you need that variable in the other file.
Related
I don't know what happened but there's no value return.
Please check if i made any mistake
Value is blank if you have no check for null or blank value on transfer.php
Case 1 : You are access file in same flow then no need to modify any thing its wokring.If you are access file without press submit button.directly using url then post is blank.
e.g. http://localhost:8080/stackoverflow/transfer.php
Case 2 : MIME type issue, enctype="text/plain" in the form, this should fix the issue.
from_post.php
<html>
<body>
<form action="transfer.php" method="post" enctype="text/plain">
<input type="text" name="name" value="">
<input type="submit" name="submit" value="submit">
</form>
transfer.php
Here you want to check is that submit button if fired or not.
<?php
if(isset($_POST['submit']))
{
$u=$_POST['name'];
if(isset($u) || (!is_empty())){
echo $u;
}
else
{
echo "the post doesn't have any value";
}
echo "<br/>";
}
?>
I am trying to run the below query which basically create a submit button. However when I am trying to click the Accept Request button it is not resolving the if condition and directly echo what is written in else condition.
Can someone help why this would be happening?
<?php
if(isset($_POST['acceptrequest'.$user_from]))
{
echo "you are now freind !";
} else
{
echo 'Error in reading - acceptrequest'.$user_from;
}
?>
<form action = "friend_request.php" method = "POST">
<input type="submit" name = "acceptrequest<?php echo $user_from;?>"
value="Accept Request" style = "margin-left: 5px;" />
<input type="submit" name = "ignorerequest<?php echo $user_from;?>"
value="Ignore Request" style = "margin-left: 5px;" />
</form>
In general, you ought not use variables to define the name of a form input.
Specifically because of the "variable" nature of variables, you can't be sure that the name posted will match the name you're looking for.
In order to keep your application stateless, you should instead post the variable kn its own hidden input.
Below, I modified your form to post user_from as its own input, separate from acceptrequest and ignorerequest.
This should fix any state issues that you were experiencing before.
<?php
if (isset($_POST['acceptrequest')) {
echo "You are now friend with {$_POST['user_from']}!";
} else if (isset($_POST['ignorerequest')) {
echo "You ignored the request from {$_POST['user_from']}!";
} else {
echo 'Error in reading - acceptrequest'.$user_from;
}
?>
<form action = "friend_request.php" method="POST">
<input type="hidden" name="user_from"
value="<?php echo $user_from;?>" />
<input type="submit" name="acceptrequest"
value="Accept Request" style = "margin-left: 5px;" />
<input type="submit" name="ignorerequest"
value="Ignore Request" style = "margin-left: 5px;" />
</form>
Maybe You haven't set all variables. Try to define that and use: $
So create something like: $acceptrequest == something
I want form to post automatically if zip variable is passed from URL.
URL looks like: www.sitename.com/maps/zipsearch.php?zip=90210
Form looks like:
<form method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
So it fills the input box with zip code but I would like it to post automatically to see results again if zip is passed.
Maybe an IF / THEN?
Any help would be appreciated.
You mean to echo the value passed in GET parameter?
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
EDIT
Or, if you are asking about submitting the form, then something like this might work I believe:
<input type="submit" name="subbut" value="<?php echo isset($_GET['zip'])?$_GET['zip']:'Find'; ?>" />
<?php if( isset( $_GET['zip'] ) ) { ?>
<script>
document.forms["name_of_the_form_here"].submit();
</script>
<?php } ?>
like this:
<form id="form" action="form.php" method="post">
Zipcode:
<input name="zip" value="<?php echo (isset($_GET["zip"]))? $_GET["zip"]:"";?>" />
<input type="submit" name="subbut" value="Find instructors" />
</form>
<?php if (isset($_GET["zip"])): ?>
<script>document.getElementById('form').submit()</script>
<?php endif; ?>
since passing data via URL means GET method, so i think you have a little misconception with your question.
if you would like to post automatically you dont need to show form.
just put this code in your zipsearch.php
if ($_GET['zip'] != ""){
// do what you want if zip parameter is not null
}else{
// do what you want if zip parameter is null
}
It looks like your form is submitting to itself. (Eg. zipsearch.php displays HTML form. When user submits form, it is posted back to zipsearch.php which displays the search results).
If this is the case, you don't have to post anything, because you are already inside the file that handles the form submission. You could do something like this:
<?php
if (isset ($_POST['zip'])) {
$zip = $_POST['zip']; /* Form was submitted */
} else if (isset ($_GET['zip'])) {
$zip = $_GET['zip']; /* "?zip=" parameter exists */
}
if (isset ($zip)) {
/* Display search results */
} else {
/* Display form */
}
I have a while loop wich creates forms like:
$result3 = mysql_query("SELECT * FROM Afbeeldingen WHERE ImgId =$imgid ORDER BY AfbeeldingPrior DESC");
while($row3 = mysql_fetch_array($result3))
{
?>
<form method=POST name="form3" action="kamer.php">
<input type="hidden" name="id" value="<?php echo$kamerid;?>">
<input type="hidden" name="afbeeldingplus" value="12345">
</form>
<?php
}
I want to post these forms with a text link.
Normally I use
<script>
function f3_submit()
{
document.form3.submit();
}
</script>
and then I put echo "<a href=\"##\" onClick=\"f3_submit();\" >";
under the form
but because I got a lot of forms with the same name this wont work.
it doesn't post anything !
How can I post these forms with a text link, so without a submit button.
Remember to return false!
<script>
function submitForm(kamerid) {
document.forms["form"+kamerid].submit();
return false;
}
</script>
and have
echo '<a href="#" onClick="return submitForm(\''.$kamerid.'\');" >';
If I understand correctly, you want to have many different forms, and many links submitting different forms? If that's true, you have to name the forms differently, e.g. using the ID you get from DB, and then use this generated name in submit links.
Like:
<?php
$result3 = mysql_query("SELECT * FROM Afbeeldingen WHERE ImgId =$imgid ORDER BY AfbeeldingPrior DESC");
while($row3 = mysql_fetch_array($result3))
{
?>
<form method=POST name="form3_<?php echo $kamerid;?>" action="kamer.php">
<input type="hidden" name="id" value="<?php echo $kamerid;?>">
<input type="hidden" name="afbeeldingplus" value="12345">
</form>
<?php echo $kamerid;?>
<?php
}
I'm not sure that you're creating these forms in the way that you'd like to. But if you're happy with the way they are, then you can add in a submit button, and then just style it to look like a text link with css:
form input[type="submit"]{
background: none;
border: none;
color: blue;
text-decoration: underline;
cursor: pointer;
}
Maybe loop through the values and generate two comma (or pipe or whatever suits the task) separated strings and then create just one form and assign "id" and "afbeeldingplus" fields with these values. Your javascript submission will work fine and you have less HTML that having all these fields repeated either in one single form or across multiple forms.
while($row3 = mysql_fetch_array($result3))
{
<form method=POST name="form3" action="kamer.php">
<input type="hidden" name="id" value="<?php echo$kamerid;?>">
<input type="hidden" name="afbeeldingplus" value="12345">
<button type="submit">Text to Submit here</buttton>
</form>
}
and then just style the button to look like text
I am trying to write a dynamic form using PHP. I'd like to have a single webpage that contains two forms:
The upper form allows to search for an element in the mysql database, e.g., for a name
The lower form shows the data that is associated with this name in the database
If I press on the "Search" button of the upper form, then the the lower form is shown and the text fields are filled with data from the database that belong to this name. If I change the user name to some other value and press again "Search", then the data that is associated with the new record is shown and so on.
The lower form also has a button "Update" which allows to transfer changes made to the text boxes (in the lower part) to the database.
Now, I have the following problem: In my script I set initially the value of name (from the upper form) to "". When I then press the "Search" button, then the lower part of the form is shown and the corresponding data is shown in the lower part. When I then press the "Update" button, then the text field associated with name is set to the empty string. This is because in my script I set initially name to the "". I'd like that in this case the data entered in the upper form is not changed, i.e., it stays the same.
I guess, I am missing something here. There is probably an easy solution for this and I am doing something fundamentally wrong. It'd be great if you could help me.
That's what I tried... I deleted lots of details, but I guess that can give you an idea what I am trying to do. Notice that the whole code is in the file update.php.
<?php
function search_bus($mysql, $name)
{
// do some stuff here...
}
function update_bus($mysql, $b_id)
{
// do some stuff here...
}
// some global variables
$b_id = 0;
$username = ""; // username of business
// get b_id that corresponds to username
if (isset($_REQUEST['search']))
{
$b_id =0; // business id
if (isset($_POST['user']))
{
$username = $_POST['user'];
$b_id = search_bus($mysql, $username);
}
}
elseif(isset($_REQUEST['update']))
{
update_bus($mysql, $b_id);
}
?>
<h2>Search:</h2>
<form name="search_bus" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="submit" value="Suchen" name="search"/>
</form>
<?php
if($b_id != 0)
{
?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<-- some form follows here -->
<?php
}
?>
I think what you're missing is to create a HTML Hidden field to keep the value of Name variable.
<input type="hidden" name="name" value="<?php print $nameVar ?>" />
Add this input to both forms so you can keep the value no matter what button the user clicks.
Hope this helps.
Adding code to verify the
<h2>Search:</h2>
<form name="search_bus" method="post"
action="<?php echo $_SERVER['PHP_SELF'];?>">
Username: <input type="text" name="user" value="<?= htmlentities($username) ?>"/>
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<input type="submit" value="Suchen" name="search"/>
</form>
<?php if($b_id != 0) { ?>
<h2>Data:</h2>
<form name="business_design" method="post" action="<?php echo $_SERVER['PHP_SELF'];>">
<input type="hidden" name="b_id" value="<?php print $b_id?>" />
<-- some form follows here -->
<?php } ?>
Dont initialize $b_id if it already comes into the http request.
if (!isset($_POST['b_id']))
{
$b_id = 0;
}
else
{
$b_id = $_POST['b_id'];
}
This way you can alway remember the last selected value of b_id.
Hope this can help you.