All Three Forms Get Empty in Single Click - php

I have Three forms in a page with different submit button and all HTML are properly closed.
all forms have different value..now problem is when i press submit button of any form for store value in database..another form field is get empty..how can i prevent them for stopping them.i want when i press a submit button of any form only these form value submit in database..other form field does not get empty..how can i solve this ..i have done coding for single form like this
<form class="form" method="POST" name="pool">
<label for="name1">Ist Player Name</label>
<input type="text" name="fname" id="fname" />
<label for="name2">2nd Player Name</label></td><td><input type="text" name="sname" id="sname" />
<label for="stime">Start Time</label>
<input type="text" name="stime" id="stime"
<label for="stime">End Time</label>
input type="text" name="etime" id="etime" />
<input type="submit" value="Confirm" name="pool" id="pool" />
</form>
same as 2nd and 3rd form .name are change of all form..and php coding of form like this
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("snookar", $con);
if(isset($_POST['pool']))
{
/* all procees to save record */
}
then 2 nd form
if(isset($_POST['snooke']))
{
/* all procees to save record */
} and so on...
now how can i done this only specified form value submit and another form does not empty...

You can do the following,
<form class="form" method="POST" name="pool1">
<!-- Form input fields -->
<input type="submit" value="Confirm" name="poolbtn1" id="poolbtn1" />
</form>
<form class="form" method="POST" name="pool2">
<!-- Form input fields -->
<input type="submit" value="Confirm" name="poolbtn2" id="poolbtn2" />
</form>
Now use the jquery to submit the form,
$("#pool1").submit(function(e) {
//Do your task here
return false; //this will prevent your page from refreshing
}
$("#pool2").submit(function(e) {
//Do your task here
return false; //this will prevent your page from refreshing
}

The ajax script.
$('input#form1button').click( function() {
$.ajax({
url: 'some-url',
type: 'post',
dataType: 'json',
data: $('form#myForm1').serialize(),
success: function(data) {
$("#myForm1").resetForm();
return false
}
});
});
Here in the above code form1button is the id of the button that is in the form i-e form with id myForm1
so repeat the same function for the rest of the two form with different form and button ids
Html
<form method='POST' aciton='' id='myForm1'>
some inputs goes here
<input type=button id=form1button />
</form>

Related

Reopen toggle after form submit

I am still working on my school project, which is almost finished.
With your help, I successfully created a working system that allows users to write, edit and delete data to/from the database.
The only problem I have right now us "user-friendly form." I managed to create auto-focus, insert correct values on edit so the user can see what was previously written in that field, etc.
I have my forms hidden with jquery. When a user clicks add, the form slides in. What I need to achieve is: "when a user clicks submit and the page refreshes and adds the element to the database, the form should appear again so users can add data faster."
Here is my code.
$(document).ready(function() {
$('#x').click(function() {
$('.y').toggle("slide");
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="x">Click</div>
<div class="y" style="display: none;">
<div class="container">
<form action="insertzunanja.php" method="POST" id="x">
<input type="hidden" name="narocilo" value="0.1412312">
<input type="hidden" name="id" value="id-1">
<input type="text" name="dolzina" style="width:49%;" placeholder="Dolzina (v cm)">
<input type="text" name="sirina" style="width:49%;" placeholder="Sirina (v cm)">
<input type="text" name="kolicina" value="1" style="width:49%;" placeholder="Kolicina">
<input type="text" name="opombe" style="width:49%;" placeholder="Opombe">
<input type="submit" value="Send">
</form>
</div>
</div>
Thanks and best regards.
You can use AJAX call to send the data to php file instead of form action. According to your code you will have something like this:
<div id="x">Dodaj</div>
<div class="y" style="display: none;">
<div class="container">
<input type="hidden" name="narocilo" value="<?php echo $randomNum; ?>">
<input type="hidden" name="id" value="<?php echo $id; ?>">
<input type="text" name="dolzina" style="width:49%;" placeholder="Dolzina (v cm)">
<input type="text" name="sirina" style="width:49%;" placeholder="sirina (v cm)">
<input type="text" name="kolicina" value="1" style="width:49%;" placeholder="Kolicina">
<input type="text" name="opombe" style="width:49%;" placeholder="Opombe">
<input id="sub" type="submit" value="Send">
</div>
</div>
$(document).ready(function(){
$('#x').click(function() {
$('.y').toggle("slide");
});
});
$("sub").click(function(){
$.post("insertzunanja.php",
{
dolzina: $("input[name=dolzina]"),
sirin: $("input[name=sirina]")
},
function(){
$("input[name=dolzina]").val("");
$("input[name=sirina]").val("");
if($('.y').is( ":hidden" )) {
$('.y').toggle("slide");
}
});
});
Basically, when you click on button you call php with AJAX POST request passing two values dolzina and sirin retrieved by the html code(note: you have more values to pass so change it accordingly) to php file. Jquery deletes the values of the input fields and check if input fields are shown. If not the inputs fields are shown.
If you are using PHP to process the form submission and generate the code in your question, and you always want the form to be displayed after submission, you can do this:
At the PHP code identify submission (e.g. isset($_REQUEST['id']) ).
[if #1 is true] On generating jQuery code, add $('.y').show(); within the ready function (but separated from the existing click function).
Example:
<?php
// your existing code...
?>
$(document).ready(function() {
<?= ( isset($_REQUEST['id']) ? '$(".y").show();' : '' ); ?>
$('#x').click(function() {
$('.y').toggle("slide");
});
});
<?php
// your existing code...
?>

Submit data of current form along with other form as well

I have two forms
first_form is for ajax request to load new input data which is displayed in second_form as input fields(this is already done)
second_form is to submit all data of its own and first_form as well(this is not achieved)
first_form is using form-inline to display inputs horizontally while the other form is a normal form so i cannot combine both the forms.
So when user clicks submit button of the second_form i want to send all its data along with the data of the first_form as well.
Example:
<form id="first_form" class="form-inline">
<input type="text" />
<input type="text" />
<button type="submit" class="btn btn-primary">Load</button> // This loads fields for second form
</form>
<form id="second_form">
<div class="loaded_data"> // this div is loaded with ajax
<input type="text" />
<input type="text" />
<button type="submit" class="btn btn-primary">Save</button> // This must send all fields of first and second form
</div>
</form>
I do not want to use ajax for second form. The form will be submitted and store method of Laravel controller will be called for further processing
You can use jquery to get values from second_form to first_form when second form is submit:
$('#second_form').submit(function({
$('#in_1_1').val($('#in_2_1').val());
$('#in_1_2').val($('#in_2_2').val());
});
In HMTL add ids to inputs:
<form id="first_form" class="form-inline">
<input type="text" id="in_1_1"/>
<input type="text" id="in_1_2"/>
<button type="submit" class="btn btn-primary">Load</button> // This loads fields for second form
</form>
<form id="second_form">
<div class="loaded_data"> // this div is loaded with ajax
<input type="text" id="in_2_1" />
<input type="text" id="in_2_2" />
<button type="submit" class="btn btn-primary">Save</button> // This must send all fields of first and second form
</div>
</form>
With jQuery you have access to the serialize method. You can grab all the data from form 1 and add it to the data of form 2 in a $.ajax call.
var form1Data = $('#first_form').serialize();
You can then send form1Data along with your second form:
$.ajax({
method: 'POST',
url: 'server/form.php',
data: {
form1: form1Data,
form2: $('#second_form').serialize();
}
})
.done(function( msg ) {
console.log('all done:', msg);
});

Having 2 forms on same page but only loading second after first is submitted

So what i want do is, have a form displayed on load where it asks user for their details such as name etc, then once the user clicks submit, i want that information to carry over to the next form, i know i have to hidden fields for that.
I want another form to be displayed after they click submit, this all has to be done using POSTBACK, so pretty much having 2 forms on one php page but only displaying the second one after the first has been submitted.
I know i can do this by creating two different php files and using header but i would like to learn how to do it via postback.
<form name="firstform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> " onSubmit="return validator();">
<p>Please fill in the following form</p>
<p>Given Name* <input type="text" name="fname" id="fname"/><br/>
Middle Name <input type="text" name="mname"/><br />
Family Name* <input type="text" name="lname" id="lname"/><br />
Chosen Username* <input type="text" name="uname"/>
</p>
<p><input type="submit" value="submit" id="submit"/>
</form>
<form name="secondform" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?> " >
Test* <input type = "text" name="test"/>
</form>
You can use jquery and Javascript if this is just a quick form
There is a nice form plugin that allows you to send an HTML form asynchroniously.
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
// Hide all forms
$('form').hide();
// Show the first form
$('form').eq(0).show();
$('form').eq(0).on('submit', function() {
// Submit via ajax
// Unhide second form
$(this).hide();
$('form').eq(1).show();
return false;
});
});
</script>
I would validate that the first forms data is correct, then test to see if the form has been submitted without errors.
if (isset($_POST['submit']) && empty($errors)) {
// Output second form
} else {
// Output first form
}
You would either send the first set of data to the database or you can add them as hidden fields in the second form.
This is assuming you put any generated errors in to an array called $errors.

Remove the form input fields data after click on submit?

hi i am using a form for users subscription on the website with target is _new.
when i click on the submit button that the submitting message shows on the new window but the previous window still holding the data.
How to remove input fields data after submitting.
<form target="_new" action="samplepage.php" method="post">
<input type="text" name="inputtxt1" />
<input type="text" name="inputtxt2" />
<input type="submit" value="submit" />
</form>
Any suggestions???
Make the autocomplete - off
try this
<form target="_new" action="samplepage.php" method="post" autocomplete="off">
<input type="text" name="inputtxt1" />
<input type="text" name="inputtxt2" />
<input type="submit" value="submit" />
</form>
Reset form data using this
$('form').get(0).reset();
$(document).ready(function(){
$('form_name').submit(function(){
$('input[type="text"]').val('');
});
});
You can set value to null for text field using jquery on submit button click
$("input[type=submit]").click(function()
{
$("input[type=text]").val('');
});
EDIT:
I don't know is this a good approach, use blur instead of click event
$("input[type=submit]").blur(function()
{
$("input[type=text]").val('');
});
But it will meet your need.

jquery 2 forms one floating

I have a page with login form (on click, login form slides dowm) and underneath I have a link for registering a new user. With click on 'register new' link, new form pops-up and after validating each field, the submit event doesn't work - because there is a login form too and jQuery tries to trigger submit event of the first form.
How to trigger this specific onsubmit event - for the second form? I tried to hide a first form, but it didn't work and then I try to disabled it, which doesn't work as well.
(forms on separate pages works fine - validated with PHP and JS)
I think this is an easy thing to do .. but I cannot figure out how to do. Till now I overcome this problem, but I really like to find out how to make it work.
Thanks.
Unfortunatelly .. none of this answers works ...
Here is the code:
<div id="loginFormContainer">
<h2 id="loginShow" class="myriad_pro_bold_condensed_italic">Login</h2>
<form name="login_form" action="<?php if(isset($action) && !empty($action)) echo $action; ?>" method="POST" id="login_form" >
<fieldset>
<label>Your name </label>
<input type="text" name="username" maxlength="30" id="username_login" value="" class="required" placeholder="Enter Your Name" />
<label>Your password </label>
<input type="password" name="password" maxlength="16" id="password_login" value="" class="required " placeholder="Enter Your Password" />
</fieldset>
<input type="hidden" name="token" value="<?php if(isset($token)) echo $token; ?>" />
<input type="submit" name="submit-login" id="submit_login" value="Login" />
</form>
<div class="small">Register | Forgotten Password</div>
<div class="error"></div>
</div>
And JS ... should work like: if on register click: register windows pops-up, if on login the login form should slide down.
Right now, both of them slides ...I can remove the login form if I want to register, but the submit button from register form doesn't work.
$("#login_form").hide();
$("#loginShow").click(function(){
$('form#login_form .error').remove();
$("#login_form").slideDown("slow");
$('form#login_form').on("submit", function(event) {
event.preventDefault();
//code validation and ajax submit
}); //end form submit
});
$('#register').click(function(e) {
//$('#loginShow').remove(); //form is removed, but submit event still doesn't work
//if I completely remove login form from php page, then works fine
e.preventDefault();
$('form#register_form').click(function(event) {
event.preventDefault();
//
}); //end form submit
});//end register floating
Oh, yes, one final detail: on Chrome works fine :S, not in FF (v.10)
$(":submit").click(function(e){
$(this).closest("form").submit();
return false;
});
Try this:
var secondForm = $('form').eq(1);
secondForm.trigger('submit');
Just give the second submit button an id.
<input type='submit' id='submit2'>
Now for trigerring validation bind onclick event to the submit2 id
$('#submit2').click(function(){
//logic goes here
});

Categories