Opening jQuery Dialog with Post Data - php

I am trying to open a jQuery dialog programmatically. I am able to open the dialog but I am not able to send post data to the dialog.
Here is the code.
HTML:
<div data-role="page" id="rename-dialog">
<?php
$current_group = $_POST['gr_name'];
$current_code = $_POST['gr_code'];
$current_id = $_POST['in_id'];
?>
<div data-role="header" data-theme="d" class="ui-header ui-bar-d" role="banner">
<a href="#main-page" class="ui-btn-left ui-btn ui-shadow ui-btn-corner-all ui-btn-icon-notext ui-btn-up-d" data-icon="delete" data-iconpos="notext" data-corners="true" data-shadow="true" data-iconshadow="true" data-wrapperels="span" data-theme="d" title="Close" ktbt_p_pass="1">
<span class="ui-btn-inner">
<span class="ui-icon ui-icon-delete ui-icon-shadow"> </span>
</span>
</a>
<h1 class="ui-title" role="heading" aria-level="1">Rename <?php echo $current_group; ?></h1>
</div>
<div data-role='content'>
<form id='rename-group-form' action='' method='post'>
New name for <?php echo $current_group; ?>: <input type='text' name='new-group-name'><br>
<input id='rename-submit' type='submit' data-theme='b' value='Rename'></input>
</form>
</div>
</div>
JAVASCRIPT:
var g_code = parseInt($(this).attr("id"));
var i_id = parseInt($(this).attr("value"));
var g_name = $(this).attr("text");
$.mobile.changePage( "#rename-dialog", {transition: 'slideDown', role: 'dialog'}, {
type: "post",
data: {gr_code: 'test', in_id: 'test', gr_name: 'test'}
});
Basically this opens the dialog but I get an error that the indexes g_code, in_in and gr_name are undefined.
What am I doing wrong?

Assuming that all of the pertinent code is shown in your question ...
Since you have the code that gets the values for g_code, i_id, and g_name outside of any block , $(this) is not defined. For instance, in this case:
$("#item").click(function() {
var item_id = $(this).attr("id");
});
$(this) would be a reference to the DOM element with an id of "item", because the selector was "#item".
It is likely that, if the code from you question is not inside of a selector block, that it needs to be moved into one, or that you need to replace 'this' with an actual selector to grab the DOM element you want.
If none of these answers apply, please post more of the code to give a better context of what's happening.

Related

get value from php tag by ajax

i want to get images that i fetched from database in my page and show them again in div with id test by ajax i want to show fetched datas again in div with test id when i click on button with addCart class . i think i haveto get value of button that i set it to ids of products in my datbase . and my problem is how can i get id s in ajax code?
$(document).ready(function(){
$(".addCart").click(function(){
$.post(".addCart.val()",function(data){
$("#test").html(data).show();
});
});
});
<div id="test" style="background-color: grey"></div>
<?php while ($row = mysqli_fetch_array($products)):?>
<div class="span3">
<div class="product">
<div class="product-img">
<div class="picture">
<img src="images/<?php echo $row['image'] ?>" alt="" width="540" height="374" />
<div class="img-overlay">
توضیحات بیشتر
<button value="<?php echo $row['id']?>" class="btn buy btn-danger addCart">اضافه به سبد خرید</button>
</div>
</div>
</div>
<div class="main-titles no-margin">
<h4 class="title"><?php echo $row['price'] ?></h4>
<!--<h5 class="no-margin">محصول ویژه 434</h5>-->
</div>
<p class="desc"><?php echo $row['name']?></p>
<p class="center-align stars">
<span class="icon-star stars-clr"></span>
<span class="icon-star stars-clr"></span>
<span class="icon-star"></span>
<span class="icon-star"></span>
<span class="icon-star"></span>
</p>
</div>
</div> <!-- /product -->
<?php endwhile;?>
$(".addCart").click(function(event){
var id = event.target.val();
});
This will get an value of your button element. But is this id is a valid url to make a ajax call? I dont think so. Please checkout Jquery Docs before returning to work on this part of functionality
in your code you are not passing correct value to php file
please do correction in your code
$(document).ready(function(){
$(".addCart").click(function(){
var jq = $(this);
var cart_id = jq.val(); /* get cart value */
$.post("ajax.php",{cart_id:cart_id},function(data){
$("#test").html(data).show();
});
});
});
and fetch cart_id in php ajax.php file like this
<?php
if(isset($_POST['cart_id'])){
/* put your code here */
}
?>

Change input type number value using jquery

All the other attribute changes seem to work fine inside the modal. The rating button inside the modal doesn't load its value on loading. But the inspect element seems to display the proper rating value.
I am new to php and jquery. Kindly excuse me from silly programming methodologies implemented in the above mentioned code.
Any help would be deeply appreciated.
HTML :
<div class="item" style="width: 400px; height: 230px">
<!-- Link trigger modal -->
<a data-img-url="upload/<?php echo $row['image_name'] ?>" data-toggle=
"modal" href="#myModal" id="<?php echo $row['image_id'] ?>" onclick=
"modalload(this.id, '<?php echo $row['image_title'] ?>' , '<?php echo $row['image_description'] ?>', <?php echo $TAVG ?> );"><img class="img-responsive carousal_scale"
src="upload/%3C?php%20echo%20$row['image_name']%20?%3E"></a>
<div class="carousel-caption">
<p><?php echo $row['image_title'] ?></p>
</div>
</div><!-- Modal -->
<div class="modal fade modal-dialog modal-content" id="myModal" tabindex="-1">
<div class="modal-header">
<button class="close" data-dismiss="modal" type=
"button"><span>×</span><span class="sr-only">Close</span></button>
<h2 class="modal-title" id="myModalLabel"></h2>
</div>
<div class="modal-body well">
<img class="img-responsive carousal_scale" src=""> <input class="rating"
data-size="xs" id="" max="5" min="0" onchange="test(this.id, this.value);"
step="0.1" style="alignment-adjust: auto;" type="number" value="">
</div>
<div>
<p style="text-align: center; font-size: 15px;"></p>
</div>
<div class="modal-footer">
<button class="btn btn-default" data-dismiss="modal" type=
"button">Close</button>
</div>
</div>
JS :
$(document).ready(function() {
$(".rating-kv").rating();
});
function modalload(id, title, description, avg) {
alert(avg);
// alert(description);
$('#myModal img').attr('src', $('#' + id).attr('data-img-url'));
$('#myModal input').attr('value', avg);
$('#myModal input').attr('id', id);
$('#myModal h2').text(title);
$('#myModal p').text(description);
}
function test(id, val) {
var pic_id = id;
var pic_val = val;
$.ajax({
type: 'POST',
url: 'rating.php',
data: {
'pic_id': pic_id,
'pic_val': pic_val
},
}).done(function(data) {
// alert(data);
});
}
"The rating button inside the modal doesn't load its value on loading." You currently have your modalload()function only activate on click of an a tag so your rating isn't going to get it's value/id populated on load, but rather on click.
Try this instead inside your modalload() function,
$('.rating').attr('value', avg);
$('.rating').attr('id', id);
Also, note that there is no jQuery rating() function, and that you have no element with the class rating-kv in your HTML, therefore the below code will do nothing.
$(".rating-kv").rating();
In other words, you have a lot of bad coding practices and incomplete code. You should consider revising what you have and re posting it.

Ajax how to disable div or item

I am new to ajax, is there a way you can help me, how to disable all items once click is successful:
Here is my code for ajax:
if (!parent.hasClass('.disabled')) {
// vote up action
if (action == 'click') {
alert("test");
};
//how do i add disabled function on a particular div
// add disabled class with .item
parent.addClass('.disabled');
};
here is my index:
<?php while($row = mysql_fetch_array($query)): ?>
<div class="item" data-postid="<?php echo $row['recipe_id'] ?>" data-score="<?php echo $row['vote'] ?>">
<div class="vote-span"><!-- voting-->
<div class="vote" data-action="up" title="Vote up">
<i class="icon-chevron-up"></i>
</div><!--vote up-->
<div class="vote-score"><?php echo $row['vote'] ?></div>
</div>
<div class="post"><!-- post data -->
<p><?php echo $row['recipe_title'] ?></p>
</div>
</div><!--item-->
i jst want to disable the loop icon-chevron-up class.not just one but all.
Actually no ajax call needed here. I will only be done by using jquery. See the code
$(document).ready(function(){
$('.item').click(function(){
if (!parent.hasClass('.disabled')) {
parent.addClass('.disabled');
}
});
});
Here you have not mentioned, on what click you need the action, so i consider that on the div contains class='item', the action will be performed. I hope it will help.

SimpleModal Foreach Loop Dynamic Content

How would you use simplemodal within a foreach loop so that it does not repeat the same information?
Example:
foreach($ret as $v)
{
<div id='osx-modal'>
<input type='button' name='osx' value='View' class='osx demo'/>
</div>
<div id="osx-modal-content">
<div class="close">x</div>
<div id="osx-modal-data">
<div id="toon_box">
<div id="toon_name">
<?php echo $v['toon_name'];?>
</div>
<div id="toon_avatar">
<?php echo '<img src="' . $v['avatar'] . '" alt="" />';?>
</div>
</div>
</div>
}
The meta data is pulled from users on the website and displays that meta data for each user in separate Modals. For clarification let's say foreach is repeated 10x since here is 10 users in the database. That will create 10 separate buttons for each user, that when you click the modal box pops up with the data inside of it.
The problem is, that when you click each button its the same user in each modal box. How would you set this up so each modal box displays the correct user meta data. I've already checked if persistence is set to false in simplemodal.js and it is.
The reason you are having the same meta data in the same model box is because this line of code
<input type='button' name='osx' value='View' class='osx demo'/>
using class is very generic so you should be using id instead where each button has its own uniqid
<input type='button' name='osx' value='View' id='os1'/>
<input type='button' name='osx' value='View' id='os2'/>
Now your <div id="osx-modal-content"> would also look like this
<div id="osx-modal-content1">
<div id="osx-modal-content2">
Your javascript would look like this
$("os1").addEvent("click", function(){
var SM = new SimpleModal();
SM.show({
"title":"Title",
"contents": $("#osx-modal-content1").html()
});
});
$("os2").addEvent("click", function(){
var SM = new SimpleModal();
SM.show({
"title":"Title",
"contents": $("#osx-modal-content2").html()
});
});
Can use see the way everything is mapped ??? this is who you achieve each content in each model
Let me know if you need more information
Edit 1 Example
<?php
$x = 0;
foreach ( $ret as $v ) {
$x ++;
$content = "
<script type=\"text/javascript\">addModal($x)</script>
<div id='osx-modal'>
<input type='button' name='osx' value='View' id='osx{$x}'/>
</div>
<div id=\"osx-modal-content{$x}\">
<div class=\"close\">x</div>
<div id=\"osx-modal-data\">
<div id=\"toon_box\">
<div id=\"toon_name\">
{$v['toon_name']}
</div>
<div id=\"toon_avatar\">
<img src=\"{$v['avatar']}\" alt=\"\" />
</div>
</div>
</div>";
}
?>
<script type="text/javascript">
function addModal(x)
{
$("osx" + x).addEvent("click", function(){
var SM = new SimpleModal();
SM.show({
"title":"Title",
"contents": $("#osx-modal-content" + x).html()
});
});
}
</script>
Finally got it to work thanks to standup75!
jQuery(function ($) {
var OSX = {
container: null,
init: function () {
$("[name=osx]").click(function (e) {
e.preventDefault();
$(this).parent().next().modal({
overlayId: 'osx-overlay',
containerId: 'osx-container',
closeHTML: null,
minHeight: 80,
opacity: 65,
position: ['0',],
overlayClose: true,
onOpen: OSX.open,
onClose: OSX.close
});
});
},

Jquery AJAX custom form not returning results from PHP

I don't like the look of normal dropdown selectors in forms, so I've decided to create my own version of them with some Jquery as a little learning project for myself.
The objective of the following code is to be able to submit the text inside the various "select" boxes, i.e. #cuisineSelect, #locationSelect and #priceSelect to "ajax.php" so that I can manipulate those variables further.
My problem is that for some strange reason ajax.php will not register the $_POST variable and it's telling me that I've got an "undefined index".
I did some debugging in IE9 with some developer tools and they show (at least I think so) that the variables are getting sent through to ajax.php, and ajax.php is returning a variable. Here are the screenshots.
On the real project, the user can click on the <li> options item and the text will transfer onto the selector - just like a dropdown selector.
Here is my code:
The HTML:
<div id="parentContainer" style="width:100%;">
<div class="container">
<div class="select" id="cuisineSelect">
Cuisine
</div>
<div class="option" id="cuisineOption">
<ul id="cuisineul">
<li>Asian</li>
<li>American</li>
<li>Indian</li>
<li>Fusion</li>
</ul>
</div>
</div>
<div class="container">
<div class="select" id="locationSelect">
Location
</div>
<div class="option" id="locationOption">
<ul id="locationul">
<li>Asian</li>
<li>American</li>
<li>Indian</li>
<li>Fusion</li>
</ul>
</div>
</div>
<div class="container">
<div class="select" id="priceSelect">
Price
</div>
<div class="option" id="priceOption">
<ul id="priceul">
<li>Price Range</li>
<li>Cheap ($5-$15)</li>
<li>Medium ($16 - $20)</li>
<li>Pricey ($21 - $35)</li>
<li>Fine Dining ($35+)</li>
</ul>
</div>
</div>
<div class="container">
<div class="select" id="searchButton" style="width:25px; height:20px;">
<center><img align="center" src="images/sml_search.png" width="17" height="18" /></center>
</div>
</div>
</div>
<div id="result" style="z-index:10;">
result=show;
<?php include "ajax.php"; ?>
</div>
The Jquery code (this has already been placed inside a $(document).ready function.)
$("#searchButton").click(function() {
/*var cuisine = $("#cuisineSelect").html();
var location = $("#locationSelect").attr('value');
var price = $("#priceSelect").attr('value');
$.ajax({
type: "POST",
url: "ajax.php",
data: "cuisine="+ cuisine +"& location="+ location +"& price="+ price,
success: function(){
$('#result').show();
}
});
return false; */
$.post('ajax.php', 'cuisine=' + $("#cuisineSelect").text(), function () {
$("#result").show();
});
});
The PHP page "ajax.php"
<?php
//Search information
//$cuisine = htmlspecialchars(trim($_GET['cuisine']));
//$location = htmlspecialchars(trim($_POST['location']));
//$price = htmlspecialchars(trim($_POST['price']));
//echo $location;
//echo $price;
//$addClient = "INSERT INTO clients (fname,lname) VALUES ('$fname','$lname')";
//mysql_query($addClient) or die(mysql_error());
//$value = $_POST['cuisine'];
//$value2 = $_POST['val'];
//echo "$value2";
$cuisine = $_POST['cuisine'];
echo $cuisine;
?>
Thanks very much for your help.
There is no need to write include the ajax.php file in #result div. Keep #result div empty and hidden.
Modify your ajax call as below:
$.post('ajax.php', 'cuisine=' + $("#cuisineSelect").html(), function (data) {
$("#result").html(data);
$("#result").show();
});
This will show the clicked text in #result 'div'. variable data is passed to callback function for further utilization.
To convert the < li > to a select box, you have to pass the values present in the div #cuisineOption to ajax.php so your PHP script can create the select box from these values.

Categories