I am new here so hope I am respecting the protocol ;-)
Here's a simple issue that for some reason I cannot solve.
I have a form with post action calling a php file, and several text input fields with default values set. Example:
<form class="form-horizontal" method="post" action="profile.php">
<input type="text" class="input-xlarge" id="q-2-Address" name="q-2-Address" value="Park Avenue">
<button type="submit" class="btn btn-primary">Save changes</button>
</form>
If I submit the form leaving the default value unchanged, $_POST['q-2-Address'] contains the default "Park Avenue" as expected.
If I try to change the value in the input text field (ex: to "Madison Avenue") I can see the result in $_POST['q-2-Address'] which gets set to "Madison Avenue", as expected.
But if I try to clear the value by removing all text, the $_POST['q-2-Address'] is NOT blank and in fact goes back to containing the default value set initially "Park Avenue".
So if I want to update a form by replacing previously filled fields with blanks, this seems to be impossible, nor is it possible to detect that the field has been cleared. I tried testing using empty() but of course it gives a false result, $_POST contains the default value so is not empty even though the text field is empty.
Short of my entering something like a single space, I see no way of actually clearing a field.
It is a trivial thing but maybe somebody knows more about this.
TIA
Mike
Instead of using a default value in your form, you could use a placeholder...
<form class="form-horizontal" method="post" action="profile.php">
<input type="text" class="input-xlarge" id="q-2-Address" name="q-2-Address" placeholder="e.g. Park Avenue">
<button type="submit" class="btn btn-primary">Save changes</button>
</form>
That way, you can give an example of what you want to have in that particular input box, without the use of default text that will get picked up by the php code.
here's a jsfiddle of what the placeholder looks like.
http://jsfiddle.net/bfwst/
Related
So im trying to create a search for a name but when i click the sumbit button it goes to websitelink.com?=name=Nighel but i want it to go like this websitelink.com/Nighel/
Yes i'm already using htacces thats why the ?= doesnt work for me
I cant seem to figure out how to sort this out.
This is for searching based on name in my logs
What i use for grabbing the name
$name = isset($_GET['playername']) ? $_GET['playername'] : "";
<div class="ironman-nav">
<form>
<span class="ironman-nav__option">Search for username</span>
<input class="ironman-nav__option" type="text" name="playername" placeholder="Username..." autocomplete="off">
<input class="ironman-nav__option" type="submit" value="Submit">
</form>
</div>
You can solve this with javascript changing dynamically the form action attribute with the input value when a key is pressed over it or before submit the form.
The client side is not aware of your server rewrite rule
For the client test in website.com/test/ is just part of the URL, so if you want the browser to submit the form to website.com/test/ then set the action attribute of the form as website.com/test/
if you want the test part to be variable then you have to build the URL of the form dynamically. something like this
<div class="ironman-nav">
<form action="https://website.com/<?=$name?>">
<span>Search for username</span>
<input type="text" name="playername">
<input type="submit" value="Submit">
</form>
</div>
I'll break down my response into two parts, the first explaining the behavior of the original code, and the second will contain possible implementation routes.
HTML forms, by default, will take collate all the input tags (<input>, <textarea>, <select>, etc) as a dictionary, where the key is the node's name (the attribute) and the value is the node's value (also the attribute).
So in your case,
$name = isset($_GET['playername']) ? $_GET['playername'] : "";
<div class="ironman-nav">
<form>
<span class="ironman-nav__option">Search for username</span>
<input class="ironman-nav__option" type="text" name="playername" placeholder="Username..." autocomplete="off">
<input class="ironman-nav__option" type="submit" value="Submit">
</form>
</div>
Will do the following, on submit:
Create a dictionary containing the following key-value pairs: playername=VALUE_FROM_PLAYERNAME_INPUT
Make a GET request to the current page with the above parameters: GET /thispage?playername=VALUE_FROM_PLAYERNAME_INPUT
Since you wanted the request to go to /thispage/VALUE_FROM_PLAYERNAME_INPUT instead, you will need to modify the submission event handler for that form. Unfortuneatly it appears that Accountant م's won't worry for you since you don't have the target username at the time the search page is loaded (so the action attribute of the form tag cannot be pre-populated with the target user).
var searchForm = document.querySelector('.ironman-nav form');
searchForm.addEventListener('submit', function(evt) {
searchForm.action = '' + searchForm.querySelector('input[name="playername"]').value;
});
The reason I put an empty string in the search form action is in case you needed to prefix the action URL with anything. For example, if your search page and results page was called search.html, then searchForm.action = 'search.html' + searchForm.querySelector('input[name="playername"]').value;. You have to do this because it is a relative URL (action URL does not begin with a protocol or slash), and as such the browser will search for that resource starting from the parent of the current page.
I have this little form:
<form action="?act=process" method="post" enctype="multipart/form-data">
<input type="file" name="IMG">
<input type="text" name="BG">
<button type="submit">Save</button>
</form>
How can I skip the file input if I want to edit only the text input?
I need both because sometimes i need to channge the image.
I need to skip the input after POST submit. Only if the input wasn't changed
without seeing your PHP code for "after POST submit" I am guessing that you will simply want to code in a conditional check if the IMG input is empty or not before updating your database.
so it might look something like this:
<?php
if(!empty($_POST["IMG"]))
// update database
EDIT
The form, and checkbox submits completely fine in Internet Explorer 8. In IE 10, and Chrome 34.0 it doesn't get submitted
Ok, this one has me stumped. I have a form, each form contains one question for a quiz, its possible answers, and a checkbox to select which is the correct answer.
<form action="/department/update_quiz_question/1/1/5" method="post" class="form-horizontal form-bordered form-row-stripped" id="question_form_5">
<tr id="question_5">
<td class="col-md-4">
<textarea name="quiz_question" class="form-control" rows="4">What letter precedes B?</textarea>
</td>
<td class="col-md-5">
<div class="input-group ">
<input type="text" class="col-md-5 form-control" name="answer[]" value="A">
<span class="input-group-addon">
<input type="checkbox" checked="checked" name="correct_answer[]" value="A">
</span>
<span class="input-group-btn">
<button class="btn red btn-delete-answer" type="button" data-question-id="5" data-answer="A"><i class="fa fa-trash-o"></i></button>
</span>
</div>
</td>
<td class="col-md-3">
Add Answer
Update
Delete
</td>
</tr>
</form>
When I submit the form, it goes to a Codeigniter method which only contains:
print_r($_POST);
var_dump($this->input->post('correct_answer'));
As you can see, the checkbox is most certainly checked. It is also checked visually (IE there is a tick in the box).
When I submit the form, it goes through a jquery checking script. If all passes, jquery submits the form.
Just before I submit the form, I log the value of the checked checkbox, using the following:
console.log($(".input-group input[name^='correct_answer']:checked").val());
Which, as you would expect, outputs A, or whichever answer is ticked. So I know for certain the checkbox is ticked.
However, Codeigniter outputs the following:
Array ( [quiz_question] => What letter precedes A? [answer] => Array ( [0] => A ) )
bool(false)
The array is from the raw $_POST data. bool(false) is outputted from $this->input->post('correct_answer');.
As you can see, the checkbox value for correct_answer is not even displaying in raw $_POST data.
I can't understand what I've done wrong. I have an identical form when I create a new question, and that works perfectly fine.
Can anyone see any obvious mistakes? Or am I doing something wrong?
Many thanks
EDIT
This is what I've tried so far:
Removing all the span elements around the checkbox, leaving just the text input and the checkbox input - Still not checkbox value in the post data
removed the square brackets around the checkbox name - no post data
renamed the checkbox name, no post data
Submitted the form straight away instead of passing through a jquery check first, no post data
Changed it from a checkbox to a radio button - no post data
changed it from checkbox to text, - works, doesnt work when its a radio or checkbox
Your HTML code is invalid – you can not have a form element as child of table.
Either put the whole table into the form, or the whole form inside a td element – these are your only two options.¹
Different error correction algorithms for faulty HTML most likely explain why you see your current code working in a certain browser, but not in others.
¹ Well, if you want to stick to a table anyway – as you said in comments, moving away from using a table resolved that structural issue as well.
I am trying to submit a form, it SHOULD send forth a name, but I know I'm messing something silly up and I just can't see it (3 hours of sleep last night + new coding project at work != a smart idea)
Here's my form on one page:
<form action="add.php" method="POST">
<button type="submit" name="exportcult">Export All</button>
</form>
And here's the code on the other page, meant to process the POST:
if (!isset($_POST["name"]) || $_POST["name"] == '') {
header('Location: '.$criteria."?error=data");
die();
}
I am getting the error message this sends back, so I know it isn't registering a name - why could that be?
I think you're confused how the form data actually gets submitted. "name" is the attribute, not the key value that is found in the POST data. You need to specify the name for that element, which will be the key value present in the POST data. You have specified name="exportcult" so in the POST data, the variable will be at $_POST['exportcult']. However, this value will always be an empty string since you have not indicated a value attribute for your button.
Keep in mind that when dealing with submit buttons, only the value of the button which was used to submit the form will be included along with the rest of the form data. Try using this:
<button type="submit" name="exportcult" value="export">Export All</button>
If that specific button was used to submit the form, then $_POST['exportcult'] should be equal to 'export'.
For those of you who are unsure: buttons do get submitted with the form, but they still have to have a value attribute.
Your form doesn't contain any field except the button, so $_POST will only contain a field exportcult.
Edit: Since you use <button> instead of <input> it might not go into the POSTed data.
Do:
if (!isset($_POST["exportcult"]) || $_POST["exportcult"] == '') {
header('Location: '.$criteria."?error=data");
die();
}
You're currently checking for a field named "name", when the field is named "exportcult". Additionally, it should be <input, not <button.
you should add
<input type="button" name="exportcult" value="Whatever you want" />
and check for exportcult on the isset() instead of name
use exactly this:
<form action="add.php" method="POST">
<input type="submit" name="name" value="Export All"></form>
if (!$_POST["name"])) {
header('Location: '.$criteria."?error=data");
exit();
}
I just started using twitter bootstrap, and I got stuck trying to get my form submit button to submit (PHP $_POST). Does anyone know why its not working?
No clue really..
I've been doing this previously and its been working until bootstrap:
<?php
if (isset($_POST["login"]) && $_POST["login"]=="login") {
echo "login here";
}
?>
<form action="http://mysite.com" method="post">
<input type="hidden" id="login" name="login" value="login" />
<button class="btn success" type="submit">Login</button>
</form>
The page seems to load but the PHP does not. Is the POST information being blocked?
My form input elements did not have an id or name (I didn't include them in my example).
Not an HTML expert, but I've always used <input type="submit" /> and not <button type="submit" />, not sure that button is a functional form element.
Also, your button element is closed twice - first with /> in the opening tag, and then again with a closing tag for no reason.
Maybe you are wrong with the action="http://mysite.com"? That attribute is for specifing the form data receiver, so in your case I think it's the page itself. So, if your page is named mypage.php, use action="mypage.php".
I had this same issue as well, turns out I was missing the name="submit". Gotta have that so the $_POST has a key in the assoc array!
it is adviseable that every element has a name thus (name="elementName") this makes it easy to reference them in your php scipt; also ensure that a form action and method are set.