Send data to php function with javascript - php

I want to send some data to a php function with javascript. I have already post a question about this earlier today here, though I didnt get a satisfactory answer, maybe didnt explain myself right.
So I have some variables in javascript( I get it through some API). I would like to send this variables to a function in php. I am using codeigniter so I call the function like("somecontroller/somefunction").
I would like the send the data through post to the function. The function calls a view and reloads the whole html. I am looking for something like sending a form. Only I have the variables in javascript. If there isnt any other way I will dynamicly create a form with jquery and send the data with the .click method on the submit button. But is there a more elegant solution??
I know about ajax, but the problem is that I dont want a asynchronous request I want to just submit the data and then for the new html to load.

The easiest way I can think to solve this would be to send the data through the query string, although you'd have to change the target page to accept GET or REQUEST instead of just POST.
To do this just:
window.location.href = 'somecontroller/somefunction?var1='+var1+'&var2='+var2;

Take a look at this link how to pass arguments to your controller methods in CodeIgniter. Then just redirect user to this link.
If you want to redirect through the Form, set action of form to your controller and in php you can access your form elements through $_GET or $_POST

Since you said you're using jQuery:
$.post("somecontroller/somefunction", {
param1: var1,
param2: var2,
...
}, function(result) { ... });

Related

onClick add to database

I have the below code, and I'm not sure if it even works. Basically, I want to do a like system, whereby when I click on the link, it adds a +1 to the user's likes.
I've tried so hard to read up but I just don't get anything, and I found a code similar below.
When I clicked on the link, it does process my insert.php page, but I don't know how to get the variable values...
What should I do? I'm not sure if the code structure below is correct...
Thanks!
<script>
function insertSalary()
{
var salary = $("#salary").val();
$.post('insert.php', {salary: salary}, function(data)
{
$("#current-salary").html(data);
});
}
</script>
<div id="current-salary">
<a id="salary" onClick="insertSalary();">+1</a>
</div>
The variable will be in your php script as $_POST['salary']
The value of salary is passed as part of the post method in jquery.
So you can do:
$.post('script.php', {salary: 100}, function(data){...});
and this will pass the value 100 to your php script as the salary value.
In php the $_POST and $_GET hashes contain the data that you pass with a given request. In jquery $.post, $.get $.ajax create requests and take data hashes to build the data you want to pass with the request.
While this may work, I would recommend separating your logic from your website by putting the javascript in an external file and then link your HTML page to it.
I would also advise against declarative event binding which you have done by specifying onClick="insertSalary()".
jQuery provides a method to pro-grammatically assign functions to events using the on method. So for your code, you could use:
$('#current-salary').on('click', insertSalary());

Using jquery to process actions which require passing PHP variables within MVC framework

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

Access html elements data and send to controller in PHP

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.

passing javascript value to another page

I am trying to passing javascript variable value to another page and retrieve here into a php variable
Using function i made a pop up to appear and store it to a java script variable so now how do i pass it another page and i do have some text box values but i get this ,i use action and give page name in it.I need both text box value and prompt value in another page.
Please understand JavaScript is ran on the client machine, and PHP on the server.
HTTP is stateless.
You have to use AJAX.
Are you familiar with AJAX?
Use JavaScript to dynamically add hidden form fields into your form and set the value (alternatively just create the hidden fields in the php output with no value and just use JS to set it).
If you use jQuery you can call something like:
$.post("test.php", { name: "John", time: "2pm" } );
See http://api.jquery.com/jQuery.post/
Using JS you can put a variable into cookie:
document.cookie = 'var1=VALUE1'
Then read it on the other page.
Or you can append #var1=value1 to URL, then read it on the other page using
top.location.hash

PHP database connectivity/access using AJAX

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.

Categories