Execute PHP code via javascript [duplicate] - php

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 9 years ago.
I'm trying to connect to my SQL server via Javascript.
In order to do this, I have to execute some PHP code, but I'm having some trouble with that.
At the moment I use this code to execute a PHP script, without success
function testConnection()
{
$.get("script/SQL/testConnection.php");
}
How is it possible to execute some PHP code, or connect to the server and execute SQL queries?
Thanks
Matt

That $.get call you have is a relative URL. Let's say your user is on page domain.com/home/. If you use that code block, the user's browser is going to make a GET request to domain.com/home/script/SQL/testConnection.php. Is that what you are expecting?
What HTTP status code are you receiving back?

Related

Javascript to remove alert() script in url before it executes [duplicate]

This question already has answers here:
How to prevent XSS with HTML/PHP?
(9 answers)
Closed 4 years ago.
I'm using PHP. I'm attempting to prevent some XSS on my page. One test I'm running has this in the url params:
www.mypage.com?error=<script>alert(11170579)</script>&foo=one&bar=two
The errorr=... param is not coming from a form input. It's just inserted into the url.
How can I use Javascript to escape/decode the tags so the alert() does not execute? I did find a couple of examples of parsing the param values in the url, but none mentioned how to prevent or change the code so it did not run.
Thanks for any help.
Take a look at PHP's htmlspecialchars function. This seems to be what you want:
<?php
echo htmlspecialchars($_GET['error']);
?>

How to access the HTML table from PHP [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
I have a page with a java script that adds a row to a table. On the same page there is a PHP script that should read the table and add it to the database. How do I read the table using PHP ?
What you're thinking of will not directly work.
A Javascript script is executed on the client side only, whereas a PHP is executed on the server side only, and before the datas are sent to the client.
PHP can not directly read the table.
What you can do is use AJAX to send the data from the user's browser to a PHP script that'll use them.
You'll find more infos here.
PHP is serverside programing, the PHP code serves data to the client - PHP can't read from the client.
If you add a row using javascript, PHP has no way of getting that info.
You'll need to use Ajax to solve your specific problem.
You cannot "read the table" with PHP. You will have to send the data of the table to the server (and read it with PHP) using JavaScript or CGI.
A well-known technique to do this is called Ajax.

Handling of the post request and execute another script in PHP [duplicate]

This question already has answers here:
How do I close a connection early?
(20 answers)
Continue PHP execution after sending HTTP response
(13 answers)
Closed 9 years ago.
So I have a PHP script that handles post requests. It works quite good, but (always a but) the data I am receiving is encoded. I need to run an external program to decode it. That also works.
My problem is, that my client connects to the server, does a POST and waits for the OK reply from the server, to verify that the post request has been handled correctly. Now when I add the part of the Decoder into my POST request handling script, it takes some time for my client to receive the OK from the server. It is only sending it back when the entire script has been finished, and not only the uploading of the files part, understandably.
What I was wondering now, can I do this in another way? When the uploading of the files is done, the client should now that the upload is finished. He should not have to worry about the decoding part. I also need it to decode immediately after the POST request has been handled, so I'm a bit puzzled at how I need to do this...
Don't really think I need to give out my code but if the need should arise i'll be glad to oblige.

curl php bash sign [duplicate]

This question already has answers here:
Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?
(12 answers)
Closed 9 years ago.
I have a code and need help. I have a curl call with a bash sign like this
http://example.com.ua/script.php
clients send me data using a script but some send me the bash # which I need to save all the data to my database. When I get $_SERVER I don't see all the data how can get all the data?
for example:
I have
http://example.com.ua/script.php#code1234
I need to save:
example.com.ua/script.php#code1234
but I see
example.com.ua/script.php
PHP does not have access to the location hash, period.
You only have access to querystrings in PHP, and if you need to get the hash, you would have to use javascript to get it, and send it to your PHP script with ajax.

retrieve name of calling script / Javascript in php [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Securing a remote ajax method call
Is there a way to retrieve the name of the javascript file that called a php script from within the PHP code?
Unfortunately, since I have to call a PHP script from Javascript (for Google maps) on a webpage, this 'invites' to retrieve my data by just calling the PHP script by hand. Checking for the script/file that invoked the PHP script would bring at least some protection.
You could set a session variable with some random value on the server when your page is opened, and add that variable in the query string to your php script.
Then in your javascript-called php script, you start the session and check the session variable against the query one.

Categories