Ajax form onsubmit stop refresh - php

I have one page lets call it "X.php" that i use that calls another page "Y.php" with the xmlhttp.open() function.
At the moment i have a form in page Y.php, and i would like to prevent it from refreshing because once i click on the submit button, my form dissapear.
I tryed with Jquery 'event.preventDefault()' in page X.php but nevertheless it didnt work.
Does anyone know a solution?
while($row = mysql_fetch_array($query)) {
echo " <h2>Update your personal settings</h2>
<form action='' id='target' method='POST'>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>";
}
Java script:
<script type="text/javascript">
$(document).ready(function(){
$("#test1").click(function(event){
event.preventDefault();
});
});

In your form tag. Add onSubmit="return false;
<form action='' id='target' method='POST' onSubmit="return false;>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>
As #andrew mentions. Your php script will generate multiple <form> tags which will all have the same id id='target'. This can cause problems on your page as that isnt valid html

I would have thought that the javascript you posted should work actually.
But if this is your actual code I suspect it fails because all of the submit buttons on all forms have id='test1' and id should be unique
Try like this:
$i=0;
while($row = mysql_fetch_array($query)) {
echo " <h2>Update your personal settings</h2>
<form action='' id='target."$i++".' method='POST'>
Surname: <input type=text name='lid_voornaam' value='".$row['lid_voornaam']."'><br/>
Name: <input type=text name='lid_naam' value='".$row['lid_naam']."'><br/>
Email: <input type=text name='lid_email' value='".$row['lid_email']."'><br/>
<input type='submit' value='Whatever' id='test1'/>
</form>";
}
and
$("form").submit(function(event){
var id = $(this).attr('id').split('target')[1];
alert('form ' +id+ ' clicked');
event.preventDefault();
});

Related

Is it possible to have multiple actions in forms using only one submit button?

See question -
I've tried this:
echo "<form action='index.php' method='post'>
Use the previous input of participants: <input type='radio' name='participants[]'value='$participants[0]'>
OR <br>
<form action='index.html' method='post'>
Use a different input of participants: <input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'> </form> <br>";
Both of the radio button lead me to index.php when I want to be able to go to index.html in case i press on the second radio button.
You may solve it by using JQuery
<form action='index.php' method='post'>
Use the previous input of participants:
<input type='radio' name='participants[]' value='$participants[0]'>
<br/>OR <br>
Use a different input of participants:
<input type='radio' name='participants[]' value='0'>
<input type='submit' value='send' name='send'>
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var form = $('form');
var input = $('input[type=radio]');
input.on('click', function() {
if ($(this).val()==0) {
form.prop('action', 'index.html');
} else {
form.prop('action', 'index.php');
}
});
});
</script>

Submit Form When a date is chosen

Is there a way I can submit a form using the input date type. meaning if a date is chosen, the form gets submitted without any button click?
For example,
I have this form:
<form action='ex.php' method='POST'>
Enter a date: <input type='date' name='date' />
</form>
If have any datepicker then try below code.
$('#date1').change(function(){
console.log('Submiting form');
$('#form1').submit();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action='ex.php' method='POST' id="form1">
Enter a date: <input type='date' name='date' id="date1" />
</form>
Something like this might do the trick - though some validation of the user supplied date might be called for to ensure it is a date that is added & processed.
<form action='ex.php' method='POST'>
Enter a date: <input type='date' name='date' />
</form>
<script>
var oDate=document.querySelectorAll('input[type="date"]')[0];
oDate.addEventListener('blur',function(e){
/* additional date validation here */
if( oDate.value!='' )oDate.parentNode.submit();
},false);
</script>

Submit a form multiple times in loop

How can I submit a form multiple times in a loop.
What I am trying to do is to post post data to some 3rd party API for SMS. Now this 3rd Party SMS gateway takes only 50 no. in one go and I have more than 50 mobile no.
for (int jj=0;jj<=5; jj++)
{
%>
<form name="f" id="ff" action=http://smswebsite.com/api.php >
<input type=text name=username value="amu" >
<input type=text name=password value="password" >
<input type=text name=source value="SenderID">
<input type=text name=dmobile value="917417010049" >
<input type=text name=message value="Testing All from hashmi, malik hayat" >
</form>
<script type="text/javascript">
document.getElementById("ff").submit();
</script>
<%
}
%>
Please help
What is the issue you are having????
3 problems I see
If a first time form is submitted the page will navigate away. so the next form may not get turn to be submitted.
I have noticed your form id remains the same, so there will be multiple forms with same id each time a loop is executed. append the jj counter value with form tag for id "ff"+jj and with javascript getElementById("ff"+jj).
Use ajax to post the form to so that your page may not navigate away.
Call ajax post when document read event is executed: because you might be submitting a form where as the page may not have been rendered yet.
$( document ).ready(function() {
// code to submit form through ajax
});
You can post it as an array.
<form name="f" id="ff" action=http://smswebsite.com/api.php >
for (int jj=0;jj<=5; jj++)
{
%>
<input type=text name="username[]" value="amu" >
<input type=text name="password[]" value="password" >
<input type=text name="source[]" value="SenderID">
<input type=text name="dmobile[]" value="917417010049" >
<input type=text name="message[]" value="Testing All from hashmi, malik hayat" >
<%
}
%>
</form>
<script type="text/javascript">
document.getElementById("ff").submit();
</script>

Multiple submit on form php

On my form I have 3 submit,I want to know the best way to handle these buttons because the default action is the same form:
<form method="POST" action="file_where_form_is.php">
Name:<input type='text' name='name'>
<input type='submit' name='add' value='Add Names' ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit'name='Preview'value='Preview'ONCLICK='window.location.href="file_where_form_is.php">
<input type='submit' name='submit'value='Submit' ONCLICK='window.location.href="send.php">
</form>
This not seems to work completely,just the 2 first work and for the last submit does nothing(with name='submit')
Any suggestions?
I finally found in header part add something like this:
if(isset($_POST['submit'])){
header("location: somefile.php");
}
elseif(isset($_POST['Preview'])){
header("location: anotherfile.php");
}
elseif(isset($_POST['add'])){
header("location: anotheronefile.php");
}

PHP submit form if conditions are met

I'm NOT using javascript has it is disabled in my environment.
Please review the code below.
I'm trying to use PHP to create the logic that Javascript or jQuery would allow me to do with a simple : document.form2.submit()
<div>
<h2>How many services do you need ?</h2>
<form action="<?php $_SERVER['PHP_SELF']?>" method="POST" name="fServ">
<input type="number" name="numServ" />
<input type="submit" value="SEND">
</form>
<div>
<?php
if(isset($_POST['numServ']) && $_POST['numServ']!==""){
echo "We need ".$_POST['numServ']." services";
echo "<form name='form2' id='form2' method='get' action= $_SERVER[PHP_SELF]>";
for($k = 0 ; $k< $_POST['numServ']; $k++){
//echo "<input type=\"text\" value=\"services$k\">";}
echo "<br><input type='text' name='services$k' value=''>";
if(empty($_GET['services'.$k])){
echo "You didn't fill up all the fields. Please put in a service";
}
if (!empty($_GET['services'.$k])){
echo "Form Filled";//
}
}
}
echo "<input type='submit' name='sendForm' value='SEND'/></form>";
if(!isset($_POST['numServ'])){
echo "We don't have any services yet.";
}
else if($_POST['numServ']==""){
echo "Please put in a number";
}
?>
Can this be achievied through PHP ? Where if the conditions are met and the form fields are NOT blank then submit the form, otherwise die and show a message.
Even then you need to submit the details of the form:
Name: <input type="text" name="name" /> (Min 5 Chars)
Age: <input type="text" name="age" /> (Should be a Number)
<input type="submit" />
Now using server-side validation, this can be done this way:
if (isset($_POST["name"], $_POST["age"]))
if (strlen($_POST["name"]) > 5 && is_numeric($_POST["name"]))
die("Error! The conditions are not met!");
die("Form Submitted!");
This way you can submit the form where JavaScript is disabled.
If I understand, you need auto-submit the form without javascript.
Maybe this thread helps you:
How to submit a form on page load without clicking submit button?

Categories