Passing a value from jquery to php to another view - php

I am getting the value of a checkbox using jquery. How can I pass that value in another view? I want to use it to access data from the DB. This is how far I've got.
Script
<script type="text/javascript" charset="utf-8">
$(function(){
$('#btnClick').click(function(){
var val = [];
$(':checkbox:checked').each(function(i){
val[i] = $(this).val();
});
});
});
//SOME CODE
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'submission-form',
'enableAjaxValidation'=>false,
'action'=>Yii::app()->createUrl('submission/create'),
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
//SOME CODE
<?php echo CHtml::submitButton('Next', array('id'=>'btnClick','name'=>'btnClick',)); ?>

You can use ajax.
<script type="text/javascript" charset="utf-8">
$(function(){
$('#btnClick').click(function(){
var val = [];
$(':checkbox:checked').each(function(i){
val[i] = $(this).val();
$.ajax({
type: "POST",
url: "some.php",
data: { Value: val[i] }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
});
//SOME CODE
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'submission-form',
'enableAjaxValidation'=>false,
'action'=>Yii::app()->createUrl('submission/create'),
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
//SOME CODE
<?php echo CHtml::submitButton('Next', array('id'=>'btnClick','name'=>'btnClick',)); ?>
And In Some.php
Get that value in variable like
<?php $Selected_Value = $_POST['Value']; ?>
And Perform any database query here.

Related

PHP - get variable obtained by ajax

I used ajax to send to me a php variables in this way:
<script type="text/javascript">
$(document).ready(function(){
$("#disdiciButton").click(function(){
var log_user = <?php echo json_encode($log_user);?>;
alert(log_user);
$.ajax({
type: 'POST',
url: 'disdici.php',
data:log_user,
success: function(data) {
$("#deleteResponse").text(data);
}
});
});
});
</script>
Now I'm trying to collect this data in the php page disdici.php but I don't know how. I've tried in this way:
$log_user = "";
if(isset($_POST['data'])){
$log_user = json_decode(data);
}
echo 'user: '.$log_user;
but $log_user remain empty. How can I do?
You have to write your data like :
data : 'var1=' + value1 + '&var2=' + value2 + '&var3=' + value3...
So, you can get the variable in php :
$_POST['var1'] = value1;
...
In your example :
<script type="text/javascript">
$(document).ready(function(){
$("#disdiciButton").click(function(){
var log_user = <?php echo json_encode($log_user);?>;
alert(log_user);
$.ajax({
type: 'POST',
url: 'disdici.php',
data:'log_user=' + log_user,
success: function(data) {
$("#deleteResponse").text(data);
}
});
});
});
</script>
In PHP file, you get the value :
$_POST['log_user']

How can i load php file after sending ajax data

So I have 2 files file1.php with all the php and ajax, and file2.php with only php.
$_POST["there_id"] will be sent from file1.php page to file2.php through ajax.
file1.php code:
<div class="list_items">
<li><p>content here ...</p>
</div>
<div class="content_area">
//load ajax content here
</div>
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id}
});
});
file2.php code:
<div id="file2content>
<?php
$there_id = $_POST['there_id'];
$user_id = 5;
$fetch_data = regular_query("SELECT * FROM contents WHERE
(user_by = :me or user_by + :them) AND (user_to = :me or user_to = :them)",
["me" => $user_id, "them" = $there_id], $conn);
foreach ($fetch_data as $data) : ?>
<li><p>database data here</p>
<?php
endforeach;
?>
</div>
now what i want to do is when file2.php is done with php and list items are available i want to load file2contents to content_area on file1.php.
$(".box").on("click", function(e) {
e.preventDefault();
var there_id = $(this).attr("id");
$.ajax({
url: "indexnew.php",
type: "POST",
data: { there_id : there_id},
success: function(data) {
$('.content_area').html(data);
}
});
});
However, content_area should really be an id rather than a class so that it's unique.
Since you're just returning html, you can simplify it using the .load() function.
$('.box').on('click', function(e) {
e.preventDefault();
var there_id = $(this).attr('id');
$('.content_area').load('indexnew.php', {there_id: there_id});
});
success: function(data) { $(".content_area").html(data); }
adding the above to your AJAX call should do what you want if I understand you correctly.

Complex javascript object access in PHP POST var

I have a complex Javascript object which is sent to PHP server with $.ajax:
the object looks like:
var obj =
{
sellerId:"1234",
buyerId:"5432",
.
.
.
items:[{id:"11",qt:"3"},{id:"22",qt:"5"},{id:"33",qt:"8"}...]
};
jquery code looks something like this:
$.ajax({
type: "POST",
url: "php.php",
data: obj
}).done(function( msg ) {
alert( msg );
});
on PHP side the following code will echo the sellerId
<?php
echo $_POST['id'];
?>
My question is how do I access the items array and its
object properties? Thanks
hi you can retrieve the value using below code at your php file.
foreach($_POST['items'] as $val){
echo $val['id']."==".$val['qt'];
}
According to your question I slightly created some code for you to start(may be not avery good code but yes u can learn something from this) --
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
var path='getdata.php';
var obj = {
sellerId:"1234",
buyerId:"5432",
items:[{id:"11",qt:"3"},{id:"22",qt:"5"},{id:"33",qt:"8"}]
};
jQuery("#test").click(function(){
jQuery.ajax({
type: "GET",
url: path,
data: obj,
success:function(results)
{
jQuery('#venue').html(results);
}
});
});
});
</script>
<div id="test">This is my active div</div>
<div id="venue"></div>
PHP Code in getdata.php
<?php
foreach($_GET['items'] as $myitem )
{
foreach( $myitem as $key => $value )
{
echo "This is my key : ".$key." This is value of key ".$value."<br />";
}
}
?>
RESULT

I want to know how can I pass the a multiple values from php to jquery see the situation below

Example I have this line of code in PHP, and I want to pass these values when button is click and receive it by the jquery. How should I do this? I have started this codes and I don't know what is the next step on it.
PHP Code:
<?php
echo '<button>Get the values</button>';
$name = 'sample name';
$age = 'sample age';
$add = 'sample add';
?>
JQuery Code:
$(function() {
$('button').live('click', function() {
$.ajax({
url: 'recieve.php',
type: 'post',
data: { get_name: pass_name, get_age: pass_age, get_add: pass_add },
success:function(){
}
});
});
});
Could you not just get php to echo out the variables in the JS. Like this:
$(function() {
$('button').live('click', function() {
$.ajax({
url: 'recieve.php',
type: 'post',
data: { get_name: <?=$name ?>, get_age: <?=age ?>, get_add: <?=add ?> },
success:function(){
}
});
});
});
I prefer to print the PHP values in a JSON variable and then access the values directly in JS. In this manner you can put your JS code in a different file and no need to be parse by PHP interpreter.
<?php
$my_values = array(
'name' => 'Sample name',
'age' => 'Sample age',
'add' => 'Sample add',
);
?>
<html>
<head>
<script type="text/javascript">
var my_values = <?php print json_encode($my_values); ?>;
$(document).ready(function() {
$('button').live('click', function() {
$.ajax({
url: 'recieve.php',
type: 'post',
data: { get_name: my_values.name, get_age: my_values.age, get_add: my_values.add },
success:function() {
}
});
});
});
</script>
</head>
<body>
<button>Get the values</button>
</body>
</html>

getting values from textbox using jquery [output via php variable]

main.php:
<SCRIPT type="text/javascript">
$("#btnSubmit").click(function () {
alert("What will i put here!");
});
</SCRIPT>
<input type = "submit" id = "btnSubmit" value = "Try IT!" />
<div id = "show_search2">
</div>
output.php:
<?php $val = $_POST['btnSubmit'];
echo "<H1>$val</H1>"; ?>
What specific jQuery functionality can get the value of the btnSubmit and access it through $_POST and output it to the div show_search2? I used prototype.js but it provides conflict with my other JS scripts in the index.
Note: all my script source is included in index and that's why I didn't include the source in this post.
Do you mean something like this:
$("#btnSubmit").click(function (e) {
e.preventDefault();
var submitVal = $(this).val();
$.ajax({
type: "POST",
url: "url_of_your_output.php",
data: {btnSubmit : submitVal},
success: function(response) {
$("#show_search2").html(response); //response from output.php
}
});
});

Categories