I don't appear to have any data being sent when a form is submitted through jquery
A snippet of my issue (code reduced to simplify):
head:
<script>
function dSubmit(formName, formAction, fieldsToCheck, divToHide){
// No Errors process form
var url = formAction; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("#" + formName).serialize(), // serializes the form's elements.
success: function(data)
{alert(data)}
});
return false; // avoid to execute the actual submit of the form.
}
</script>
body:
<a onclick="dSubmit('treatmentDetails','treatments/processTreatments.php','','')"><img style="width:35px;" src="i/update-icon.png"/></a>
<form name="treatmentdetails" id="treatmentdetails"
action="processTreatments.php" method="POST">
<input type="text" name="treatmentName" value="<?php echo $treatment;?>"/>
<input type="text" name="treatmentId" value="<?php echo $treatmentId;?>"/><br>
</form>
php (treatments/processTreatments.php):
$table = $dbPrefix."treatmentsOptions";
$treatmentId = $_POST['treatmentId'];
$treatmentName = $_POST['treatmentName'];
$query = mysql_query("UPDATE $table SET name='$treatmentName' WHERE id='$treatmentId'");
echo "variables = id: $treatmentId name: $treatmentName"; // according to jquery submit neither variable appear
You typo treatmentDetails, fix to treatmentdetails.
$("#treatmentDetails").serialize() is return empty string.
your correct code is:
<a onclick="dSubmit('treatmentdetails','treatments/processTreatments.php','','')"><img style="width:35px;" src="i/update-icon.png"/></a>
Related
pleae note that i m trying jump to test.php page from index.php/html using ajax and mysql, simple if text-input not found in table so it should go to test.php else stay on index.php/html page with ajax alerts, but from index page everytime receiving NOT AVAILABLE and sometime submit button not functional, below code FYR...
//index.php $(document).ready(function() {
//default form not submitted
$("#submit").data("submitted",false);
//preventing enter key in input field from submitting form
$("#welcome").on("submit", function(e){
if($('#submit').data("submitted")===false) {
e.preventDefault();
}
});
//trigger form submission
$("#submit").on("click",function(){
validate();
});});
//default form not submitted
//$("#submit
function validate() {
var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
cache: false,
dataType: "json",
success: function(data) {
if(data){
alert("NOT AVAILABLE");
} else {
$("#submit").data("submitted", true);
$("#welcome").submit();
}
}}</script> <form action="check_test.php" method="post" name="welcome" id="welcome" /> <table width="550" border='0' align='center' cellpadding='0' cellspacing='0'> <tr>
<td align="center"><label>
Enter Inv. # *:
<input name="invst_num" type="text" id="invst_num" size="40" />
<input name="submit" type='submit' id='submit' /> </label></td> </tr></table> </form>
//check_test.php <?php
include ("config/config.php");
//get the username
if (isset($_POST['submit'])) {
$invst_num = $_POST['invst_num'];
//mysql query to select field username if it's equal to the username that we check '
$sql = mysql_query("select invst_num_ar from shareholders_ar where invst_num_ar = '$invst_num' ");
if ($result = mysql_num_rows($sql)>0) {
echo ($result);
}
}
?>
// if not found...test.php should load
<html>
<form
...
Register Data
/form>
</html>
Your conditional is backwards
if(data){
Should be
if(!data){
Or the alert should be flipped with the listener adding logic.
var num = $('#invst_num').val();
$.ajax({
type: 'POST',
url: 'check_test.php',
data: num,
ajax call is sending value frome data key, so you send only the text field content.
You probably should send sth like this:
$.ajax({
type: 'POST',
url: 'check_test.php',
data: { 'invst_num': num },
...
Open Dev tools in your browser and check what content is being sent during ajax call.
I have several places that's returning empty and I am not sure why.
One place is using ajax. When an user clicks on the radio button, the value Y or N is sent upon clicking the radio button. When I go to my controller and do echo $_POST['vote']; it is always returning an empty value.
var value = $(this).val();
$.ajax( {
type: "POST",
url: url,
data: {'vote':value},
success: function(data){
alert(data);
}
})
Another place is when i do an ajax upload on a file using a form. On change of the upload field, it always return empty as well. So when I tried isset$_POST[Image] I am always getting a blank. It sees that all my file upload instance is blank.
So what would be the cause of these blanks? I also tried $_GET doesn't help much.
*****[EDIT]*********
<form id="vote-form" action="/some/path?r=shop/vote" method="post">
<input id="ytvote" type="hidden" value="" name="vote[hot_not]">
<span id="vote_hot_not">
<input class="fire-toggle" value="Y" type="radio" name="Vote[hot_not]" data-radio-fx="Vote[hot_not]" style="display: none;"><a title=" " data-radio-fx="Vote[hot_not]" class="radio-fx" href="#"><span class="radio"></span></a>
<label style="display:inline" for="Vote_hot_not_0"><img src=" /images/site/red-hot-not.png" id="hot-i"> </label>
<!--input class… fire-toggle-->
<input class="fire-toggle" value="N" type="radio" name="Vote[hot_not]" data-radio-fx="Vote[hot_not]" style="display: none;"><a data-radio-fx="Vote[hot_not]" class="radio-fx" href="#"><span class="radio"></span></a> <label style="display:inline" for="Vote_hot_not_1"><img src=” /images/site/hot-not.png" id="not-i"></label></span>
</form>
Now in my fire-toggle I have:
$(".fire-toggle").click(function() {
var value = $(this).val();
var url = "<?php echo Yii::app()->createUrl('shop/vote');?>";
$.ajax( {
type: "POST",
url: url,
data: {'vote':value},
success: function(data){
alert(data)
}
})
});
So value actually gives me the expected value..
and here in the controller... I simplified everything down to this:
$pid =Yii::app()->request->getQuery('id');
$uid = yii::app()->user->user_id;
$model = PrototypeReview::model()->getUserVote($uid, $pid);//also tried hard code numbers
$response = $_POST['vote']; //there's nothing in here!
echo $_POST['vote'];
I've been at this for hours, and i'm at a complete loss.... I've tried everything I can but the problem is that i'm not very familiar with Jquery, this is the first time I've ever used it.... Basically, i'm attempting to pass form data to a php script, and then return a variable which will contain the source code of a webpage.
Here is the jquery:
$("button").click(function(){
hi = $("#domain").serialize();
var page;
$.ajax({
type: "POST",
url: "webcrawler.php",
data: hi,
//dataType: "text",
success: function(data){
page = data;
document.write(page);
}
});
});
Here is the html it references:
<div id="contact_form">
<form name="contact" action="">
<fieldset>
<label for="domain" id="domain_label">Name</label>
<input type="text" name="domain" id="domain" size="30" value="" class="text-input" />
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</fieldset>
</form>
</div>
Here is the PHP that process it:
$search = $_POST["domain"];
if(!$fp = fopen($search,"r" )) {
return false;
}
fopen($search,"r" );
$data = "";
while(!feof($fp)) {
$data .= fgets($fp, 1024);
}
fclose($fp);
return $data;
?>
I think the variable $search is blank, but is that because i'm not sending it correctly with jquery or receiving it correctly with php? Thanks!
Well, when you serialize form data using jQuery, you should serialize the <form>, not the <input> field.
So try this:
$("button").click(function() {
var formData = $('form[name="contact"]').serialize();
var page;
$.ajax({
type: "POST",
url: "webcrawler.php",
data: formData,
success: function(data) {
page = data;
document.write(page);
}
});
});
See you have to do several things:
$("form[id='contact_form']").submit(function (e) {//<---instead click submit form
e.preventDefault(); //<----------------you have to stop the submit for ajax
Data = $(this).serialize(); //<----------$(this) is form here to serialize
var page;
$.ajax({
type: "POST",
url: "webcrawler.php",
data: Data,
success: function (data) {
page = data;
document.write(page);
}
});
});
So as in comments:
Submit form instead button click
Stop the form submission otherwise page will get refreshed.
$(this).serialize() is serializing the form here because here $(this) is the form itself.
Ok i have this code and i want to send this price from and price to to some other page when user clicks on submit but without refreshing page and without form.On other page i will use this inputs for sql query and display results.
Price from:<input type="text" name="from" id="from" width="50px" />
Price to:<input type="text" name="to" id="to" width="50px" />
<input type="submit" id="submit" value="Search"/>
using jquery
$('#submit').click(){
event.preventDefault();// prevent form from submitting
$.ajax({
type: 'POST',
url: 'targetpage.php', //your page here
data: {price_from:$('#from').val() , price_to: $('#to').val()},
success: function(response){
}
});
});
Hi you can achieve this using jQuery+Ajax, try this
$('#submit').click(function(event) {
var from = $('#from').val();
var to = $('#to').val();
$.ajax({
type: "POST",
url: "page_name.php",
data:"from="+from+"&to="+to,
success: function (msg) {
//some action
}
});
});
and in your php(page_name.php) file you can get the posted value and can do the necessary operation
$from = $_POST['from'];
$to = $_POST['to'];
var _from = $('#from').val();
var _to = $('#to').val();
$.post('your_file.php',{from: _form, to: _to},function(data){
//Do Something with returned data.
},'json');
'json' at the end could also be 'html' depending upon what you're returning.
I'm new to jQuery / AJAX.
I'm trying to send single input with jquery/ajax/php.
LIVE EXAMPLE
But, after pressing submit nothing is happening, where is my error?
Any help much appreciated.
HTML:
<form action="submit.php">
<input id="number" name="number" type="text" />
<input id="submit" name="submit" type="submit" />
</form>
JQUERY / AJAX:
$(document).ready(function(e) {
$('input#submit').click(function() {
var number = $('input[name=number]');
var data = 'number=' + number.val();
$.ajax({
url: "submit.php",
type: "GET",
data: data,
cache: false,
success: function(html) {
if (html == 1) {
alert('wyslane');
}
else {
alert('error');
}
}
});
return false;
});
});
PHP:
<?php
$mailTo = 'email#gmail.com';
$mailFrom = 'email#gmail.com';
$subject = 'Call Back';
$number = ($_GET['number']) ? $_GET['number'] : $_POST['number'];
mail($mailTo, $subject, $number, "From: ".$mailFrom);
?>
HTML:
<form id=submit action="">
<input id="number" name="number" type="text" />
<input name="submit" type="submit" />
</form>
The action URL is irrelevant as you want to submit your data via AJAX. Add the submit id to the form and override the default submit behavior, instead of overriding the onclick handler of the submit button. I'll explain in the JS section.
JS:
var number = $('input[name="number"]');
Quotes were missing.
$(document).ready(function(e) {
$('#submit').submit(function() {
var number = $('input[name=number]');
var data = 'number=' + number.val();
$.ajax({
url: "submit.php",
type: "GET",
data: data,
cache: false,
success: function(html) {
if (html == 1) {
alert('wyslane');
}
else {
alert('error');
}
}
});
return false;
});
});
I don't really understand your success callback, why do you expect that html should be equal to 1?
Atleast I got 404 error when pressed your submit button:
Not Found
The requested URL /index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
When you get it to work, remember to add mysql_real_escape_string function to avoid SQL injections http://php.net/manual/en/function.mysql-real-escape-string.php
Since you are also using ID for number, you could just use: var data = 'number=' + $('#number').val()
Also if you add ID to your form, you can use:
$('#formId').submit(function(){
});
instead of that click. This function will launch when that form is submitted. This is better way because users can submit the form with other ways aswell than just clicking the submit button (enter).
var number = $('input[name=number]');
is wrong. It's
var number = $('input[name="number"]');