value is not set after the form is submitted - javascript

I m trying to insert user's check in value into db. For this i call the function and set the hidden field's value and than trying to submit the form but here after the form is submiteed hiden field's value is reset .
HTML Code
<form name="checkinout" action="logindetails.php" method="post" enctype="multipart/form-data" id="checkinout">
<table class="login-detaills" width="100%"><thead><th></th><th>Check In</th><th>Check Out</th><th>Hours</th></thead>
<tr><td></td><td></td><td></td><td></td></tr>
</table>
</fieldset>
<table>
<tr><td><input type="button" onClick="call()" value="checkin"></button></td><td><input type="button" id="logout" onclick="call2();" value="Check Out" ></td></tr>
<tr><td><input type="hidden" id="checkin" name="checkin"></td><td><input type="hidden" id="checkout" name="checkout"></td></tr>
</table>
</form>
Javascript
function call(){
$('#checkin').val("checkin");
$('#checkinout').submit();
}
function call2(){
$('#checkout').val("checkout");
$('#checkinout').submit();
}
PHP CODE
if(isset($_POST['checkinout']) ){
if(isset($_POST['checkin']) && $_POST['checkin']=='checkin'){
$sql = "INSERT INTO attend (loginout,log_type,fk_userid) VALUES ('".TODAY_DATETIME."','I','".$_SEESION['user_id']."')";
}elseif(isset($_POST['checkout']) && $_POST['checkout']=='checkout'){
$sql = "INSERT INTO attend (loginout,log_type,fk_userid) VALUES ('".TODAY_DATETIME."','O','".$_SEESION['user_id']."')";
}
$stmt = $class->dbh->prepare($sql);
$stmt->execute();
$res = $stmt->fetch(PDO::FETCH_ASSOC);
}
first of all its not satisfied this if(isset($_POST['checkinout'])

<input type="hidden" id="checkout"> has no name attribute, so even if you give it a value, it won't be a successful control and won't be submitted.
If you want $_POST['checkout'] to be populated, you need an element with name="checkout".

You dont have name attribute defined in you code ..For using $_POST['checkout'] you must have <input type="hidden" id="checkout" name="checkout">

It looks like you're checking if the post value $_POST['checkinout'] is set, but I dont see that form control anywhere. The only controls I see are these:
<input type="button" onClick="call()" value="checkin"></button>
<input type="button" id="logout" onclick="call2();" value="Check Out" >
<input type="hidden" id="checkin" >
<input type="hidden" id="checkout">
Like #Quentin said, they don't have a name attribute so they won't show up in the $_POST array after submission in any event, so that's another thing you must fix.
I think you're most likely assuming that, since the form id is checkinout, that the index $_POST['checkinout'] will be set upon form submission. This is not the case; only form controls (inputs, selects, checkboxes, buttons, etc.) will populate data inside of $_POST and the key inside of the $_POST array will be set equal to the value of the name attribute of the form control.

you really should consider using two submit button with your from, take a look at this question, and remove the hidden input and all the unnecessary js code

Related

How to get input data from html to nodejs [duplicate]

When I submit form by using below function it is submitting but values are not passed through this function. I use all functions but nothing found:
document.getElementById("postad").submit();
Form is given below.
<form action="register.php" id="postad" method="post">
<input class="textfield2" type="text" id="post_title" style="width:640px;" placeholder="Ad Title" onBlur="check('post_title')" />
<input class="button" type="button" name="save" value="Publish" onclick="send();" />
</form>
Your form contains two form controls. Neither will be a successful control (i.e. one that appears in the submitted data), but for different reasons.
Only form controls with name attributes can be successful. Your text input doesn't have a name. (It also doesn't have a default value, so you need to type in it first).
Buttons can only be successful if they are the submit button used to submit the form. Your button isn't a submit button and you use JavaScript to submit the form.
There is no name attribute in your input text fields
<input name="post_title" class="textfield2" type="text" id="post_title" style="width:640px;" placeholder="Ad Title" onBlur="check('post_title')" />
.........^

Get values submitted to php from form

Consider the following form:
<form method="get" enctype="multipart/form-data">
<input type="hidden" value="0" name="foo">
<input type="checkbox" value="1" name="foo">
</form>
When submitting the form, the URL ...?foo=0 will be requested if the checkbox is not checked. If the checkbox is checked, the URL ...?foo=0&foo=1 will be requested. In PHP, query string arguments override any previous arguments with the same name, so foo will have the value 1 in the PHP script handling the latter request.
What is the best way to obtain the value foo would have in the PHP script using JavaScript, when not knowing anything about the form? In theory, there could be an arbitrary number of inputs named foo of different types, and I would like to know the value that foo would have in the PHP script handling the request if the form was submitted.
As I understand it, the answer is the value of the last enabled input element named foo. By enabled, I mean that the input element is not disabled and that it is not a button (the button's name and value are not added to the query string unless the button is used to submit the form) or an unchecked checkbox or radio button.
Maybe there is an easy way to get this value using jQuery?
Edit
Loads of people suggest that I rename the input elements to foo[] or similar. I guess I was not clear enough that I actually want all the input elements named foo, and to only receive one of the values in the php script.
My questions is how to determine which value the php script will receive using JavaScript.
use this:
<form method="get" enctype="multipart/form-data">
<input type="hidden" value="0" name="foo[]">
<input type="checkbox" value="1" name="foo[]">
</form>
and in PHP, use:
$params = $_GET['foo']; //params would hold array
it would be good to use.
You can do a loop on the global variable $_GET
foreach($_GET as $key=>$val) {
echo "key = $key , val = $val";
}
if you have multiple inputs with the same name you will need to append []to the name so you can get all the values as a array
<form method="get" enctype="multipart/form-data">
<input type="hidden" value="0" name="foo[]">
<input type="checkbox" value="1" name="foo[]">
</form>
to get the values in jquery you do the following:
$('input[name="foo[]"]').each(function(){
console.log($(this).val());
});
if you only want one value of foo but you have multiple elements with that name you will need to change your logic to using radio buttons
This is really bad design. You should check checkbox state in your php (backend) code. Get rid of the hidden input:
<form method="get" enctype="multipart/form-data">
<input type="checkbox" value="1" name="foo">
</form>
and put this in your php code:
if (isset($_GET['foo'])) {
$foo = 1;
} else {
$foo = 0;
}
If you want to use javascript, you can try this on form submit:
var checkboxValue = $("input[name='foo']").prop("checked");
checkboxValue would be true or false depending on checkbox status.

How to change div id name based on form value?

I have a couple forms on a page with a single button and a hidden input field with a value already pre-set:
<form action="product.html" method="get">
<fieldset>
<input style="display: none;" type="text" name="RSS" id="RSS" value="RSS" />
<input type="submit" value="Go"/>
</fieldset>
</form>
<form action="product.html" method="get">
<fieldset>
<input style="display: none;" type="text" name="RSS2" id="RSS2" value="RSS2" />
<input type="submit" value="Go"/>
</fieldset>
</form>
Once they hit the Go button on either form, it will redirect to the product.html page where a specific div loads based on the value above.
<div id="ID CHANGE TO OCCUR HERE to either be RSS or RSS2"></div>
My question is, how do I get that id to change on that div?
Thanks
PS: PHP is not enabled on the company servers...so yeah..yeah...
If I understand you correctly, when program control transfers over to product.html, you wish to discover which form value has come across (i.e. which form did the user click).
I cannot think how you would do this solely with HTML. This is a job for a server-side language like PHP or ASP .Net.
It's pretty simple in PHP. Note that you can take all your existing HTML files and simply rename them to .php (eg. product.php) and they will still work the same.
Just put this at the top of the file -- in fact, this is the complete file (just copy/paste to your server to test):
product.php
<?php
/* Below not required, but un-comment to see useful info:
echo '<pre>';
print_r($_REQUEST);
echo '<pre>';
*/
if (isset($_REQUEST['RSS'])) {
echo 'User clicked the RSS form';
} else if (isset($_REQUEST['RSS2'])) {
echo 'Sent here by the RSS2 form';
}
Since the name of the processing file has changed, remember to change the action= line in each of your forms before trying this:
<form action="product.php" method="get">
Explanation:
When a form is submitted, the form elements (input fields, radio buttons, checkboxes) are turned into variables and sent to the processing document (the target document specified in the action= attribute of the form tag).
For each element, the variable name is the name= attribute for that element, and the variable value is either the value= attribute, or, in the case of an input field for example, whatever the user typed into the field before pressing submit.
The is very little difference to the programming/functionality between sending the form as method="Get" or method="POST", but the post method is more secure and can transfer more information, so most of us use that.
Finally, on the other end, there are three ways to get the variable values (PHP Example):
$newvar = $_GET['varname']; //if method="GET" was used
$newvar = $_POST['varname']; //if method="post" was used
$newvar = $_REQUEST['varname']; //works for both
If you need more assistance with PHP, view some of the Alex Garret's free ten-minute videos on the New Boston or on his own site.
Re-reading your question, I put together the completed example. In your target page, you have two DIVs and you wish to display one or the other depending on what form the user clicked.
Here is a working example of the solution. Copy/Paste into two files called:
test.php -- this file can be renamed whatever you want
product.php -- if change this name, must also change name in both action= attributes of forms
test.php
<form action="product.php" method="get"> <!-- product.html -->
<fieldset>
<input type="hidden" name="RSS" id="RSS" value="RSS" />
<input type="submit" value="Go"/>
</fieldset>
</form>
<form action="product.php" method="get">
<fieldset>
<input type="hidden" name="RSS2" id="RSS2" value="RSS2" />
<input type="submit" value="Go"/>
</fieldset>
</form>
product.php
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var whichone = $('#xfer').val();
//alert( whichone );
$('#' + whichone).show();
});
</script>
</head>
<body>
<input type="hidden" id="xfer" value="<?php echo ( isset($_REQUEST['RSS']) ? 'RSS' : 'RSS2' ) ; ?>">
<div id="RSS" style="display:none;">
<h1>RSS DIV</h1>
Here is some information regarding the RSS div.
</div><!-- #RSS -->
<div id="RSS2" style="display:none;">
<h1>RSS2 DIV</h1>
<i>Here is some <strong>different </strong>information regarding the RSS2 div.</i>
</div><!-- #RSS -->
</body>
</html>
You can test your request with javascript:
if(location.href.indexOf("RSS=RSS") > 0) {
var element = document.getElementById('RSS')
element.id = "RSS2";
element.name = "RSS2";
element.value = "RSS2";
}
this is just an indexOf check, u could also parse the whole query string and associate the key/value pairs into an array. See How can I get query string values in JavaScript?

Send value with Onchange and javascript function

I'm new at JavaScript and I need some help to submit my form when anyone select anything from the dropdown list. Here is the sample code :
<form action="" method="post">
<select name="car" onChange="this.form.submit()">
<option value="car1">car1</option>
<option value="car1">car1</option>
<option value="car1">car1</option>
</select>
<!-- Some other code -->
<input type="submit" name="sentvalue">
</form>
This is my form and when anyone selects the dropdown, the form automatic submit but I also need the sentvalue when automatic submit the form. So can anyone help me to capture the sentvalue when anyone select the dropdown.
If you want to send any other data with the form submit you can send it in hidden inputs
<input type='hidden' name='data1' value='Test1' />
<input type='hidden' name='data2' value='Test2' />
//etc
And if you want the value of submit button with the form just set the value attribute into your code for submit button every this else seems fine,
<input type="submit" name="sentvalue" value="Test1" />
Hope this answers your question
Just give the <input> tag a name
<input name="submitbtn" type="submit" value="sentvalue" />
then you can get it by $_POST['submitbtn'] in case of PHP which value is "sentValue"
It sounds like you want the value of the input field (sentvalue) to be submitted as well. But how do you guarantee that the value was specified by the user? Is this supposed to be a hidden input field?
Either way your input tag is incomplete. This sounds better:
<input type="text" name="sentvalue"></input>
Also, when you submit, the value of this field (sentvalue) will be passed in too. As long as your tags are right you don't need to worry.
You will have to create a hidden input element
<input type='hidden' id='sentvalue' value='' />
and call a function on submit instead of directly submitting
Use this in the function to set the values and submit
var e = document.getElementByName("car");
document.getElementById("sentvalue").value = e.options[e.selectedIndex].value;
this.form.submit();

Pass parameter from a text field contained in another form

I have two forms in a JSP page, one contains a text field. I need to make sure that whenever the other form is submitted, it copies the value contained in that text field, and writes it's text value in an hidden parameter.
More clearly:
One form is named "search";
Another form is named "changeInitialForm";
The text field is contained in the "search" form, and it's name is "searchString";
The "changeInitialForm" has an hidden field, also this named "searchString";
The purpose is to make sure that whether the user submits one or another form, a "searchString" parameter is passed, with the value of the text field.
I tried to include an action in javascript, executed when the "changeInitialForm" is submitted, that reads the text field value and writes it into the hidden parameter:
function searchContacts(form)
{
var searchString= document.search.searchString.value;
form.searchString.value= searchString;
}
...
<form name="search" >
<input type="text" name="searchString">
<button name="search"> Cerca </button>
</form>
...
<form name="changeInitialForm" method="post" action="AddressBookView.jsp" onSubmit="searchContacts(this.form);">
<input type="hidden" name="selectedInitial"/>
<input type="hidden" name="status" value="view"/>
<input type="hidden" name="searchString" />
</form>
But after the "changeInitialForm" is submitted, regardless of the text field value, and empty parameter is passed, I am seeing this with firebug:
I would also appreciate an alternative solution, because I know what I am doing is tricky, but I don't find another method to do that. "search" and "changeInitialForm" cannot be joined in a single form, because they do very different things.
The following seems to work
function searchContacts(form)
{
var searchString= document.search.searchString.value;
form.searchString.value= searchString;
}
...
Form 1:
<form name="search" >
<input type="text" name="searchString">
<button name="search"> Cerca </button>
</form>
...
Form 2:
<form name="changeInitialForm" method="post" action="AddressBookView.jsp" onSubmit="searchContacts(this);">
<input type="hidden" name="selectedInitial"/>
<input type="hidden" name="status" value="view"/>
<input type="hidden" name="searchString" />
</form>
Notice that searchContacts(this.form) was replaced with searchContacts(this).
UPDATE after some precisions by the author of the question:
The onsubmit event is not triggered when form.submit() is called by some javascript code. Thus, what you need when you submit the form is to call searchContacts separately, for example using
searchContacts(document.changeInitialForm);
document.changeInitialForm.submit();

Categories