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 8 years ago.
Improve this question
I'm trying to send a form using jQuery and it's not working. This code with a <a> tag works fine, but inside a form using the submit button doesn't.
$('#send-comment').click(function(){
var id_parent = $('#id_parent').val();
var comment_text = $('#comment-box').val();
var user_id = <?php echo $_SESSION['log_id']; ?>;
var new_id = <?php echo $id_new; ?>;
$.post( "dn-functions.php", { func: "registerComment", id_parent: id_parent, comment_text: comment_text, user_id: user_id, new_id: new_id })
.done(function( data ) {
alert( data );
});
});
Do you know what am I doing wrong?
$('#send-comment').click(function(e){
e.preventDefault();
var id_parent = $('#id_parent').val();
var comment_text = $('#comment-box').val();
var user_id = <?php echo $_SESSION['log_id']; ?>;
var new_id = <?php echo $id_new; ?>;
$.post( "dn-functions.php", { func: "registerComment", id_parent: id_parent, comment_text: comment_text, user_id: user_id, new_id: new_id })
.done(function( data ) {
alert( data );
});
});
I'm pretty sure you are submitting the form and your request is not being sent since you're leaving the page on form submit.
So pass the event and prevent the default action of form submit.
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 4 years ago.
Improve this question
I've tried to look up different ways to get this json to output correctly but im not sure if im accessing the right variable in php/or the success function value.pTitle as well as how to I get the access to the other value out such as artTitle im failing somewhere not sure where or why.UPDATE fixed the php file added and an array $data[].
this is my php code.
$sqlPAQuery = "SELECT pTitle, GROUP_CONCAT(artTitle) AS
artTitle
FROM p
JOIN art ON art.pId = p.pId
GROUP BY pTitle";
if ($result=mysqli_query($conn,$sqlPAQuery))
{
$data = [];
while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$data[] = $row;
}
echo json_encode($data);
}
This is the outcome from the php encode of row:
[{"pTitle":"ent","artTitle":"11,12"},{"pTitle":"pro","artTitle":"10"},{"pTitle":"sports","artTitle":"1,13"}]
This is the html code:
<h3>Output: </h3>
<div id="output"></div>
<script id="source" language="javascript" type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: 'Data.php',
data: "",
dataType: 'json',
success: function(data)
{
$.each(data, function(index, value) {
var pageTitle = value.pTitle; //get name
$('#output').append("<b>pageTitle: </b>"+pageTitle+"<br/>");
}
});
});
Output should be:
pageTitle: ent
pageTitle: pro
pageTitle: sport
FIXED THE PHP FILE WORKS
If you want to proces the json like that this is what you need to do:
if ($result=mysqli_query($conn,$sqlPAQuery)) {
$data = [];
while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$data[] = $row;
}
echo json_encode($data);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a php page called posts.php, this page just select all from a database table named posts and display them. Down the page I have a textarea named post. When I press the submit button the AJAX script will send the text from textarea to a page called insert.php. This insert.php gets the submited text and insert it into posts table. How can I do this and how can I upload the posts.php when I have inserted a post into posts table.
Example:
User 123 writes a message in the textarea. He presses submit button. AJAX sends the post to insert.php. Insert.php inserts the post into posts tabel. Posts.php shows the message.
You can do it very easily! Here's a very simple example.
func.js
$(document).on("click", ".submitBtn", function() {
var textareaVal = $(".textarea").val();
$.ajax({
type: "POST",
url: "insert.php",
dataType: "text",
data: { postVar: textareaVal },
success: function(data) {
$(".resultDiv").load("posts.php");
}
});
});
insert.php
// You have to pay attention to AJAX can only invite your file!
if (!empty($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") {
if (isset($_POST["postVar"])) {
$var = sanitize($_POST["postVar"]); // You need sanitize the data!
// Insert into your table...
}
}
In your posts.php, SELECT datas from your table, but only select the data, without „HTML container”! Your JS file load this datas on your resultDiv box.
I'm not certain why you would need to do this via ajax, but:
$( "#myForm" ).submit(function( event ) {
$.ajax({
type: 'post',
url: '../insert.php' ,
data: { value1: document.getElementById('textarea').value },
success: function () {
alert("yay!");
}
});
});
I tried to find help via the search function on here but all the answers given to similar problems were too elaborate for me to understand, i.e. the example code was too complex for me to extract the parts which could have been relevant for my problem :(
I have a html form which sends userinput on a specific row in a datatable via an ajax-request to a php file, where the input gets inserted into my sqldb.
I have no problem sending the textinput entered by a user and also transferring additional infos like the specific row they were on, or the network account of the user. But i now want to add a checkbox, so the users can choose whether their comment is private or public. However i somehow cannot transmit the value from the checkbox, there is no error but also no checkboxdata inserted into the db.
Do i have to handle checkboxes differently than textareas? I'd be very grateful for help!
My code looks as follows:
Html:
function insertTextarea() {
var boardInfo = $( "<form id='boardComment'><textarea rows='2' cols='30'>Notizen? Fragen? Kommentare?</textarea>Privat:<input type='checkbox' name='privatcheckbox' value='private'><input type='submit' value='Submit'><input type='reset' value='Cancel'></form>");
$( this ).parent().append(boardInfo);
$("tbody img").hide();
$("#boardComment").on( "submit", function( event ) {
event.preventDefault();
var change_id = {};
change_id['id'] = $(this).parent().attr("id");
change_id['comment'] = $(this).find("textarea").val();
change_id['privatecheckbox'] = $(this).find("checkbox").val();
if( $(this).find("textarea").val() ) {
$.ajax({
type: "POST",
url: "boardinfo.php",
cache: false,
data: change_id,
success: function( response2 ) {
alert("Your comment has been saved!");
$("tbody img").show();
$("#" + change_id['id']).find("form").remove();
}
});
};
});
and this is the php part:
$id = mysql_real_escape_string($_POST['id']);
$comment = mysql_real_escape_string($_POST['comment']);
$privatecheckbox = mysql_real_escape_string($_POST['privatecheckbox']);
$sql="INSERT INTO cerberus_board_info (BOARD_INFO_COMMENTS, BOARD_INFO_USER, BOARD_INFO_CHANGE_ID, BOARD_INFO_ENTRY_CHANNEL, BOARD_INFO_PRIVACY_LEVEL) VALUES ('$comment', '$ldapdata', '$id', 'Portal', '$privatecheckbox')";
The following line:
change_id['privatecheckbox'] = $(this).find("checkbox").val();
Searches for a element with the tagname checkbox. Such an element doesn't exist, I believe you are trying to search for an <input> element with a type of checkbox.
The following should work for you:
change_id['privatecheckbox'] = $(this).find("input[type=checkbox]").val();
Or even better, the :checkbox pseudo selector:
change_id['privatecheckbox'] = $(this).find(":checkbox").val();
On a final note: Why shouldn't I use mysql_* functions in PHP?
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 8 years ago.
Improve this question
I works on local but does not work on live server
Yii::app()->clientScript->registerScript('users', "
var user_id = $('#uId').val();
$.ajax({
url:'user/personalInfo'
,type:'get'
,dataType:'json'
,data:{id:user_id}
,success:function(data){
//append to popup window
}
});
");
Try using Yii::app()->createUrl('user/personalInfo') fro creating the url
Yii::app()->clientScript->registerScript('users', "
var user_id = $('#uId').val();
$.ajax({
url:'" . Yii::app()->createUrl('user/personalInfo') . "'
,type:'get'
,dataType:'json'
,data:{id:user_id}
,success:function(data){
//append to popup window
}
});
try by this way
Yii::app()->clientScript->registerScript('users', "
var use_id = $('#uId').val();
var url = window.location.href.split('/');
var base_url = url[0] + '//' + url[2] + '/';
$.ajax({
url:base_url + 'user/personalInfo'
,type:'get'
,dataType:'json'
,data:{id:user_id}
,success:function(data){
//append to popup window
}
});
");
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 8 years ago.
Improve this question
I am learning jquery. I wrote an script but it is not working
1 My HTML
<ul id="recur" class="elasticstack">
<li id=<?=$item['id']?>>.......</li>
....................................
</ul>
2 My jquery function
function viewDetails()
{
$('#recur>li').live(function(){
var id = $(this).attr('id');
$.ajax({
url:'myUrl'
,type:'post'
,dataType:'json'
,data:{propertyId:id}
,success:function(data){
console.log(data);
}
});
});
}
where is my error? Thanks all!
I think you have more id "recur" in same html.
Check your jquery library version for live function. you can use 1.8 version of library
Firstly, you've omitted the event you want handled for each <li> element. Let's assume click for now...
If you're using jQuery > 1.9, you cannot use .live(). Instead, use .on() with event delegation
$('#recur').on('click', 'li', function(e) {
var id = this.id;
$.ajax({
url: 'myUrl',
type: 'POST',
dataType: 'json',
data: {propertyId: id}
}).done(function(data) {
console.log(data);
});
});
You are missing the quotes in the id. Try this:
<ul id="recur" class="elasticstack">
<li id="<?=$item['id']?>">.......</li>
</ul>
You cant get the id because isn't there.
A better way to do the jQuery part is:
function viewDetails(){
$('#recur').find('li).each(function(){
$(this).on('click', function(){
var id = $(this).attr('id');
$.ajax({
url:'myUrl'
,type:'post'
,dataType:'json'
,data:{propertyId:id}
,success:function(data){
console.log(data);
}
});
});
});
}
Don't use the .live() method. Is deprecated now. And with the .find() method you have better performance.
If you are dinamicaly adding the items and want to display the data every time an item is added, you can use the livequery.js plugin, and do like this:
function viewDetails(){
$('#recur li').livequery(function(){
var id = $(this).attr('id');
$.ajax({
url:'myUrl'
,type:'post'
,dataType:'json'
,data:{propertyId:id}
,success:function(data){
console.log(data);
}
});
});
}
in HTML
<ul id="recur" class="elasticstack">
<li id="<?=$item['id']?>" >.......</li>
....................................
</ul>
In Ajax function
data:[{name:'propertyId', value:id}]