Here is my scenario, I present the user with a table of tests, which I have retrieved from my database in a loop and created a form for each test(row of table). So each has a submit button to execute that particular test.
basics of my loop:
while ($ts = mysql_fetch_assoc($test_info))
{
//PRESENT VALUES $ts['name'] in table within a unique form etc.
}
What I am trying to do and failing is, on clicking a particular submit button for a test, to run a JS function which checks; if the test has a password attached, if it does, present a popup form for password input, and on submitting that small form check if password is correct, if it is submit the original test form.
My problem is that I cannot parse the password value attached to that form to my javascript.
so ideally i want to do this:
<input id='submit' type='button' onclick='JSfunction(test_password)' value='execute test' >
So I can somehow parse a value from that particular form to a javascript function without actually submitting the form.
and I believe I know how to do the rest in my JSfunction.
I hope somebody can follow my poor explanation and point me in the right direction.
Many thanks,
When a form should have a password associated with it, add the following:
<input type="hidden" name="has_password" value="yes" />
<input type="hidden" name="password" value="" />
Then, in your check triggered by the submit button (assuming the button itself is the this context):
if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'yes') {
// pop password request
return false;
}
You'll need a way to store the context of the current form, and I can suggest a few if you like, but you can then populate the password into the hidden field for it and submit as normal. By the way, you might want to consider onSubmit instead of the submit button's onClick, as onSubmit will catch attempts by scripts to perform a submit. If you do this, just remove the .parent() portion of the above, as the <form> element should be the this context.
Related
i have a textbox and i wish to use the value entered in the textbox in controller- onclick of a link(not form submit). So i assume i have to use postlink to submit. But how do i get the value of that textbox in postlink?
following is my code:
<?php echo $this->Form->postLink(
'Get Coords',
array('action' => 'test', $this->request->data['Rideoffer']['PickFrom'])
);
?>
i get an error on $this->request->data['Rideoffer']['PickFrom']. data['Rideoffer']['PickFrom'] is name of my cakephp textbox(i saw it in firfox inspect element).
How do i get the textbox value?
The FormHelper::postLink method has no way of getting a textbox value. The postLink method pretty much just creates an <a> link element that submits a hidden form using Javascript. Here is an example of what postLink spits out:
<form action="/posts/delete/16" name="post_511c870e05d25" id="post_511c870e05d25" style="display:none;" method="post">
<input type="hidden" name="_method" value="POST"/>
</form>
Delete
As you can see, when you click the <a> element, it submits the form with the hidden input. You can change the value of what this input submits by passing in parameters to postLink, but you cannot dynamically get a value of a textbox that a user can modify and submit it with the form without doing something extra.
There are two similar options (one being slightly more Javascript heavy):
1) Since you are using Javascript, you can use Javascript (or jQuery) to dynamically change the value of the hidden input to whatever the user types. Even better, you can do it so the Javascript/jQuery only updates the hidden form input when the user clicks the link. Note it may be easier to not even use the postLink function and do all the form stuff yourself (or with Cake's FormHelper).
2) Don't use the postLink method at all. Create a normal form with the textbox input and mimic what postLink does. Specifically, you wouldn't have a submit button for your form. You would just basically just copy what it spits out.
<form action="test" name="UNIQUE_ID" id="UNIQUE_ID" method="post">
<input type="text" name="data[RideOffer][PickFrom]" value="POST"/>
</form>
Click
Note in the above example you should match UNIQUE_ID as the same value and you must also remove style="display:none;" from the <form> tag.
I have a form that will be validated with AJAX and return an error if the user makes any mistakes.
The problem is this: When the user enters the reCaptcha string and presses the ENTER key when the reCaptcha field is on focus, the form is Submitted instead of calling the specified function.
So for instance, let's say I have a form such as:
<form ... onSubmit='checkform()'>
<-- reCaptcha code goes here -->
</form>
As I described, when the user hits the ENTER key on the reCaptcha field, it automatically submits the form, wihtout calling the function 'checkForm()'. (I've debugged it with alert and return false in the function, and indeed seems like reCaptcha overrides the function)
Any ideas on how to prevent this, i.e., actually force reCaptcha to call the function and only be submitted if it returns true?
Thank you in advance
You can remove the submit input and just use a button
<input type="button" value="ClickMe" onclick="checkForm();" />
Also remove the
onSubmit='checkform()'
After filling the form when submit, accidentally due to some filling error ,the form is not submit and return to back,in this condition the value of all text box is blank. i want to stable value of all fields in this condition . I'm using php with smarty framework. Please reply with solution as soon as possible.
Thanks.
If the form is submitted to the page that contains it then you will have access to the submitted values, and can use them to populate your form. For example, if you are submitting the form via POST:
<input name="something" value="<?=$_POST['something']?>" />
If you are submitting the form to a different script, you could send the values back to the page with the form as URL parameters, or you could use temporary session variables, and unset them when the input passes whatever validation you are using:
$_SESSION["temp_something"] = $_POST["something"]; //In form processing script
Then in your form:
<input name="something" value="<?=$_SESSION['temp_something']?>" /> <!--In form-->
You can fill the form fields, on the second round, by filling the content inside the value attributes of html tags, like so:
<input type="text" value="<?php echo $_REQUEST['test']; ?>" name="test">
Pay attention: this is a fast and simple solution. It gives you an idea. In good web programming practice you should sanitize the form data received by client in order to avoid security issues.
I have a form with two submit buttons.
The user fills field-A and hits submit.
Done that, some input fields will be filled with data.
After that first submission, the value on the field-A should not disappear.
How can we preserve this value after the first submission?
Should we, on the field-A value attribute, place:
value="<?php echo isset($_POST['fieldA'])) ? $_POST['fieldA'] : ''; ?>" ?
The form submits to self.
Update - Additional details:
This is a form that will have two submit buttons on the same page (sort of speak).
Submit Button A - Will grab some data based on a input field, and fill the other input fields on that form.
Submit Button B - Once the form is filled, it will use all that data to do another submission.
This is a very simple case, no frameworks are in place here. I do have, however, some sort of MVP structure here.
Thanks in advance,
MEM
In general, such things being done using 2 forms, no one.
And GET method, not POST. At least for the first form.
But as you cannot ask a question, it's impossible to give you an answer.
Here you go:
index.php
<form action=edit.php>Enter name: <input name="name"><input type=submit></form>
edit.php
<? $row = dbget("row","SELECT * FROM domains WHERE name = %s",$_GET['name']); ?>
<form method="POST" action="save.php">
Edit data for <?=htmlspecialchars($row['name'])?>:</br>
NS: <input name="ns" value="<?=htmlspecialchars($row['ns'])?>"><br>
Another: <input name="another" value="<?=htmlspecialchars($row['another'])?>"><br>
<input type="hidden" name="name" value="<?=htmlspecialchars($row['name'])?>"><br>
<input type=submit>
</form>
save.php
do whatever you do usually to save info
I would store these values into $_SESSION, as user fabrik said. This way they can be stored across the entire form submission process(assuming it is multiple pages) and posted all at once at the end.
Assuming you're having some kind of submission system with a "next" button to go to the next set of forms, using session_start() and $_SESSION is certainly the best method. More information could be found here, or various tutorial sites--
http://php.net/manual/en/reserved.variables.session.php
It's ok to do that with $_POST, some people dont like the ternary operator but for me it works just fine. Although, there are better ways to deal with forms using O.O.P. You could create a class that manages your form, and pass an array to the constructor of that class (eventually you could pass the $_POST) and the class will create your form according to the info submited. You could even use the same class to valdidate your form
I don't see the need of using $_SESSIONS, cause this is not information that you need to preserve during the whole session.. or not?
Try this:
<?php
$fieldA = (isset($_POST['fieldA']) ? $_POST['fieldA'] : '')
?>
// and in your form
<INPUT type="text" name="fieldA" id="fieldA" value="<?=fieldA?>" />
as you mentioned, this should work.
I've written a simple entry form that compiles information onto a database, and I'm having trouble with my required fields...
I use a form to process the data via post method if the user neglects to fill in a required field I would like to bring him back to the original form with his/her previous fields entered.
How to I pass info back into an input label? Is there an easy way to do this without any crazy scripts? I started with sessions but I realized I have no clue how to put the stored info from the session back into the input field so the user doesnt have to retype all of their info... Also would a cookie be better for this over session?
Thanks guys,
Arthur
When you post a form, all those variables are submitted into a global array named $_POST['input_name'] and available on the page posted to. A lot of times what I like to do if I'm doing it fairly quickly, is just make the value of those input fields equal the same as what would be posting.
For example lets say we have a desired username field but the form didn't validate for some reason and posted back to itself; we don't want them to have to enter it again:
<input type="text" name="username" value="<?php print $_POST['username']; ?>" />
Of course when they first load the page, the value will be empty so there is nothing there, but if for some reason it posts back, that "username" field will already contain entered information.
Even better is java script validation, as the form doesn't have to post back, but this will do the job just fine!
Since the user posts all your data to you, these values are also available in your scripts. So you can use them very easily in the case of text-fields, but a bit more work is required for select options, checkboxes and radio buttons:
<input id="myid" name="myid" type="text"
<?php echo !empty($_POST['myid'] ? "value=\"{$_POST['myid']}\"" ?> />
For radio buttons, select options and checkboxes you instead have to check the value to see if it corresponds with the entry you are currently outputting and print selected="selected".
When it comes to validation you can also have a JavaScript validation to give feedback sooner to the user about possible failures. Just remember to have the same validation on the server side in case someone doesn't have JavaScript enabled or submits it using JavaScript, thus bypassing your client side validation.
Not sure if this is the best way, but you could redirect to a "reload" page and use the values from POST or GET to reinput the existing fields. So first validate, the fields that are required and if any are missing, redirect to this page. Then the POST or GET will have all of the values the user filled in (and the missing required fields will already be blank) so you just loop through and load up the supplied info. Additionally, if they supplied incorrect info you could manually clear it and this will also allow you to mark the missing required fields.
Another option is put your validation in JS so you know the data is good before you submit. However, I'm not sure if there are security concerns with that or not.
I check to see if the post value has been set otherwise you can show a default value, then use a bit of jQuery to remove it when the input has focus
<input type="text" name="first_name" id="first_name" value="<?php if(isset($_POST['myid'])) { echo $_POST['myid'] } else { echo "Your Name" ?>"></input>
Here's the jQuery which will remove the default Your Name when the textbox has focus.
$(document).ready(
function(){
$.fn.clearDefault = function() {
return this.focus(function() {
if( this.value == this.defaultValue ) {
this.value = "";
}
}).blur(function() {
if( !this.value.length ) {
this.value = this.defaultValue;
}
});
};
// clear default textbox entries
$("#first_name"). clearDefault();
}
);
jQuery Validation Plug-in
<input type="text" name="username" value="<?php isset($_POST['username']) ? echo $_POST['username'] : null; ?>" />
will work fine