Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
When I click on the submit button it should give a $_POST to my send page.
What I do is:
-> When Click on <input type="submit" name="sendMail" value="Verzend E-Mail" class="sendMail">
-> I make a var link and set the whole <form action="" method="post" id="formSendMail"> in it.
-> Then give with a $.ajax() request the link as data to the page where I send it to.
-> When .done(function(data)) put data in $("div.emailSendComplete")
So now you know what I exually want...
What I have till now on code is:
Jquery:
<script>
$(document).ready(function(){
$("input.sendMail").on("click", function(){
var link = $("form#sendMailForm");
$.ajax({
type: "POST",
url: "checklistHandler.php?action=sendMail",
data: link
}).done(function (data){
$("div.emailSendComplete").append(data);
});
});
});
</script>
HTML:
<table id="cont" style="line-height: 80px; text-align: center;">
<tr id="sendMail" style="">
<form action="checklistHandler.php" method="post" id="sendMailForm">
<td style="width: 150px;">Email:</td>
<td>
<input style="padding: 5px; text-align: center; min-width: 10px; border-radius: 3px;" id="email" type="text" name="email" id="email" value="<?= $email ?>">
<span class="emailCheck"></span>
</td>
<td style="position: relative; left: 50px;">
<input type="hidden" name="verzendMail" value="1">
<input type="submit" name="sendMail" value="Verstuur E-Mail" class="sendMail" style="border-radius: 3px;">
</td>
</form>
</tr>
PHP(checklistHandler.php):
if ($_GET['action'] == 'sendMail') {
print_r($_POST);
exit();
}
Resolved:
The I did try to give the whole POST but I also could only give the INPUT with the email in it.
Cuz I only needed that.
Are you trying to send the HTML of the form? If so, instead of this:
var link = $("form#formSendMail");
(That will return a jquery object unless I'm mistaken)
use this:
var link = $("#formSendMail").html();
Not entirely clear what you are trying to do, so I may have misunderstood...
There are two three problems here:
You are not cancelling the default form submit, so your form will submit the normal way as well and your page will be refreshed;
You are not sending any data to your script with your ajax call, just a jQuery object, the form;
Your form has a different ID than the one you are using in your ajax call: formSendMail vs sendMailForm.
It should look more or less like:
$(document).ready(function(){
$("input.sendMail").on("click", function(event){
^^^^^ you need this later on
// second and third problem, missing data and wrong ID
var link = $("form#sendMailForm").serialize();
^^^^^^^^^^^^ use the right ID here
// first problem, default submit
event.preventDefault();
// the rest of your code
There are few things you need to change in your code
$(document).ready(function(){
//instead of input button click use form
$("#sendMailForm").submit(function(e){
// first problem, prevent default submit
e.preventDefault();
var link = $('#sendMailForm').serialize();
$.ajax({
type: "POST",
url: "checklistHandler.php?action=sendMail&email=",
data: link
}).done(function (data){
$("div.emailSendComplete").append(data);
});
});
});
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a problem that, i want to check whether the user is already registered or not, if registered, the form needs to submit and show user data's, if not, show that the user is not registered on the same page. I am new to ajax
<form class="form-group" action="<?php echo site_url('home/verify_usr'); ?>" method="post">
<table style="width: 100%;">
<tr><td style="">
<input class="form-control" type="text" name="reg_id" placeholder="Enter the User ID here.." aria-label="Search" style="width: 100%">
</td><td>
<button type="Submit" class="btn btn-danger">Submit</button>
</td></tr>
<tr><td style="text-align: center;color: red">
<span id="fails">
<!-- the verification fails comes here -->
</span></td></tr>
</table>
</form>
html:
<form id='yourFormId'>
//add your html
</form>
js:
<script type="text/javascript">
$(document).ready(function () {
$( "#yourFormId" ).submit(function( event ) {
//event.preventDefault();
$.ajax({
beforeSend: function () {
},
complete: function () {
},
type: "POST",
url: "<?php echo site_url('controller/checkUserReg'); ?>",
data: $(this).serialize(),
success: function (data) {
}
});
});
});
</script>
php:
function checkUserReg(){
// your code to Model->checkUserReg which check user registered or not
// if not reg then reg it else show your user list
}
try to make form hidden then appear after login
then take id from session
I have a strange problem that is driving me crazy!
I'd like to submit a HTML form by ajax using jquery-form version 3.46.0 and then get the POST data on server, but my Submit button's name won't show up in the POST data! Maybe there's something wrong with this lib?
This my code:
<form id="form" action="test.php" method="post">
<input name="email" type="email" placeholder="Email" required>
<!-- And yes! I didn't use an input with type submit because
I need my button to have some HTML content and applied css styles to them. -->
<button id="submit" type="submit" name="myFormName">
<span class="label">Submit</span>
</button>
</form>
<div id="formResult" style="display: block; margin: 20px 0; padding: 10px; background-color: #fbe6e6;" role="alert"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="//oss.maxcdn.com/jquery.form/3.50/jquery.form.min.js"></script>
<script>
jQuery(document).ready(function($) {
$('#submit').on('click', function(e) {
e.preventDefault();
$('#form').ajaxSubmit({
clearForm: true,
target: '#formResult',
success: function() {
// do sth
},
error: function() {
// do sth
}
});
});
});
</script>
And this is the test.php file:
<?php
var_dump($_POST);
I need the name of my Submit button appear in POST data on server as expected... But it doesn't! Does anybody have any idea?
If you use name for button, then while taking as $_POST, the value will be shown as post data. Instead of using name try using id or simply remove that if you don't have any use of that
We can solve this by following ways -
Use jquery form plugin and it will post all form data
If you are not using the above or any plugin then simply append submit button name to the form serialize as below:
$('#myForm').submit(function(event){
event.preventDefault();
$.ajax({
type: "POST",
url: "process.php",
data: $('#myForm').serialize()+"&btnSubmitName=true",
success: function(response){
$('#resultDiv').html(response);
}
});
});
<div><a href="javascript:foo1()">
<div class="holder"><span class="label">Submit</span></div>
<div class="loader"><i class="fa fa-circle-o-notch fa-spin"></i></div></a></div>
And implement foo1 to submit the form.
Updated: It still not work after I add "#".
I am new to ajax. I am practicing to send value to php script ,and get result back.
Right now, I met one issue which I can not show my result in my html page.
I tried serves answers online, but I still can not fix this issue.
My index.html take value from the form and send form information to getResult.php.
My getResult.php will do calculation and echo result.
How do I display result into index.html?
Hers is html code
index.html
<html>
<body>
<form name="simIntCal" id="simIntCal" method="post"
>
<p id="Amount" >Amount(USD)</p>
<input id="amount_value" type="text" name="amount_value">
<p id="annual_rate" >Annual Rate of Interest
(%)</p>
<input id="rate_value" type="text" name="rate_value">
<p id="time_years" >Time (years)</p>
<input id="time_value" type="text" name="time">
<input id="calculate" type="submit" value="Calculate">
</form>
<p id="amount_inteCal" >The Amount (Acount
+ Interest) is</p>
<input id="result" type="text">
</body>
</html>
ajax script :
<script>
$('#simIntCal').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'getResult.php',
data: $('#simIntCal').serialize(),
success: function (result) {
$("#result").text(result);// display result from getResult.php
alert('success');
}
});
});
</script>
getResult.php
<?php
if ($_SERVER ["REQUEST_METHOD"] == "POST") {
//do some calculation
$result=10;//set result to 10 for testing
echo $result;
}
?>
You are missing the '#' in front of your css selector for result.
$("result").text(result);// display result from cal.php
Should be
$("#result").text(result);// display result from cal.php
index.php
----php start---------
if(isset($_POST['name'])){
echo 'Thank you, '.$_POST['name']; exit();
}
----php end ---------
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script>
function test(){
var formDATA = {'name': $('#input_name').val()}
$.ajax({
type: 'POST',
url: 'index.php',
data: formDATA,
success: function(response){
$('#result').html();
}
});
}
</script>
<input id="input_name" type="text" value="">
<button onclick="test();">Test Ajax</button>
<div id="result"></div>
Try something simple, this is a very basic version of ajax and php all in one page. Since the button triggers the function you don't even need a form (doesn't mean you shouldn't use one). But i left it simple so you could follow everything.
Sorry when i added php open and closing tags it didn't show up as code.
Also don't forget to include your jquery resources.
In your html file where you want the result to display, you probably want to be using a div.
Currently your code is using an input field:
<input id="result" type="text">
And what you probably want is something like this:
<div id="result"></div>
Unless you were intending to have the result show up in an input field inside your form, and in that case, the input field isn't actually inside your form code.
Hi I have been at this for days now and I just cant figure out why this isn't working, please could someone take a look.
index.php
<form id = "update_status" method = "POST">
<textarea id="shadow" name ="user_status" placeholder = "Share what is on your mind!" cols="97" rows="1" title="Share what's on your mind"></textarea>
<input style="float:right; margin-top: 0.3%;margin-right : 0% !important;" id = "btnStatus_update" name = "btnStatus_udate" type="button" value="Add Post" title="Your posts will be made public"></input></form>
swift.php
$(document).ready(function(){
$("#btnStatus_update").click(function(e) {
e.preventDefault();
//alert("Confirming function works");
$.ajax({
cache: false,
type: 'POST',
url: './datacenter.php',
data: $("#user_status"),
success: function(d) {
$("#successMesg").html(d);
}
});
});
});
datacenter.php
if (isset($_POST['user_status'])) {
var_dump($_POST['user_status']);
foreach ($_POST as $ak => $av) {
if (empty(trim($_POST['user_status']))) {
echo 'Say what is on your mind'.' ';
} else {
$userstatus = $_POST['user_status'];
add_user_status($user_data['id'], $userstatus);
}
}
}
using var_dump for testing I was hoping it did return the data, but instead i get NULL, i need the data so i can pass it into the add_user_status function to be added to the database, but it seems there is something missing or off about the code denying me my satisfaction. Please help
There are a few things that appear to be missing:
Index.php:
You need to add an action="something" attribute to the form tag, this tells the form what to do when you submit it. (unless you are manually handling this is JS somewhere else?)
<form id = "update_status" action ="data.php" method = "POST">
Also, unless you are using JavaScript on the index page to handle the actual submitting of the form, your <input> should include a type="submit" attribute. (this will also make it a button) and when clicked it will automatically submit the form to the action location above.
Swift.php:
the code posted is JS, and does what the first line of the previous paragraph mentioned (handles the submit button). Do you include this file inside the index.php? if the index.php cannot see it, then it wont run. It must also be in the html somewhere in a proper <script> block.
I believe the correct way to send form data using ajax is to serialize the data:
$('#update_status').serialize() instead of just sending the one input field.
You will also be required to reference the jQuery libraries, preferably in the index, but could also go in swift.php. I am also assuming that the code posted appears in the necessary <script> block.
Data.php:
should this be datacenter.php? your Swift.php is sending the ajax request to ./datacenter.php
On a side note, if you need it to use Ajax then you actually don't need the action ="data.php" method = "POST" in the form (Ajax does all that for you)
The way it could be done would be something like this:
Index.php:
// HTML beginning stuff
<head>
// Either reference the script in its own JS file or:
// Need to also include jquery library
<script type="text/javascript">
$(document).ready(function(){
$("#btnStatus_update").click(function(e) {
e.preventDefault();
//alert("Confirming function works");
$.ajax({
cache: false,
type: 'POST',
url: './datacenter.php',
data: $('#update_status').serialize(),
success: function(d) {
$("#successMesg").html(d);
}
});
});
});
</script>
</head>
<body>
<form id = "update_status">
<textarea id="shadow" name ="user_status" placeholder = "Share what is on your mind!" cols="97" rows="1" title="Share what's on your mind"></textarea>
<input style="float:right; margin-top: 0.3%;margin-right : 0% !important;" id = "btnStatus_update" name = "btnStatus_udate" type="button" value="Add Post" title="Your posts will be made public"></input>
</form>
</body>
datacenter.php:
<?php
var UserStatus = user_status
if (!empty(UserStatus)) {
var_dump($_POST['user_status']);
// process as necessary
}
?>
But, including the
<form id = "update_status" action ="datacenter.php" method = "POST">
and changing the button to
<input style="float:right; margin-top: 0.3%;margin-right : 0% !important;" id = "btnStatus_update" name = "btnStatus_udate" type="submit" value="Add Post" title="Your posts will be made public">
will allow users to still post information if they have JavaScript disabled.
the selector for the textarea is incorrect, the id is not "user_status", its "shadow"
data: $("#user_status"),
should really be:
data: $("textarea[name=user_status]")
or
data: $("#shadow")
I was looking for a simple contact form in jQuery and I found out this one on Google
http://code.google.com/p/gunnertech/source/browse/trunk/public/javascripts/contact.js?r=3
the problem is that it is missing the PHP part. Here is the part where the script handle the message and pass it to the PHP file:
function send(a, d){
$.ajax({
type: "POST",
url: a,
data: d,
success: handle
});
}
I don't know what this "a" and "d" means. Do you know what do I need to do in order to make this jquery form work and simply send the content to a specified email address? Thanks.
EDIT: thanks to everyone. Now I understand what the two letters means. The problem is that I don't know what to write in the PHP file to send the data to my email! This is the HTML part:
<form method="post" action="/" id="contact">
<div>
<label for="contact-text" class="js-placeholder placeholder">
<span>Type your name, email, and message here.</span>
</label>
<textarea aria-required="true" required="" rows="1" cols="70" id="contact-text" name="contact_text" style="height: 15px; overflow: hidden; padding-top: 0px; padding-bottom: 0px;">
</textarea>
</div>
<input type="submit" value="Send" name="contactsend" id="contactsend">
</form>
Can you help me? Thanks.
I suppose 'a' would be the form's 'action' attribute. And 'd' the forms 'data', meaning the form's fields concatenated into one string (which you'd then need to parse in your php script).
EDIT: Could you also post the part where the 'send' function is called?
a is your contact form handler (ex: contact.php) and d is the data you want to pass to it, for ex: {name: 'Joe', age: 27}
The function is looking for the action file (where the PHP is). So whatever you set <form action=""> to jQuery will send to it. "d" is the data your contact form is collecting sent serialized to your action file.
You can create a simple php file to handle the data that works the same way a traditional PHP send mail would work.
According to your posted code:
//note that d get the data from your contact form
var a = 'your_contact_page.php';
var d = {name: 'your_name_from_contact', subject: 'subject_from_conatc_form'}
function send(a, d){
$.ajax({
type: "POST",
url: a,
data: d,
success: handle
});
}
and handle is also a function that will produce you the response