form method post and jquery - javascript

I'm doing a cakephp form in my view and clicking on the button, I would like to evaluate values before making a certain action, the problem is not me, in another view I have exactly the same code and if it works, but here , do not. However, I do not have any error console.
echo $this->Form->create('Detalle', array('class'=>'form_center', 'type'=>'file','id'=>'detalle_form'));
echo $this->Form->input('FechaEvento', array('type'=>'hidden','value'=>$fecha_evento));
if($SuperAdmin and !$evento['Evento']['gratuito']){
echo $this->Form->input('Inscripcion.cortesia', array('type'=>'checkbox', 'value'=>1, 'label' => utf8_encode('Cortesía'), 'div' => 'input checkbox inputRutPasaporte'));
echo $this->Form->input('Inscripcion.boleta_no_generar', array('type' => 'hidden', 'value' => 0));
}
$options_btn = array('label' => __('Pagar'), 'class' => 'btn_blue', 'div' => false);
echo $this->Form->end($options_btn);
javascript :
<script type="text/javascript">
$(document).ready(function(){
<? if($evento['Evento']['generar_boleta']){ ?>
<? if($evento['Evento']['habilitar_compra_multiple']){ ?>
$('#detalle_form').data('callback', function(form){
alert("TEST");
});
<? } ?>
<? } ?>
});
</script>

The code looks fine but the point is we need information on your control flow. I assume there are missing variables due to it being different.
I advise you to
combine the code into a CakePHP element that is called in the view so you write the code only once (keeping your code DRY)
set DebugLevel to 2
install CakePHP DebugKit DebugKit on Github
check for missing variables and errors
if the problem persists, give us more information accordingly

Related

Updating and passing a value in PHP

I want to make a bundle creator in Wordpress using Woocommerce, where you select 1 of 4 T-shirts + 1 of 4 pairs of socks and they get added to the cart. Currently I am stuck on figuring out how to approach this. What I currently need to achieve is this:
There is a top image which corresponds to the currently selected product and three smaller images below that. Once you click the small image, it needs to change the top image. There is also a title on the bottom, which corresponds to the currently selected product, that changes together with the top image. You can see the intended result here.
I need to somehow get the ID of the product the user clicks on and pass it to other php functions. This is where I got stuck. Can anybody help me out?
The code should look something like this:
<div id="selected-product-image">
<?php get_the_post_thumbnail(/* ID of the currently selected product*/); ?>
</div>
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 4, 'product_cat' => 't-shirts', 'orderby' => 'name' );
$loop = new WP_Query( $args );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="product">
<div class="select-product"><!--This should have a function to capture the product ID on click. -->
<?php echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); ?>
</div>
</li>
<?php endwhile; ?>
<div id="selected-product-name">
<?php get_the_title(/* ID of the currently selected product*/) ?>;
</div>
<?php wp_reset_query(); ?>
</ul>
I understand that I can do something like this using AJAX, but I am not sure how to use the returned ID back in get_the_post_thumbnail() or get_the_title(). This is what I got:
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#select-product").click(function() {
var id = 29; /* Any value for testing */
jQuery.ajax({
method: "post",
url: "/test.php",
data: {
productID: id
}
})
.done(function(data) {
alert(data);
/* How do I use this data to update the picture/title? */
});
});
});
</script>
<!-- THE test.php FILE -->
<?php
$productID = $_POST['productID'];
echo $productID;
?>
UPDATE:
I have tried editing the test.php to echo a function, but I am getting a 500 error every time I try using a Wordpress function inside the test.php file. I tried including the wp-blog-header.php file so the functions can run, but it still doesn't help. What am I doing wrong?
<!-- THE test.php FILE -->
<?php
include_once('wp-blog-header.php');
$productID = $_POST['productID'];
echo get_the_post_thumbnail($productID);
?>

How to pass extra data content on yii activeform's ajax parameters?

I am trying to set up a ajax request when radio button is clicked. Here is my code that I am trying for.
<?php echo $form->dropDownListRow($model, 'page', $pages, array('class' => 'span5', 'prompt' => '-- page --')); ?>
<?php echo $form->radioButtonListInlineRow($model, 'for', array('desktop'=>'Desktop', 'mobile'=>'Mobile'), array('ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/admin/adv/loadpositions'), //or $this->createUrl('loadcities') if '$this' extends CController
'update'=>'#Adv_position', //or 'success' => 'function(data){...handle the data in the way you want...}',
'data'=>array('for'=>'js:this.value', 'page' => 'XXXXXXXX'),
)));
?>
I just want to pass its's value as well as it's upper field's value to the ajax action. How can I make it come true. 'data'=>array('for'=>'js:this.value', 'page' => 'XXXXXXXX'),.
Thanks in advance.
$(this.form).serialize()
Will give you the form data. Try something like:
<?php
echo $form->radioButtonList($model, 'for', array('desktop'=>'Desktop', 'mobile'=>'Mobile'), array('ajax' => array(
'type'=>'POST',
'url'=>Yii::app()->createUrl('/admin/adv/loadpositions'),
'data'=>array('data'=>'js:$(this.form).serialize()'),
)));
?>
Update:
You can easily parse this serialized string using PHP function parse_str
<?php
$data = 'str1=value1&str2=value2';
parse_str($data);
echo $str1; //value1
echo $str2; //value2
Just change your data attribute to something like this:
'data'=>array('for'=>'js:this.value', 'page' => 'js:$("#someId").val()'),

AJAX GET simple PHP Multiple variables

I need a simple way to retrieve multiple PHP variables into html divs. I searched a lot of posts but I can't found an answer.
I am looking for something like this:
go-to-index.php
<?php
$name = 'Jonh';
$phone = '123456789';
$details = 'Detail about';
?>
index.php
<div class="name">Your Name is : <?php echo $name; ?></div>
<div class="phone">Your Phone Number is : <?php echo $phone; ?></div>
<div class="details">Your Details are : <?php echo $details; ?></div>
I want instead of echo to get them via AJAX Call.
What is the correct AJAX REQUEST syntax to do that?
UPDATE
My bad I do not noticed before but forgot to say I also need to load the calls one by one. I have too many requests and take a lot of time.
May the query .each() function should work like I want?
In your PHP:
<?php
echo json_encode(Array(
'name' => "John",
'phone' => "1234567890",
'details' => "Details about..."
));
Your HTML:
<div class="name">Your Name is : <span class="name_value"></span></div>
<div class="phone">Your Phone Number is : <span class="phone_value"></span></div>
<div class="details">Your Details are : <span class="details_value"></span></div>
Your jQuery:
$(document).ready(function(){
$.getJSON('user-info.php',function(data){
$(".name_value").html(data.name);
$(".phone_value").html(data.phone);
$(".details_value").html(data.details);
});
});
Note: you'll set the user-info.php string to the URL (relative or absolute) of your PHP script that grabs the user info.
You need a PHP script that will output JSON containing the values you want, and you need a Javascript handler to ask for that data and do something when it gets it. Here's an example:
# File: go-to-index.php
<?php
$name = 'Jonh';
$phone = '123456789';
$details = 'Detail about';
echo json_encode(
[
'name' => $name,
'phone' => $phone,
'details' => $details
]
);
Then your HTML page:
<!-- File: index.php -->
<div class="name">Your Name is : <span class="container"></span></div>
<div class="phone">Your Phone Number is : <span class="container"></span></div>
<div class="details">Your Details are : <span class="container"></span></div>
<button class="loadMe" type="button">Click here to make things work</button>
And finally your jQuery:
$(document).ready(function() {
$('.loadMe').click(function() {
$.ajax({
// Path to your backend handler script
url: 'go-to-index.php';
// Tell jQuery that you expect JSON response
dataType: 'json',
// Define what should happen once the data is received
success: function (result) {
$('.name .container').html(result.name);
$('.phone .container').html(result.phone);
$('.details .container').html(result.details);
},
// Handle errors in retrieving the data
error: function (result) {
alert('Your AJAX request didn\'t work. Debug time!');
}
});
});
});
You can do this on any event - the button was just an example. You can also use plain Javascript or any other library, just used jQuery since you tagged it in your question.

Jquery flickering when I add classes to my divs

I'm having trouble with Jquery and flickering. My problem is this, when I convert my id's to classes the below syntax isn't working. The problem is, it flickers. It flickers approx. 7-8 times. What am I doing wrong? Any help would be appreciated. Thanks everyone.
I would also like to add when I change my classes to ID's everything works great but only one item on my webpage has the ability of hide>click>slideToggle (which isn't what I want because I'm listing more than one item per page for sale). This syntax is below as well, it's very similar to the syntax that does not work but I've decided to include it anyway.
(Not working syntax. Has classes)
<?php
echo "<div class=\"fmv2\">Your Name</div>";
?>
<div class="p122">
<?php
echo form_open("submit/submit_info");
echo form_label('Your Name:','name');
$data = array(
"name" => 'name',
"id" => 'box_width',
"value" => set_value('name')
);
echo form_input($data);
echo '<br>';
echo form_submit('Submit','Submit');
echo form_close();
?>
</div>
My Jquery (Not working, has classes)
// JavaScript Document
$(document).ready(function(){
$(".p122").hide(function(){
$(".fmv2").click(function() {
$(".p122").slideToggle(300);
});
});
});
Working syntax (Has Ids)
<?php
echo "<div id=\"fmv2\">Your Name</div>";
?>
<div id="p122">
<?php
echo form_open("submit/submit_info");
echo form_label('Your Name:','name');
$data = array(
"name" => 'name',
"id" => 'box_width',
"value" => set_value('name')
);
echo form_input($data);
echo '<br>';
echo form_submit('Submit','Submit');
echo form_close();
?>
</div>
My Jquery. (Working, has Ids)
// JavaScript Document
$(document).ready(function(){
$("#p122").hide(function(){
$("#fmv2").click(function() {
$("#p122").slideToggle(300);
});
});
});
I figured out my problem. I've been playing around with the syntax and the below code works.
$(function() { // Shorthand for $(document).ready(function() {
$('div.p122').hide();
$('div.fmv2').click(function() {
$(this).next('div.p122').slideToggle(300);
});
});

yii framework CActiveform dependent dropdown won't work inside pjax'ed content

here's my dependent dropdown php code:
<?php $find= new SomeModule;
$form=$this->beginWidget('CActiveForm', array(
'action'=> Yii::app()->createUrl('someurl'),
'method'=>'post',
'id'=>'filter-id',
'enableAjaxValidation'=>true,
)); ?>
<?php
echo $form->dropDownList($find,'category',
CHtml::listData(BaseCategory::model()->findAll(array(
'order' => 'name',
'condition'=>'type=:postID',
'params'=>array(':postID'=>1),
)), 'id', 'name'),
array(
'prompt'=>'Select Category',
'ajax'=>array(
'type'=>'POST',
'url'=>CController::createUrl('someurl/searchCategory'),
'update'=>'#'.CHtml::activeId($find,'category2')
),
)
);
?>
<?php
echo $form->dropDownList($find,'category2', array(), array(
'prompt'=>'Select Sub Category',
)
);
?>
I need to place this php code inside pjax content. Working fine when the page refreshed. But when it's loaded using pjax, this dependent dropdown won't work.
question:
How to make this dependent dropdown working when its loaded using pjax? What code should I add in:
$(document)
.on('pjax:success', function() {
})
to make this dependent dropdown works? Is there any script to "reload" this php code, so the dependent dropdown works? Or is there another solution?

Categories

Resources