i do have multiple text boxes called first name,last name,phone no and so on.. and i want to clear/refresh the input typed in it on button click called as refresh or clear.the problem is that the input in these text boxes are not getting refreshed because the data in it is coming from other page and it is getting refreshed when we type some thing in it
my code is as follows:
<div class="control-group">
<input value="<?php echo $model['pros']['oppid'] ?>" name="oppid" type="hidden" id="prospectid" />
<label class="control-label">First Name </label>
<div class="controls">
<input type="text" name="f_name" pattern="[-a-zA-Z]+" required="" title="First Name is required" value="<?php echo $model['pros']["f_name"]; ?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Last Name </label>
<div class="controls">
<input type="text" name="l_name" pattern="[-a-zA-Z]+" required="" title="Last Name is required" value="<?php echo $model['pros']["l_name"];?>" />
</div>
</div>
<div class="control-group">
<label class="control-label">Primary phone </label>
<div class="controls">
<input type="tel" required="" pattern="[-0-9]+" title="Please Enter correct phone" name="pri_phone" value="<?php echo $model['pros']["pri_phone"];?>" />
</div>
</div>
and the button as
<button class="btn">New</button>
please suggest me on this...
use the following code:
$("#elementid").live('click',function(){
$(this).parents('form').find('input[type="text"]').val('');
});
Assuming that your input elements are inside of a form, and so too is your button, you can do this using jQuery:
$('button.btn').click(function(e) {
e.preventDefault();
$(this).parents('form').find('input[type="text"], input[type="tel"]').val('');
});
you can write code in jquery as:
$('#new').live('click',function(){
$('#fname').val('');
$('#lname').val('');
$('#tel').val('');
});
If your button is not within the form you can use the following
$('button.btn').click(function() {
$('.control-group input:not(:submit)').val('');
});
This will reset any type of input within the control-group divs except for submit inputs
If your reset button is within the form then BenM's answer is what you want.
$(".clearform").live('click',function(){
$(this).parents('form').find('input[type="text"], input[type="tel"],input[type="time"],input[type="email"],input[type="number"],input[type="date"],select').val('');
});
Related
I have a website AMP project and I wanted to use PHP for forms.
On the webpage form which has a post method returns no action in the backend code.
So I think that the data inn´t transfered.
edit on answer:
My form code is:
<form method="POST" class="sit-form" action-xhr="ampsend.php" data-form-title="contactForm">
<div class="sit-row">
<div submit-success="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">success!
</template>
</div>
<div submit-error="" class="sit-col-lg-12 sit-col-md-12 sit-col-sm-12">
<template data-form-alert="" type="amp-mustache">failed! {{error}}</template>
</div>
</div>
<div class="dragArea sit-row">
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="firstnameHidden" value="Firstname"
id="firstnameHidden"
data-form-field="">
<input type="text" name="firstname" placeholder="Firstname" data-form-field="Name"
required="required" class="field-input display-4" value=""
id="firstname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="lastnameHidden" value="Lastname"
id="lastnameHidden"
data-form-field="">
<input type="text" name="lastname" placeholder="Lastname" data-form-field="Name"
class="field-input display-4" required="required" value=""
id="lastname">
</div>
<div class="sit-col-md-12 field sit-col-sm-12">
<input type="hidden" name="emailHidden" value="Email" id="emailHidden"
data-form-field="">
<input type="email" name="email" placeholder="Email" data-form-field="Email"
required="required" class="field-input display-4" value=""
id="email">
</div>
<div class="sit-col-md-12 sit-col-sm-12 field">
<input type="hidden" name="messageHidden" value="Message"
id="messageHidden"
data-form-field="">
<textarea name="message" placeholder="Message" data-form-field="Message"
class="field-input display-4" required="required" value=""
id="message"></textarea>
</div>
<div data-for=""
class="sit-col-md-12 sit-section-btn sit-pt-4 align-center sit-col-sm-12 field">
<button type="submit" class="btn btn-primary display-4">
send
</button>
</div>
</div>
</form>
The file_put_contents of the POST by clicking on submit is:
a:8:{
s:15:"firstnameHidden";
s:9:"Firstname";
s:9:"firstname";
s:8:"TestName";
s:14:"lastnameHidden";
s:8:"Lastname";
s:8:"lastname";
s:12:"TestLastname";
s:11:"emailHidden";
s:5:"Email";
s:5:"email";
s:13:"test#test.com";
s:13:"messageHidden";
s:7:"Message";
s:7:"message";
s:11:"testMessage";}
So does it work correctly? because as return I get the "failed!" - message from the AMP template.
I think you cannot execute PHP code on the displaying document. PHP is serving the data to show through an POST on the displaying web document as described in this post.
My solution to check if PHP is running ,and check what data was posted via form - was write everything to text file.
$serializedData = serialize($_POST);
file_put_contents('your_file_name.txt', $serializedData);
Open file and see what data was pass. If you want display message after form was submitted and pass some date like name or email. You need to use amp-mustache. And and return JSON form PHP to pass it back to your AMP page.
For example in your PHP file:
header('Content-Type: application/json');
echo json_encode(array( 'name' => $_POST['name]' ));
Of course don't forget to put appropriate CORS headers in PHP or you .htaccess file.
What i am trying to do is:
post a form when user is logged in.
but if he is not logged in then pop up login is shown to user.
and in that popup redirection URL is added to hidden field.
when popup opens and i login it redirect me to that form.
But when i try to submit form it not being submitted.
// submit button in form
$('#submitcompanyEnquiry').on('click',function(e){
e.preventDefault();
//get data attr to check if user is login
if($('#companyEnquiry').data('login')){
//companyEnquiry =>form id
//here i try to submit form
console.log('testing'); --->it is working
jQuery('#companyEnquiry').submit(); ---> //the problem is here this piece of code is executing
}else{
if($('#companyEnquiry').attr('action')!=''){
//here i added the current url to hidden field latter to used for redirection
$('#loginForm #redirectUrl').val($('#companyEnquiry').data('seotitle'));
}
//here the login popup is trigger.
jQuery("#login").trigger('click');
}
});
Things that I confirmed:
ensure that there is unique id with the
name provided.
console some value in the if block which was
running but the line of code i have mention.
PHP part is working fine i have removed the e.preventDefault();
it is works fine but doesn't achieve the require functionality.
HTML Code
<form action="<?=Route::url('default',array('controller'=>'contact','action'=>'user_contact'))?>" data-login="<?php echo $data; ?>" data-seotitle="<?=Route::url('company', array('controller'=>'listing','seotitle'=>$company_seotitle))?>" id="companyEnquiry" method="post">
<input type="hidden" name="company_to" value="<?php echo $id; ?>">
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" name="name" required aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="Name" required value="<?php echo $auth->get_user()->company_name; ?>" name="name" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if (!$auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required name="company_from" aria-describedby="basic-addon1">
</div>
<?php }else { ?>
<div class="input-group searchbox">
<input type="email" class="form-control search" placeholder="email" required value="<?php echo $auth->get_user()->companyemail; ?>" name="company_from" aria-describedby="basic-addon1">
</div>
<?php } ?>
<?php if ($auth->logged_in()) { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" value="<?php echo $auth->get_user()->company_phone_1; ?>" aria-describedby="basic-addon1">
</div>
<?php } else { ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="phone number" required name="phone" aria-describedby="basic-addon1">
</div>
<?php } ?>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="subject" required name="subject" aria-describedby="basic-addon1">
</div>
<div class="input-group searchbox">
<input type="text" class="form-control search" placeholder="message" required name="message" aria-describedby="basic-addon1">
</div>
<input data-login="<?php echo $data; ?>" id="submitcompanyEnquiry" type="submit" name="submit" value="SEND" class="form-control blue-btn send-btn">
</form>
The problem can only exist in the way you are adding the login variable to the form with the id : companyEnquiry
Check if you have add it as correct parameter because jquerys data function will only read values which have a "data-" tag infront of it.
So your php code should look like this :
echo '<form id="companyEnquiry" ' . ($login ? 'data-login="1"' : '' . '>
I have the file "1.php" and the file "2.php" ...
In "1.php", there's a HTML form:
<form action="2.php" method="post">
<div class="form-group">
<label for="db_username">Field 1:</label>
<input type="text" class="form-control" id="db_username">
</div>
<div class="form-group">
<label for="db_password">Field 2:</label>
<input type="password" class="form-control" id="db_password">
</div>
<div class="form-group">
<label for="db_passphrase">Field 3:</label>
<input type="text" class="form-control" id="db_passphrase">
</div>
<button type="submit" class="btn btn-default">Next</button>
</form>
While in "2.php", the action must be applied in PHP .. like that:
<?php
// Process of Step "1"
$db_username = $_POST['db_username'];
$db_password = $_POST['db_password'];
$db_passphrase = $_POST['db_passphrase'];
if( !isset($db_username) || !isset($db_password) || !isset($db_passphrase) ) {
header("Location: 1.php?error=1");
die();
}
?>
However .. the values of $_POST['db_username'], $_POST['db_password'] and $_POST['db_passphrase'] is empty...
You need to use the name attribute to later access the $_POST data, as in:
<input type="text" class="form-control" id="db_username" name="db_username">
You need to give the inputs a name and acces that value through $_POST['name'].
id is not relevant for php form handling
You have to give name for the textfields like this
<input type="text" class="form-control" id="db_username" name="db_username">
Put name instead of id. it should work
let's say that I have a form
<form >
<div class="control-group">
<label class="control-label" for="numeinculpat">Nume inculpat</label>
<div class="controls">
<input type="text" id="numeinculpat" placeholder="Nume inculpat" name="nume" />
</div>
</div>
<div class="control-group">
<label class="control-label" for="numeinculpat2">Nume inculpat2</label>
<div class="controls">
<input type="text" id="numeinculpat2" placeholder="Nume inculpat2" name="nume" />
</div>
</div>
</form>
And the user introduce some empty value .
How cand can I do if someone enter wrong data(or empty),when enter again, the input to have the value of data that he previous introduced ??
Add a value attribute and get last value from $_POST like below
<input
type="text"
id="numeinculpat2"
placeholder="Nume inculpat2"
value="<?php echo (isset($_POST['nume']) ? $_POST['nume'] : '' ); ?>"
name="nume"
/>
Input name must be unique, don't use same nume for other input, This might help
You are missing the action and the method attribute of form.
Follow the basic tutorial from here.
http://www.w3schools.com/php/php_form_complete.asp
If you are submitting it to the same page use " method="post">. Now when you submit and if the value is blank or whatever is the problem you will get the last entered value also use this to get the value.
if(isset($_POST["submit"])
{
$text1=$_POST["numeinculpat"];
}
over your html input
<div class="control-group">
<label class="control-label" for="numeinculpat">Nume inculpat</label>
<div class="controls">
<input type="text" id="numeinculpat" placeholder="Nume inculpat" name="nume" value="<?php echo $text1 ?>" />
</div>
</div>
Now when you submit the value you will get the last entered value.
I have the following code in index.php:
<div class="done">
<b>Thank you<?php
echo $_SESSION['session_vname']." ";
echo $_SESSION['session_lname']."! </b><br><br>Email: ";
echo $_SESSION['session_email']."<br> Status: ";
echo $_SESSION['session_status']."<br>";
?>
</div>
and
<div class="form">
<form method="post" action="process.php">
<div class="element">
<label>First Name</label>
<input type="text" name="vname" class="text" />
</div>
<div class="element">
<label>Last Name</label>
<input type="text" name="lname" class="text" />
</div>
<div class="element">
<label>Email</label>
<input type="text" name="email" class="text" />
</div>
<div class="element">
<label>Status</label>
<input type="text" name="status" class="text" />
</div>
<div class="element">
<input type="submit" id="submit"/>
<div class="loading"></div>
</div>
</form>
</div>
I execute the form with an ajax submit so the index.php gets no refresh. When finished
gets a fadeOut and a fadeIn.
This works fine except for the following part:
When div class="form" is shown, it shows the session variable from the form before the last submit.
Example:
first input: session_vname = testa
first output $_SESSION['session_vname'] = nothing
second input: session_vname = overflow
second output $_SESSION['session_vname'] = testa
So finally the question:
How can I force the to update the session variables after form submitting also index.php is not reloaded?
PHP code is executed on server side, before the page is shown to the viewer. That means you need to update static elements with JavaScript if you don't want to refresh the whole page.
You cant update server side php sessions using javascript, you should be using cookies for that type of functionality