Issue checking value of form input with jQuery - php

I want to perform an if statement based upon the value of a hidden input within myform.
the scenario is that I have a table consisting of tests. Each test is essentially its own form. the hidden input basically contains 'YES' if the test has a password attached. I want to prompt the user to enter the password if that is the case when they attempt to execute a particular test (submit the form).
so here is my code I am attempting:
$("#submit").live('click', function(event) {
if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'YES') {
$('.messagepop').remove();
$(this).parent().append('<div class="messagepop pop"><p><label for="password">Enter test password</label><input type="text" size="30" name="pass" id="pass" /></p><p><input type="button" id="submitPass" value="GO" id="pass_submit"/> or <a class="close" href="/">Cancel</a></p></div>');
$(".pop").slideFadeToggle(function() {
$("#pass").focus();
});
return false;
}
});
Without the if statement the code works: on clicking a particular button within a form, another small password entry form pops up. However I only want the form to pop up if a value of a hidden input is 'YES'.
I have checked that values are present in the hidden input.
I imagine I am using the jQuery slightly wrong as I am a beginner. So can somebody identify a better way of performing the check?
Many thanks,
EDIT (jsfiddle with html etc):
jsfiddle

Your HTML is extremely malformed. The <tr> and <form> end-tags are mismatched, you've got a <form> tag directly inside the <tr>, which is not allowed, you haven't self-closed your <input /> tags and you have duplicate id's on the same page.
<tr>
<form name='myform' method='post' action='test_sim.php'>
<input name='test' type='hidden' value='1'>
<input type='hidden' name='has_password' value='NO'>
<td>TEST 1</td>
<td>B352</td>
<td>10</td>
<td>2011-05-12 06:00:00</td>
<td>2011-05-12 12:00:00</td>
<td>06:00:00</td>
<td><input id='submit' type='button' value='execute test'></td>
</tr>
</form>
It should be like this:
<tr>
<td>TEST 1</td>
<td>B352</td>
<td>10</td>
<td>2011-05-12 06:00:00</td>
<td>2011-05-12 12:00:00</td>
<td>06:00:00</td>
<td>
<form name='myform' method='post' action='test_sim.php'>
<input name='test' type='hidden' value='1' />
<input type='hidden' name='has_password' value='NO' />
<input class='submit' type='button' value='execute test' />
</form>
</td>
</tr>
Fixing the HTML solves your problem: http://jsfiddle.net/brianpeiris/ezFgU/16/
You should use the W3C Validator tool to check your HTML so that you can prevent these types of issues.
Edit: As mcgrailm mentioned, your id attributes are duplicated. jQuery is forgiving here, it seems, that's why the above jsFiddle works. You should use a class to identify your submit buttons and use the corresponding $('.submit') selector to attach a click handler.

i believe
if ($(this).parent().find(':input:hidden[name=has_password]').val() == 'YES')
should be
if ($(this).parent().find('input[name=has_password]:hidden').val() == 'YES')

i think your selector is incorrect try this
$("#submit").live('click', function(event) {
if ($(this).parent().find('input[name="has_password"]:hidden').val() == 'YES') {
$('.messagepop').remove();
$(this).parent().append('<div class="messagepop pop"><p><label for="password">Enter test password</label><input type="text" size="30" name="pass" id="pass" /></p><p><input type="button" id="submitPass" value="GO" id="pass_submit"/> or <a class="close" href="/">Cancel</a></p></div>');
$(".pop").slideFadeToggle(function() {
$("#pass").focus();
});
return false;
}
});
WORKING DEMO

Related

Reset button not working in html (php)

I have placed my input type reset button inside form but its still not working.
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"><?php echo $rows["news"] ; ?></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /></td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /></td>
</tr>
</form>
I have also tried <button type="reset" value="Reset">Reset</button>
If you're expecting the reset button to empty out the form, that's not the way reset works. It will reset the form fields to what they were when the page was loaded. In this case, you have pre-filled content in your form, so clicking reset will simply undo any user changes since the page-load.
To reset all form fields to blank/nothing, you will need javascript. First get the form DOM object, then iterate over all the input types you want to reset, and set each field.value = '' (input text types / textarea), or modify attributes (for select, radio, checkbox etc.) to deselect and so on. Turn that into a function (there's probably a ready piece somewhere out there, I seem to have lost mine), and attach it to your reset button's click event -- or your form's reset event.
Edit: On a quick search, here's a basic example of how to clear a form to empty values.
Edit: If you're not concerned over anything but text fields, here's a simple way to do this. We add a "reset" event listener to the form, and when it fires, all fields are set to empty string.
function setFormCleanReset(formId) {
let formEl = document.querySelector(formId);
// Add event listener for reset event
formEl.addEventListener('reset', function(e) {
// Iterate all non-hidden fields, set values to ''
for(const fieldEl of formEl.querySelectorAll('input:not([type=hidden])')) {
// #todo check input type and handle "select" etc.
fieldEl.setAttribute('value', '');
}
});
}
// usage: setFormCleanReset('my_form_id');
I think you forgot to insert form tag in your html. It should be work if you insert button code into <form>..button code here..</form>.Something like this:
<form>
<input type="text">
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</form>
You should place the <input type="reset"> in a form like this,it will definitely work
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Tring Reset</title>
<style>
</style>
</head>
<body>
<form>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type="text"></input>
<input type=reset></input>
</form>
</body>
</html>
You just need to put your reset button inside form. In following code I have taken one input box also an an example. I hope it helps.
<form>
<input type="text" />
<table>
<td class="col-md-4">
<input name='reset' type="reset" value='Reset' class="btn btn-primary" />
</td>
</table>
</form>
can you please try without echo anything inside textarea
try this
<form method='post' action='process/update_news_action.php' >
<tr>
<td colspan='2' class="col-md-4">Update News Content</td>
</tr>
<tr >
<td colspan='2' class="col-md-8"><textarea name="news" id="text"></textarea></td>
</tr>
<tr>
<td class="col-md-4"><input name='submit' type="submit" value='Publish' class="btn btn-success" /> </td>
<td class="col-md-4"><input name='reset' type="reset" value='Reset' class="btn btn-primary" /> </td>
</tr>
</form>
I think reset button is working fine.
Excuse Me! You are using default value in input attribute. Remove default value from it. Then type anything then click rest it will make your text box empty.
I have a form submitting variables to another PHP file. In the second file, there is a button that leads back to the first file for eventual correction od submitted values. The values are retained in the form by $_POST. The "Reset" button works only when the form is used the first time (empty). After submitting and return from the second PHP file, the "Reset" doesn't do anything, no values are cleared. The button seems to be fully inactive.
<button title="<?php echo $lang['reset-button-hint']; ?>" alt="TOOLTIP 1" type="reset" name="reset"><?php echo $lang['reset-button']; ?></button>
#Jaspreet Kaur You need to add form tag outside the table. Please check my previous comment for it.

Regular button and submit button in one form don't work

I have a form returned back from a PHP request. The form has a submit button and a regular button. The submit seems to work just fine, but the regular one won't work! Please advise.
Here is my HTML:
<tr id='actionRow' name='actionRow' hidden='hidden'>
<td>
<input class='actionBtn' type='submit' id='confirm' name='confirm'
value='Confirm' onclick='genChanged();'/>
</td>
<td>
<input class='actionBtn' type='button' id='cancel' name='cancel'
value='Cancel' onclick='cancel();' />
</td>
</tr>
And here is the cancel() function:
function cancel(){
alert('in cancel');
document.getElementById('editRow').hidden = false;
document.getElementById('actionRow').hidden = true;
window.location.replace("admin.php");
};
The alert never appears!
Is it even right to put multiple non-submit buttons in one form?
UPDATE
my form looks something like:
<form action='save.php' method='POST' id='myForm'>
I've added the line document.getElementById('myForm').submit(); to the getChange(); function, to make the button be:
<input class='actionBtn' type='button' id='confirm' name='confirm'
value='Confirm' onclick='genChanged();'/>
Yet, cancel(); function still doesn't work!
You can't use the same name for the id and the function name.
In my sample 'a' have the same id and function name test() and b have different id and function name cancel()... cancel work fine and test don't.
<script>
function cancel(){
alert('in cancel');
};
function test(){
alert("in test")
};
</script>
<body>
<form action='save.php' method='POST' id='myForm'>
<tr id='actionRow' name='actionRow' hidden='hidden'>
<td>
<input class='actionBtn' type='button' id='test' name='a'
value='a' onclick='test();'/>
</td>
<td>
<input class='actionBtn' type='button' id='b' name='b'
value='b' onclick='cancel()' />
</td>
</tr>
</form>
</body>
For more informations you can see this helpful answer : https://stackoverflow.com/a/9160009/1318727

Submitting form issues: TypeError: this.form is null

I have a php page that displays all of the service logs for a given site in a table.
At the end of the table I have a new row that has an "Add new service log" button.
Clicking the button hides that row and displays a new row with a form and the desired inputs as well as a new row with buttons "Save" and "Cancel".
The "Save" button calls a Javascript function that checks that the key input has been filled out. When hitting "Save", I see in the console log
TypeError: this.form is null
Here is my code:
// SQL Query
// Table data of queried results
// Add a new service log
echo "<tr id='AddNew'>
<td><input type=button value='Add New' onclick=\"$('#AddNew').hide();$('#AddNewHid').show();$('#SaveCancel').show();\"></td>
<td colspan=12> &nbsp </td></tr>
<form name='AddNewLog' method='POST' action='servicelook.php' id='AddNewService'>
<tr style='display: none;' id='AddNewHid'>
<td><input type='text' value='$lastID' name='ticketid' size=10></td>
<td><input type='text' value='$siteID' name='SiteID' size=4></td>
<td><input type='text' value='$siteName' name='SiteName' size=20></td>
<td><input type='text' value='$userid' name='takenBy' size=4></td>
<td><input type='text' value='$now' name='callTime' size=20></td>
<td><input type='text' value='' name='caller' size=20></td>
<td><input type='text' value='' name='callPhone' size=14></td>
<td><textarea name='issue' value = '' rows=3 cols=10></textarea></td>
<td><textarea name='fix' value = '' rows=3 cols=10></textarea></td>
<td><input type='text' value='$userid' name='solvedBy' size=4></td>
<td><input type='text' value='$now' name='solvedTime' size=20></td>
<td style='min-width:100px;max-width:100px;'><input type='checkbox' name='fup' value='fup'>Follow Up</td></tr>
<input type='hidden' value='Yes' name='AddNew'>
<input type='hidden' value='$userid' name='userid'>
<input type='hidden' value='$session' name='session'>
<input type='hidden' value='$siteid' name='siteid'>
<tr style='display: none;' id='SaveCancel'>
<td colspan=2>
<input name='save' type='button' onclick='inputCheck(this.form.issue);' value='Save'> &nbsp
<input type='button' value='Cancel' onclick=\"$('#AddNew').show();$('#AddNewHid').hide();$('#SaveCancel').hide();\"></td>
<td colspan=11> &nbsp </td>
</tr></form>";
echo "</table>";
My inputCheck function looks like this:
<script type="text/javascript">
function inputCheck(areaName)
{
console.log("checking...");
if (areaName.value.length > 0)
{
$('form#AddNewService').submit();
}
else
{
alert("You didn't describe the problem!");
}
} // end noteCheck function
</script>
I tried changing the submit button to this:
<input type=submit value=Save>
but nothing happens, nothing on the page or in the log.
Anyone have any idea as to what I am doing wrong or what the problem is here?
Thanks in advance!
EDIT:
After using Steven's advice, I was able to successfully submit the form.
However, the inputs aren't properly being sent. I am submitting to the same page so that the table reloads and and the user can see their latest entry. At the beginning of the script I have this:
if($AddNew == 'Yes')
{
echo "YES...Adding New";
echo $ticketid .$SiteID. $SiteName. $takenBy. $callTime. $caller. $callPhone. $issue. $fix. $solvedBy. $solvedTime;
// Start SQL Query
}
The inputs return null. Is there something wrong with the way I am setting my inputs?
The general strategy here is to make the save button a submit button, then put the validation on the form's submit handler and cancel submit if validation fails, e.g.
<form onsubmit="return inputCheck(this.issue);" ...>
then if validation fails, return false from the inputCheck function.
Your issue is probably because you have a form in a place it can't be, so it's being moved outside the table, i.e. you have:
<table>
<form>
...
</form>
</table>
which the browser is "correcting" to:
<table>
...
</table>
<form>
</form>
Now the form is outside the table but the controls are still inside it. So change the markup to:
<form>
<table>
...
</table>
</form>
so the table and form controls stay inside the form.
when this.form is null:
onclick='inputCheck(this.form.issue);'
replace with:
onclick='inputCheck();'
and edit your inputCheck method:
function inputCheck() {
console.log("checking...");
if ($('textarea[name=issue]').val().length > 0){
$('form#AddNewService').submit();
} else {
alert("You didn't describe the problem!");
}
}

href hyperlinks as POST

I'm wondering which is the best way to map <a href=...></a> hyperlinks performing a HTTP GET to perform a HTTP POST (I'd like to avoid passing all variables in the URL)? For instance, the 2 hrefs below. Furthermore, I would like also to get rid of the submit button and use a regular <a href=...></a> hyperlink. Any suggestions?
<form action="test.php?action=update" method="post" id="cart">
<table>
<tr>
<td>
<a href="test.php?action=delete&id=<?php echo $id ?>" class="r">
remove
</a>
</td>
<td>
add
</td>
</tr>
<tr>
...
</tr>
</table>
<div> <button type="submit">Update</button> </div>
</form>
I'd suggest using jQuery.post and click. When the link is clicked, submit the data via something like this
$('.r').on('click', function() {
$.post('test.php', {key:'value', key2:'value2'}, function(data) {
//error check here
});
return false;
});
As far as i know you can't perform any POST requests with links. You can make your GET requests with links and php as a fallback for users with javascript disabled and for those who have javascript enabled cancel default behavior for links with javascript and make with ajax your POST request with help of AJAX.
for example:
<a class="submit" href="hallo.html?hello=world&test=yes">Test</a>
and js(i used jquery) would be:
$('.submit').click(function(){
var urlPairs = this.href.split('?')[1].split('&'),
total = urlPairs.length,
current = [],
data = {};
for(;total;) {
current = urlPairs[--total].split('=');
data[current[0]] = current[1];
}
$.post('test.php', data);
return false;
});
​
you could also write your POST function other for more info check jQuery post function doc
P.S. I wounder, if you are using FORM any way why wouldn't you use submit button, is it because of CSS styling? You could style it the same way like any other element(with some tweaks in some browsers).
You can keep the input tags as hidden in your form to submit them as POST.
<td>remove</td>
<td>add</td>
Rewrite the above using two forms, having <input type="hidden" name="id />
and buttons <input type="button" name="delete" />
etc..
if you run print_r($_POST) in your action script. You'll get a list of the buttons and the values of the hidden fields. Now, you can write conditions for the actions to run. I didn't write complete code here, just passing the idea.
Edit: See the example below.
<form method="post" id="cart">
<table>
<input type="hidden" name="id" value='some value' />
<input type="submit" name="action" value="delete" />
<input type="submit" name="action" value="add" />
</tr>
<tr>
...
</tr>
</table>
<div><button type="submit">Update</button></div>
</form>
<?php print_r($_POST); ?>

i have problem in javascript email validation

i have problem in javascript email validation,
I wrote code something like,
//emp.php
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2" >
<table>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Desired Email-ID:</td>
<td><input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
</tr>
</table>
</form>
//java scrit code for checkMail()
<script language="javascript">
function checkMail() {
var email=document.getElementById('emp_email').value;
var mail = email.value;
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
if(!reg.test(mail) || mail == "")
{
alert("Your email address isn't valid!");
return false;
}
else {
alert("Email address is okay, let's send the form!");
}
}
</script>
Plz help me thanks in advance
The problem is that there is no element with id
emp_email.
Your element has name emp_email. Add an id attribute to the text box.
<input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/>
change it to
<input type="text" name="emp_email" id="emp_email" value="" size="32" onsubmit="checkMail()";/>
Edit:
To put javascript validation on submit button change
<input type="submit" value="Insert record" onclick="checkMail();"/>
to
<input type="submit" value="Insert record" onClick="return checkMail();" />
and remove the onsubmit event from the email textbox.
phoenix has the basic error right. But:
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
Don't put form validation on a submit onclick. It may be possible to submit the form without clicking the submit button (depending on the browser and number of fields and buttons in the form). Always put it on the form itself:
<form ... onsubmit="return checkMail();">
also:
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]­{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
That's a really bad idea, like most ad-hoc “e-mail validation” regexes it will reject many completely valid e-mail addresses. Your rules for what usernames and TLDs are allowed are particularly, pointlessly, restrictive.
See this infamous regex for how to actually validate e-mail addresses. Then cry, give up, and just do basic checks instead. (Is there an ‘#’ in it? is there a ‘.’ in it? are there no spaces? well fine then.)
(Even the page-long-horror-regex can't cope with non-ASCII e-email addresses via IDN, which it would be nice to support.)
Try moving the javascript function call off the submit button and onto an onblur event on the email field itself.

Categories