Is it possible for extracting object values from url link? - php

I want to extract object values instead of url parameter using PHP.
This is my link http://reussis.com/match-betting/admin2/edit_withdrawal_request.php?id=[object%20Object].
php code
print_r($_GET['id']);
When i print i didn't get any values why?
Is it foolish question please neglect. Thanks!

You cannot pass JavaScript objects directly over HTTP, because HTTP is not JavaScript.
Serialise it by JSON.stringify() on your object, then pass it as an encodeURIComponent()'d URL string.
For example:
let myObject = {'javascript': 1, 'isNumberOne': true};
// Serialises the object into a string, then urlencode it
encodeURIComponent(JSON.stringify(myObject)); // "%7B%22javascript%22%3A1%2C%22isNumberOne%22%3Atrue%7D"

use POST method instead of GET. GET method is an old style and it is prone to the hackers. Try to reconstruct your program now! Good Luck.

Related

Angular http.get/post with URL in data being passed

I have the following http.get request...
$http.get("api/checkSave/"+ JSON.stringify($scope.programDetails)).then(function(data) {
....
});
In my programDetails object, there is at least one field that can have a URL in it. I thought the stringify would've properly encoded the object so it would get passed correctly to my PHP function. What seems to happen though is that the URL isn't sent properly. I get an error that the server responded with a 404 not found error. I manually tried removing the URL in the request (copied it to the browser and sent an empty string for the URL field) and the request was processed correctly - that's why I am thinking the stringify can't do what I need.
I also need to send this data via a http.post later to save the data in the object. Hopefully, whatever I need to do in the get will also apply when I call the post.
Should the stringify pass along the data correctly or do I need to encode it differently? I've been using stringify in other apps, but haven't had to pass a URL in the data.
If I need to do it differently, I would need to know what to do on the PHP side to decode it for processing. The examples I find for get/post all seem to pass individual variables and not an object and not with an URL in one of the variables. I've seen encodeURIComponent mentioned but wasn't sure if that's what I need to use instead and if so, then what to use on the PHP side to decode it properly.
This doesn't really have much to do with the data consisting of a URL (only that a URL has a much better chance of having characters in it that have special meaning within a URL than other data).
The problem is that you are putting the data into a URL and URLs are not JSON texts.
To encode a string to put it in a URL use encodeURIComponent.
So it ended up that I had to replace the '/' in the fields that contained links and then it worked. Otherwise, the '/' was making it seem as though I was passing additional parameters in my get call. Thanks for the help!

A clean way of obtaining values from inputs and sending them to php file

I am looking for a really simple way of getting all the values from all input elements (and some dividers as well) and then quickly sending them to a PHP file with an already prepared submit function.
I have came up with a very crude solution but it needs to be heavily optimized.
For now when I click submit button I call a function
function $('#submit').click(function(){
var $divValues = new Array();
$divValues[0] = $('#divName').html();
$divtValues[1] = $('#divImgName').html();
var $values;
$values = $(':input').serializeArray();
$fileName = "php.php?firstVal=\""+$divValues[0]+"\"&secondVal=\""+$divtValues[1]+"\"&thirdVal=\""+$values['firstValName']+"\"&ect..."
//then i simply call the ajax function which will send the information to the php file
fetch($fileName);
});
maybe i could use the fact that the names of values in my associative array and the names of variables retrieved in my php file match and instead of setting my filename to ..."&thirdVal=\"" ... I would instead use the name of the $values['firstValName'] which in this example would be third value (although i have no idea how to do that)
or maybe there is an even easier way to accomplish what im trying to.
Any ideas on how to implement this piece of code in a nice manner??
Update:
For now I don't have a form element in my html but I can easily add one as soon as I see a solution that requires it to make the code simpler...
I know I can use serialize() function but my questions is about a nice way of dealing with the array that gets returned. (or maybe using 2 arrays one with name other with value or a 2 dimensional array)
Either way I just wish to see what is an easy way of both getting the data and creating the $fileName with correct values for the PHP file
I would appreciate some code or pseudocode in the answer that shows a simple way of creating the fileName with all the values
If anybody doesn't understand what I am asking or needs some clarification then they should feel free post a comment below so I can try to make myself clear.
Since you're using jQuery, you could use its AJAX methods with serialize() applied to the entire form. You can read documentation about it by clicking here. Also you can read documentation about jQuery's AJAX methods by clicking here, here and here.
In response to your request, here is a really simple example:
$.post("test.php", $("#testform").serialize());
You could really get the rest of what you need on how to use $.post or $.get functions from the documentation I referenced.
Serialize also takes care of urlencoding all the variables so you don't have to worry about that.
EDIT:
It's not really necessary to have a form. You could just do what you are trying to do with serializeArray() above, except (a) $(':input') does not select anything and (b) you should use serialize() and not serializeArray(). So without a form, the code would look as follows:
$.post("test.php", $("input,select,textarea").serialize());
This will properly request the script with the data encoded correctly and you can then read it in PHP script from $_POST or $_GET depending on whether you used $.post or $.get to request the script.

How to pass multiple variables between jQuery and PHP files using JSON?

How can I pass multiple variables between PHP files using jquery with json?
Ex:
$.post("example.php", {asdf:asdf, sdfg:sdfg}, function(data){
$('section').html(data);
});
But instead of just using the .html function I want to retrieve multiple variables from the PHP file and then use them for .html on my page. I can't just use data because that only outputs whatever PHP returns so to my understanding php can only return one thing in a form of an echo(), but how can I make it retrieve more than just that one variable? I think I have to use JSON but I have never used JSON before so I would appreciate it if someone could help me out.
Thanks.
You can return a JSON object from php ( Returning JSON from PHP to JavaScript? ). And then in your jquery you can acees it like this,
$.post("example.php", {asdf:asdf, sdfg:sdfg}, function(data){
$('section1').html(data.Variable1);
$('section2').html(data.Variable2);
});
You could have the php script output javascript code creating your variables and then just eval() it.
See the jQuery post() documentation here. Check out the dataType parameter, which tells jQuery what kind of data to expect back from the server.
See the json_encode() and json_decode() PHP functions here. Use json_encode() to prepare your populated data structure for echo()'ing back to the client.

Assign Javascript variable to PHP with AJAX

I understand...
the disparity between PHP being server-side and Javascript being client-side.
AJAX can patch this issue.
the XMLHttpRequest object is significant.
However, I still can't work out how to pass the variable between the two languages.
Any assistance would be much appreciated!
EDIT: Thanks for your replies so far, I should clarify that I would like to do this without loading a new page.
You need to send an AJAX request containing the value of the variable in the query string or POST body.
The PHP script at the other end of the URL can read the value from the query string or POST body.
you could use JSON with JS and decode it with php, but as the post above states, it involves ajax.
UPDATE: Tutorial found at link
You should give a more detailed example of what you want to accomplish. But if I understand correctly, you want to use a value from your JavaScript code in your PHP code?
You should try using POST or GET parameters to pass values into PHP. For PHP to be able to do anything, you will have to load a new page.
Lets say you have list.php serving a list of posts. You want to "like" a post. Your JavaScript is on this page. When you click "like", an AJAX POST is sent to like.php with POST parameter id=123.
In like.php, you get the value from the request (using $_POST['id'] or something more appropriate), do your magic (save to db) and echo some result, json_encode(array('success' => true)) for example.
The JavaScript-code that called like.php can then use it's callback to check if the "like" was a success or not and display feedback. The like.php could be any page, you could use list.php if you'd want. I used like.php for clarity.
If this is not at all what you wanted, please provide more information.
If you haven't looked at jQuery framework for javaScript, do it.
If you have a variable called city in JS, you can pass it to the file.php by using
var city = "London";
$.get("file.php", {location:city} );
Which is a HTTP GET request.
In PHP(file.php), you then grab it by using
$city = $_GET['location'];
echo $city; //London

What's the best way to send JavaScript array to PHP script using GET?

I have an interactive web application powered by jQuery where users can manipulate visual objects on the screen. When done, the "state" of JavaScript objects should be sent to PHP to store into database. I'd prefer to use GET for this, but solution that uses POST to submit the data is also viable.
I'm currently thinking to serialize all JS objects using something like base64 encoding and just use something like:
var str = encode_objects();
document.location = 'result.php?result='+str;
However, something tells me there has to be some, more elegant, way than writing my own base64 encoding function in JavaScript. Isn't there something already built into JavaScript that can do the job?
Update: decoding in PHP is not the problem. I know how to decode base64, JSON, whatever in PHP. The problem is how to encode data on JavaScript side.
AJAX is out of the question. It has to be a clean GET or POST with page reload.
json_decode() should serve you well here.
If you simply append a string containing the JSON representation of your javascript object to your HTTP request, you can json_decode() it in PHP and have a PHP object ready to go.
Edit: I should mention that this page has a link to a Javascript JSON stringifier, which will convert your JS objects into the necessary JSON.
first of all, if you're updating the objects state, it should be a POST. try to keep all your GET idempotent.
second, even if you don't want to do any AJAX, JSON is still your friend. the easiest way would be to serialize your objects into JSON and send the resulting string in the POST dataload. there are many ways to decode JSON in PHP, and you're all set.
You can use this serialize function, and then unserialize it on the PHP end.
GET or POST will probably depend on the size of your data : there is a limit on the amout of data you can pass through GET (something like 2000 bytes ; more, depending on the browser).
There is also an other thing to take into account : if you are modifying data, you should use POST ; not GET, which is used to... get... data.
About the format, I would really not go for anything like a custom function doing any kind of stuff like base64 : I would definitly go for JSON, which is a native Javascript notation.
You can for instance have a look at :
wikipedia
http://www.json.org/
There are many JS libraries that can generate JSON (probably every JS Framework can ; and there are standlone libraries like this one) ; and since PHP 5.2 there are functions in PHP to encode/decode it (see json_encode and json_decode)
If it's just a simple flat array you don't need to do anything fancy, as PHP has a built in feature to parse array syntax from GET/POST variable names. Rough example below.
Javascript side:
// Do the parameter-building however you want, I picked the short/messy way
var arrayvalues = [1, 2, 'a'];
var querystring = "var[]=" + arrayvalues.join("&var[]=");
querystring += "&var[something]=abcdef";
// querystring is now "var[]=1&var[]=2&var[]=a&var[something]=abcdef"
PHP side:
var_dump($_POST);
// Remember to validate the data properly!
if ( is_array($_POST['var']) ) {
count($_POST['var']);
echo $_POST['var']['something'];
array_map('do_something_interesting', $_POST['var']);
}
I've used Jquery/JSON.
var jsonArray = $.toJSON(jsArray);
Then sent it via JQuery/Ajax

Categories