I have a script which is coded in PHP and smarty and I want to make some changes in this script like in user profile setting when a user click on profile setting and fill the profile data like (full name , email , bank account number , bank name , branch code , tile etc) after fill this information and click the save button the script save all setting but I want to make some of form input fields (readonly) after user submitted his/her data I want make this data (readonly) (bank name , account number , branch code , title) I mean about readonly that after user fill this and save it in profile its show to user (readonly) user can't change it only I can change it from database .
Below is the form code
<form id="settingsform" onsubmit="return submitform(this.id);">
<input type="hidden" name="a" value="submit" />
Bank Name:
</td>
<td><input type="text" name="bank" id="bank" value="{$user_info.bank}"></td>
</tr>
<tr>
<td align="right" width="50%">
Bank Account Number:
</td>
<td><input type="text" name="baccount" id="baccount" value="{$user_info.baccount}"></td>
</tr>
<tr>
<td align="right" width="50%">
Bank Account Title:
</td>
<td><input type="text" name="btitle" id="btitle" value="{$user_info.btitle}"></td>
</tr>
<tr>
<td align="right" width="50%">
Bank Branch Code:
</td>
<td><input type="text" name="bcode" id="bcode" value="{$user_info.bcode}"></td>
</tr>
<input type="submit" name="btn" value="{$lang.txt.send}" class="orange" />
also i am already trying like this below
<script>
function hide(){
document.getElementById("bank").disabled = true;
document.getElementById("baccount").disabled = true;
document.getElementById("btitle").disabled = true;
document.getElementById("bcode").disabled = true;
}
</script>
and
<input type="button" onClick="hide()" value="save">
<input type="text" name="country" value="Norway" readonly>
http://www.w3schools.com/tags/att_input_readonly.asp
Then simply do a check if the POST/GET is set for the field you want to have readonly on ->
<input type="text" name="country" value="Norway" <?php isset($_POST['country']) ? echo 'readonly' : ''; ?> >
UPDATE
<input type="text" name="country" value="Norway" <?php isset($user_info.bank) ? echo 'readonly' : ''; ?> >
Just replace the variable inside of the isset() with the variable of the field, depending on how you initialize the variables you might need to use
empty()
instead, to do so just simply replace isset() with !empty()
The reason for the ! sign is to tell the code to execute the first bit (the echo part) if the variable ISN'T empty
UPDATE 2
<input type="text" name="country" value="Norway" {if isset($user_info.bank) } readonly {/if} >
Above code should work with smarty how ever, the idea is simple, simply have a "if" sentence that checks whether the variable is set/have data, and if it does, then echo 'readonly' in the input field
Try to use attr() in submitform() like,
function submitform(id){
....
....
$('#bank,#baccount').attr('readonly','readonly');
return false;
}
Updated,
function hide(){
$("#bank,#baccount,#btitle,#bcode").prop('disabled', true);
}
Or try,
function hide(){
$("#bank,#baccount,#btitle,#bcode").attr('disabled', 'disabled');
}
Related
This question already has answers here:
Multiple inputs with same name through POST in php
(5 answers)
Form input field names containing square brackets like field[index]
(3 answers)
Closed 2 years ago.
I am trying to update a restaurant menu via form. firstly I take all contents of the menu category and put them via foreach in a input value so they are all editable and pre setup for editing.
now the big problem is whenever I completely try to edit the form, it only edits the last row because that's the last received and thus ignoring the rest. How would I make the form submit all requested items from the foreach?
making a form for each item would be a big no no because then there would be an button for each row which hinders the productivity
Blade view:
<form action="test" method="head">
#foreach ($Voorgerecht as $item)
#csrf
<tr>
<td>
<input type="text" name="id" value="{{$item->id}}">
</td>
<td>
<input type="text" name="name" value="{{$item->product_name}}">
</td>
<td>
<input type="text" name="price" value="{{$item->product_price}}">
</td>
<td>
<input type="submit" value="edit">
</td>
#endforeach
</tr>
</form>
Controller:
public function editsubmit(Request $req) {
update::where('id',$req->id)
->update(['product_name'=>$req->name]);
return redirect('/admin');
}
If you really want to do this all in one go you can name the inputs in array form as a container for all the records, something like records[1][product_name]:
<form ...>
#csrf
#foreach ($Voorgerecht as $item)
<tr>
...
<td>
<input type="text" name="records[{{ $item->id }}][product_name]" value="...">
</td>
<td>
<input type="text" name="records[{{ $item->id }}][product_price]" value="{{ $item->product_price }}">
</td>
...
</tr>
#endforeach
</form>
Then in the Controller
... do validation ...
foreach ($request->input('records', []) as $id => $record) {
Model::findOrFail($id)->update($record);
}
You can change the form fields so they are posted as an array:
<input type="text" name="id[{{ $loop->index }}]" value="{{$item->id}}">
i added the $loop->index so all fields are combined.
Here i am linking the question and it is the continuation of that question LINK IS HERE
Actually i have made that work by changing the script like this
my javascript
<script type="text/javascript">
$(document).ready(function()
{ var ac_config = { source: "fill_patient_info.php", select: function(event, ui){ $("#p_name").val(ui.item.p_name); $("#p_dob").val(ui.item.p_dob) }, minLength:1 }; $("#p_name").autocomplete(ac_config); });
</script>
and in fill_patient_info1.php
$p_name = $_GET['p_name'];
$result = mysql_query("SELECT patient_id, patient_name, dob from patient WHERE patient_name LIKE '".$p_name."%'");
$myrow = mysql_fetch_array($result);
if(mysql_num_rows($result))
{
$p_name = $myrow["patient_name"];
$p_dob = $myrow["dob"];
$textout = $p_name.','.$p_dob;
echo $textout;
}else
{
echo "Sorry This Patient Does Not Exist";
}
It is not working properly. If i have more than 1 name with the same letter, i cannot type in the 1st textbox. As soon as i enter the 1st letter it just fills up both the name and dob with the 1st patient name starts with that letter. after that i cannot delete and change that too.
Here is my input box for patient name and dob, which should be filled automatically(form)
<form name="consult" method="post" action="consult_submit.php" enctype="multipart/form-data" onsubmit="return validate()">
<table class="selection">
<tr><td>Patient Name :</td><td><input type="text" name="p_name" id="p_name" onkeyup="return filldetail()" /></td></tr>
<tr><td>DOB :</td><td><input type="text" name="p_dob" id="p_dob" class="tcal" /></td></tr>
please suggest how to correct it
Instead of onkeyup you need to use onblur. If you use onkeyup event, the javascript function is called whenever you complete entering a charecter with keyboard. If you use onblur, the function will be called when you finish entering the text and change focus from the text field.
<form name="consult" method="post" action="consult_submit.php" enctype="multipart/form-data" onsubmit="return validate()">
<table class="selection">
<tr><td>Patient Name :</td><td><input type="text" name="p_name" id="p_name" onblur="return filldetail()" /></td></tr>
<tr><td>DOB :</td><td><input type="text" name="p_dob" id="p_dob" class="tcal" /></td></tr>
Hi i have a cart that i want to add a form automatically this code seems to kinda do the trick only the problem is i have to hit F5 for it to add the amount i am quite new to this and cant figure out where i have gone wrong.
<form method="post" action="level3.php" class="jcart" id="foo">
<fieldset>
<input type="hidden" name="jcartToken" value="<?php echo $_SESSION['jcartToken'];?>" />
<input type="hidden" name="my-item-id" value="ABC-8" />
<input type="hidden" name="my-item-name" value="Level 1" />
<input type="hidden" name="my-item-price" value="95.00" />
<input type="hidden" name="my-item-url" value="" />
<table>
<tr>
<td width="65%">
<strong>Level 1 all for £95</strong>
</td>
<td width="15%" align="right">
£95.00
</td>
<td>
<input type="text" hidden ="true" name="my-item-qty" value="1" size="3" hidden="true" />
</td>
<td width="20%" align="center">
<input type="submit" name="my-add-button" id="my-add-button" value="add" class="button" />
</td>
</tr>
</table>
<input type="hidden" name="visited" value="" />
</fieldset>
That is the form that submits the amount into the check out.
<script>
$(document).ready(function(){
//Check if the current URL contains '#'
if(document.URL.indexOf("#")==-1)
{
url = document.URL+"#";
location = "#";
} else {
location.reload(true);
document.getElementById("my-add-button").click();// Simulates button click this has to be in as it links to another piece of java that adds the item to the check out with out this it wont add the item
document.foo.submit(); // Submits the form without the button
}
});
</script>
document.getElementById("my-add-button").click();
The code above links to the code below that adds the items from to my knowledge
$('.jcart').submit(function(e) {
add($(this));
e.preventDefault();
});
Thank you in advance for any help or suggestions
If i understand correctly, you increase some value of cart in PHP and than want to update quantity on page without refreshing page.
I made little JSFiddle: http://jsfiddle.net/7SbBA/
Basically use ajax:
$.ajax({
url: 'example.com',
data: /* pass product id here */
success: function(){
/* update quantity you want */
}
});
There is some html change too.
UPDATE
If you want updated total values on page load, just £<span class='amount'><?php echo $product['quantity'] * $product['price'] ?></span> and there is no need to use jquery/js for that. But if you still want dynamically update on window load, i updated my JSFiddle v.2
I want this script to disable date fields when the page loads, then enable/disable depending on the drop down field. The script simply isnt doing anything, was wondering if anyone can see what I am doing wrong.
<head>
function formdisable(){
var myTextField = document.getElementById('searchby');
if(myTextField.value = "agencyname")
document.searchform.Date1.disabled=true
document.searchform.Date2.disabled=true
document.searchform.agencyname.disabled=false
else
document.searchform.Date1.disabled=false
document.searchform.Date2.disabled=false
document.searchform.agencyname.disabled=true
}
onload = start;
</script>
</head>
<table>
<form id="searchform" name="searchform">
<tr>
<td>Search Term: </td><td><input tabindex="1" type="text" name="search" id="search1" value="" /></td>
</tr>
<tr><td>Search By:</td><td><select size="1" id="searchby" name="searchby" onclick="formdisable()" tabindex="2">
<option value="agencyname">Agency Name</option>
<option value="daterange">Date Range</option>
</select></td></tr>
<tr>
<td>Beginning Date: </td><td><input id="Date1" name="Date1" size="10" maxlength="10" value="<?php echo $_GET['Date1']?>"/> </td><td>Ending Date: </td><td><input id="Date2" name="Date2" size="10" maxlength="10" value="<?php echo $_GET['Date2'];?>" /> </td></tr>
<input type="hidden" id="wildcard" value="= 'Approved'" />
<input type="hidden" id="repid" value="<?php echo $_SESSION['REPID']; ?>" />
<tr><td><input tabindex="3" type='button' onclick='searchAgency()' name="searchsub" value='Search' /></td></tr></td>
</tr>
</form>
</table>
Right away, I see missing {} in your if/else, and an assignment = where an equality == belongs:
if(myTextField.value = "agencyname")
document.searchform.Date1.disabled=true
document.searchform.Date2.disabled=true
document.searchform.agencyname.disabled=false
else
document.searchform.Date1.disabled=false
document.searchform.Date2.disabled=false
document.searchform.agencyname.disabled=true
Should probably be:
<script type='text/javascript>
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Forgot the opening script tag
Then...
if(myTextField.value == "agencyname") {
//-------------^^^^^^^
// ==, not =
document.searchform.Date1.disabled=true;
// Please manually insert semicolons--^^^
document.searchform.Date2.disabled=true;
document.searchform.agencyname.disabled=false;
}
else {
document.searchform.Date1.disabled=false;
document.searchform.Date2.disabled=false;
document.searchform.agencyname.disabled=true;
}
In your original version, you were assigning agencyname to myTextField.value inside the if() condition. Followed by that was a mess of partially executed code. The assignment would succeed, and then the first (and only the first) subsequent statement would execute as part of the if() block. The next two would execute before coming to a syntax error on the else.
You are relying on automatic semicolon insertion, which is a dangerous (mis-)feature of the JavaScript language when used recklessly. Please always terminate your statements with ;
You are missing 6 ; two } and one { also you need to use == instead of =.
You should read a getting started tutorial.
I got to agree with Michael but you also missed opening tag
The editvalidate() function is not getting called at all:
Please suggest why. What's the remedy?
<script type="text/javascript">
function editvalidate() {
var emailExp = /^[\w\-\.\+]+\#[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
var numericExpression = /^[0-9]+$/;
if(document.editprofile.userid.value == '' || document.editprofile.password.value == ''||document.editprofile.name.value == ''||document.editprofile.age.value == ''||document.editprofile.collegeid.value == ''||document.editprofile.mobile.value == ''||document.editprofile.address.value == ''||document.editprofile.department.value == ''||document.editprofile.email.value == ''||document.editprofile.sec_ques.value == ''||document.editprofile.answer.value == ''){
alert("Hey! you can't left a field blank!");
return false;
}
else if(!document.editprofile.email.value.match(emailExp)){
alert("You need to enter a valid email address to get proper notifications!");
return false;
} else if(!document.editprofile.mobile.value.match(numericExpression)){
alert("Mobile numbers are all numeric digits i think!");
return false;
} else if(document.editprofile.mobile.value.length < 10){
alert("Mobile number must be 10 digit long!");
return false;
}
else{
return true;
}
}
</script>
the form is given below and its used to fetch data from database and itself getfilled with the values.the editable entries are corrected and the form is submitted.its working fine just not getting validated cz the editvalidate() is not getting called at all.why?
<form name="editprofile" action="editprofile.jsp" method="post" onsubmit="return editvalidate();">
<table align="center">
<%
for(int i = 0; i < list.length ; i++){
%>
<tr>
<td>Name:</td>
<td><input type="text" name="name" size="35" style="width: 219px" value="<%=list[i].getName() %>" maxlength="25"></td>
</tr>
<input type="hidden" name="userid" size="20" style="width: 220px"
value="<%=list[i].getUserid() %>" maxlength="10">
<tr>
<td>Address:</td>
<td><input type="text" name="address" size="46"
style="width: 221px" value="<%=list[i].getAddress() %>"
maxlength="50"></td>
</tr>
<tr>
<td>E-mail:</td>
<td><input type="text" name="email" size="20"
style="width: 220px" value="<%=list[i].getEmail() %>" maxlength="40"></td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="age" size="20" style="width: 219px"
value="<%=list[i].getAge() %>" maxlength="2"></td>
</tr>
<tr>
<td>College ID:</td>
<td><input type="text" name="collegeid" size="20"
style="width: 219px" value="<%=list[i].getCollegeid() %>"
maxlength="10"></td>
</tr>
<tr>
<td>Mobile:</td>
<td><input type="text" name="mobile" size="20"
style="width: 218px" value="<%=list[i].getMobile() %>" maxlength="10"></td>
</tr>
<tr>
<td>Department:</td>
<td><input type="text" name="department" size="20"
style="width: 218px" value="<%=list[i].getDepartment() %>"
maxlength="10"></td>
</tr>
<tr>
<td>Security Question:</td>
<td><input type="text" name="sec_ques" size="20"
style="width: 218px" value="<%=list[i].getSec_ques() %>"
maxlength="50"></td>
</tr>
<tr>
<td>Answer:</td>
<td><input type="text" name="answer" size="20"
style="width: 218px" value="<%=list[i].getAnswer() %>" maxlength="50"></td>
</tr>
<tr>
<td><input type="submit" name="operation" value="editprofile"
style="width: 118px"></td>
<td><input type="reset" value="Reset" name="B2"></td>
</tr>
<%
}
%>
i checked it the way u suggested and found that the function is getting called.but why the rest alerts r nt visible? isnt thatdue to the value attribute in input tags
With the as far given little information, all I can answer is: Just run a Javascript debugger. I can recommend you Firebug for this.
That said, in the future please come up with an SSCCE instead of cutouts of code with unnecessary clutter. This avoids crawling long in the code searching for lines of relevance and at first glance obvious questions as "Are they in the same file?", "Did you add alert('blah') as 1st line of function to see if it actually is invoked?", "Did the browser have JS enables?", "Are you sure that you didn't typo'ed the function name?", "Isn't there more into the code which may have disturbed it?", etcetera.
Here's a basic example of such an SSCCE:
<!doctype html>
<html lang="en">
<head>
<title>SO question 2063598</title>
<script>
function validate(form) {
alert('Validate method invoked!');
return false;
}
</script>
</head>
<body>
<form onsubmit="return validate(this)">
<input type="text" name="foo" class="required">
<input type="submit">
</form>
</body>
</html>
That said, I see that you're still using the legacy scriptlets in your JSP. If you can, I strongly recommend to stop using it and switch to taglibs/EL before it's too late. Raw Java code belongs in Java classes, not in JSP files.
Basic example with JSTL's (just drop jstl-1.2.jar in /WEB-INF) c:forEach:
<table>
<c:forEach items="${users}" var="user">
<tr>
<td><input type="text" name="address" value="${user.address}"></td>
<td><input type="text" name="email" value="${user.email}"></td>
<td><input type="text" name="college" value="${user.college}"></td>
</tr>
</c:forEach>
</table>
And also keep CSS in its own CSS file to separate style from content and to increase webapp performance and maintainability.
you don't call it anywhere from your html, you should probably call it <form onsubmit="return editvalidate();">
Ok now I see he formatted it properly, I don't mind the down votes I give them sometimes as well.
#Robin Agrahari
you probably have somewhere typo in the javascript no one is going to go trought it but you, try to test one by one if by using alert as you do. But don't do it on sumbit, create a mock button that will call(on click) this function editvalidate(); and check your function thoroughly and you will get to the problem eventually
How can you tell it's not being called? I'd put an alert right at the top of the method and see if it hits that. It's possible you're hitting some kind of error somewhere. If an alert at the top of editvalidate doesn't occur, try changing the obsubmit to:
onsubmit="alert('hello!'); return editvalidate();"
then you should at least see "Hello!" when you click the submit button.
looking at the code it should be called
Or Try
onsubmit="editvalidate(); return false;"
Or check where it is not misplaced, I mean the javascript should be in <head> </head>
actually i used a password checking parameter in the first if block document.editprofile.password.value == ''
and i never used an attribute like password in the form.that was my mistake and so it was not working.i found it using the firebug tool.thanks for the help.