Sorry for this but I searched the whole web for a solution to my probleme but in vain :(
What I want to achieve is creating a normal Post and adding a form to it that once submitted, goes to a database and gets back a value.
I created a plugin for that and integrated it in the admin menu then set a function that queries the db :
myfunc_getcode($db, $table, $value, $return) // returns a value
how can I achieve this!? I mean, when a user inserts some data in the form (that exists inside a post or page) then he clicks on submit, Ajax talks to the db and gets the results back.
I don't even know if wordpress 3.0.1 allows such things!
I got it to work by using
add_action('the_content', 'my_function');
this hooks my plugin to the posts and pages.
then in the function I transmit the content like;
function my_function($content) {}
Then I used Ajax by integrating jquery inside my script;
<script type="text/javascript">
jQuery(document).ready(function($) {}
for submitting forms I used jquery/ajax to listen to the form
$("#my_form_id").submit(function() {
and used jquery's $.post to pass variables to a php page that handeles my results and returns them by echo.
hope this helps someone!
Related
I am able to retrieve data sent via ajax in functions.php. But I need that data in template.php (of my theme). This is my first time using ajax and maybe I'm going about it the wrong way. I am able to echo $_POST['myvar']; within the functions.php (I will be posting the code once back to work). Assuming the setup is correct, can I access the ajax data outside of the functions.php? Btw, I just signed up here at Stack as well, so if failed to follow some procedure, I apologize.
Edited
Thanks guys - here is a sample code. In the js file I have:
$(window).load(function(){
$("#cat").on("click",function() {
var selectedCat = $(this).children("option").filter(":selected").text();
$.get('../../../../../../wp-admin/admin-ajax.php', {
action: "parent_cat_send",
parent_cat: selectedCat
});
});
});
And in functions.php I have:
add_action('wp_ajax_parent_cat_send', 'current_par_cat');
add_action('wp_ajax_nopriv_parent_cat_send', 'current_par_cat');
function current_par_cat() {
global $parent_cat;
$parent_cat = $_GET['parent_cat'];
echo $parent_cat;
wp_die();
}
The response has the $parent_cat value. I would like to use $parent_cat outside of functions.php i.e in the template.php. So far, I've tried creating a function with global variable:
function set_global_var($new_value)
{
global $my_global_var;
$my_global_var = $new_value;
}
and calling it inside the ajax function:
set_global_var($parent_cat);
To further explain my goal, I'm trying to get the selected value from a drop-down. This value is the parent category name generated by wp_dropdown_categories. After getting it, I would like to then get that parent category's sub categories. I thought of Ajax (which I've never used before) because I'd like to have two drop-down options - one with the parent categories, and the other with subcategories generated by the selected parent category(possibly without refreshing the page). Thanks.
Follow the step to rendor your data in you template file. There are two way to rendor data that you can implement same time but its denpands on the situation.
You are sending ajax request from admin panel and want to display on frontend
You are sending ajax request form front end and want to display on frontend
If you are sending ajax request from admin panel and want to update the template(front end) in this case first you have to stroe the your data in database. As you said you accessed the data through functions.php file now you have to update_option() to add or update your data to wordpress database. Add have to check and fetch it in your template with get_option() But it will only rendor on page refresh from front end.
Now situation two, You are sending data from front end and want to update it also. in this case follow the first step to update_option() and get_option() and also send response from functions.php that you want to display in your front page(Only on the same page that you used to send request.) and and display it. So that if you refresh page then the same updated data will rendor on your template.
Hope you understand what i tried to explain.
maksbd19 gave me the right answer as I can't access the variable outside of functions.php. Not sure how to mark the response as the correct answer but here it is:
"agreed with #rjdown it is impossible to assist you with this little information. But what I can say for now is as far as I know you can not use your ajax code in your theme file/s as they are not loaded when your ajax request is parsed. Only the functions.php file is loaded and you should have all of your business logic in this file only. – maksbd19 Apr 26 at 3:28"
I am using the Jeditable plugin to edit in place. If I use it directly on the page, the plugin works perfectly; but if I use it in the modal window, it does not work anymore. The data populating the modal comes from a PHP file via AJAX, it seems that the plugin does not load in the modal.
This is the code that ajax receives from PHP:
<div class='edit2' id='div_1'>Dolor</div>
And this is the plugin call that I make:
$(document).ready(function() {
$('.edit2').editable('http://www.example.com/save.php');
});
If i do this directly on the page, it works fine; but not in the modal.
From the description and the code in the question, it looks like the problem is the time in which you call the editable() function.
Right now, you call it when the document is ready (inside the $(document).ready() function). This is fine and will work great if the element with class .edit2 is already in the page (e.g.: when you use it directly on the page), but it doesn't work if the element is not there yet (e.g.: when you load it later using AJAX).
The solution to this issue would be to move the call:
$('.edit2').editable('http://www.example.com/save.php');
from the page load into the success function of the AJAX call, right after you have added the content into the modal. That way, .edit2 will exist when the plugin is called, and it should work normally.
I am using codeigniter. What i want to achieve is as follows.
I am on a page /profile/username (Controller profile, function username).
This function loads a view which has a submit button.
<input type=submit value=apple>
I then have a jquery file which has ajax processing such that when the submit button is clicked /profile/process_click (a private function) is called with $.post.
The function process_click in the controller profile calls a method function which inserts the value of the submit button into the database (in this case 'apple'). Alongside this function needs to insert the username of the person on whos profile the link was clicked (in this case 'username').
So.. my question is, how do I pass username from my profile controller to my jquery file such that it can be passed to process_click by $.post?
I need a secure and safe and ideally not resource intensive way to do this..
Cheers
First of all, the process_click function cannot be private or you won't be able to call it via Ajax.
Jquery post method has a callback function which is called when the PHP script returns an answer, so in your js file do:
var userData = $("#form1").serialize();
$.post('http://localhost/profile/process_click',userData, function(data) {
//some code that handles data returned from php
});
in the php script:
public function process_data(){
$userName = $_POST['userName'];//or whatever the input name is
...//do some processing
echo $username;
}
i assume all of us would like to help but your question is not understandable, i think this is the reason you have received one answer only. Please edit and explain your problem in a wilder context. Don't save words, and use reference properly. for example, what is to 'jquery file' ??? what is to pass value to jquery? i assume you refer to javascript variable, and jquery $.ajax{} call, but can't be sure. can you be more oriented in your description. i also assume your english is much better than mine according to your residence so it shouldn't be an issue for you.
i can try and post back if you fix,
thanks and goodluck
I'm a super beginner in php, and I'm using a MVC php framework called Yii. I can't seem to find any articles that explain how to get values of html elements with PHP. Everywhere I look it's all about how to get values from form fields after a POST in some other view. Is there anyway to get field values and send them to a controller in PHP and just come back to the original view.
In .Net MVC I just use jquery to get form fields and do an ajax call. It's not sensitive data so I'm not worried about security. I like ajax because I don't do any page post back, I just send my data over and remain on the same page I was on.
Is there any way to do MVC AJAX kind of thing with PHP? Read html element values and send them to a controller for data manipulation?
It works the same way. Yii comes bundled with jquery, so you
just use jquery to get form fields and do an ajax call
to some controller function, do whatever you want with it, and return a response, with php's echo.
If you already know some jquery, then the client-side shouldn't be much different from .net mvc.
Edit:
To add a <script> to the generated html see registerScript.
To create urls use the createUrl function.
To add ajax options to html tags code looks similar to:
echo CHtml::checkBox('mybox',false,
array(// array for htmloptions, we also pass ajax options in here
'class'=>'checkBoxes_class',
'ajax'=>array(// this is ajax options for jquery's ajax
'type'=>'POST',
'url'=>Yii::app->createUrl('xyz',array('clickedboxid'=>'mybox')), // here you passed clickedboxid as a get variable
'beforeSend'=>'function(){}',
'success'=>'',
// etc etc
)
)
);
Every html tag generator helper function takes htmlOptions array, where we can also pass ajax options.
While reading these values in the controller:
public function actionSomeAction($id){
// $id is mybox
echo "Hello"; // this is returned as response to the client
}
Hope this is enough for you to get started.
I need to pass some data via AJAX POST or GET to a php file which then uses that data to
reference or access a database without actually refreshing the page. How am i to do it?
For ex: if i use:
function callFunc() {
$.post ( " phpFile.php " , { name: "James"}
...
...
...
}
Take a look at Easy Ajax with jQuery should explain things.
When you use Ajax, an http request is made to the server that calls a php script sending post or get variables to this. The script is then executed and the response returned to the main page, with jquery you can manage the response and make the page not to refresh. If you have a HTML form and a submit button the page will be refreshed, you have to avoid this. Use jquery Val() selector to get the input values and send them trought an Ajax call without the submit button.