how to put variable from javascript to php variable? [duplicate] - javascript

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
This is my codes using script, its a part of my codes, how can i put the var x into my VALUES in php, in the specified 'NULL' value?
enter code here
var x = document.getElementById("name_stop");
<?php require_once('dbconnect.php'); $result2=mysql_query("INSERT INTO log (status, code)VALUES('login','NULL')"); ?>

You can't move JavaScript variables to PHP unless you refresh the page because while JavaScript is client-side, PHP is server-side. When the page is loaded, PHP's job is done.

You should have a look on how to use ajax.
Moreover, for security of your apps, always check the data which are coming from client-side.

You are looking for AJAX. You will have to send a AJAX request to your phpscript.
The easiest way to send an AJAX request is using the jQuery library.
$.ajax({
type: "POST",
url: "insert.php",
data: { name: x }
})
.done(function( msg ) {
alert( "Data Saved: " + msg );
});

Related

JavaScript - PHP HttpRequest Security Concern [duplicate]

This question already has answers here:
Users can change html code to delete what they want
(2 answers)
HTML hidden input shouldn't be editable
(6 answers)
Prevent URL editing by users to restrict access to server's resources
(1 answer)
Input form with hidden field how to secure it
(4 answers)
How do I prevent others from sending their own data to my php page?
(5 answers)
Closed 4 years ago.
I am creating an application using JavaScript and PHP.
I post my data to a PHP file and then the PHP file insert my data into the MySQL database.
All good but I have some doubts about security because my dom can be easily manipulated using the browser.
For example my code like below
var req = {
method: 'POST',
url: 'phpfile/send_message.php',
headers: {
'Content-Type': undefined
},
data: {
"UserId": UserId,
"Message": Message,
...
..
}
};
$http(req).then(function successCallback(response) {
//Do Something
}, function errorCallback(response) {
//Do Something
});
anyone can put a breakpoint in this code and easily can change the UserId, therefore, the message sends to another user.
Ok, I know I can minify my Script but I think this is not enough. If someone wants to change data, he can find the code easily.
Is there any way to prevent this?
Any information you can provide me would be greatly appreciated.

Editing Javascript to Call MySQL [duplicate]

This question already has answers here:
Can JavaScript connect with MySQL?
(16 answers)
Closed 5 years ago.
I have this function that fires when a checkbox form is submitted:
function report_build() {
var checked_count = 0;
var where = ""
$('.peer:checked').each(function (i) {
checked_count++;
var label = $(this).parent().next().html();
where += "'" + label + "', "
});
$('#tabs-1').html(where);
$('#ui-id-6').click();
}
I want to send the where variable to a PHP script to use in the where clause of a select list. What's the easiest way to do that?
Thanks!
You will have to use AJAX for that.
A simple post should do the job:
$.post('handle-sql.php', {where: where}, function(data) {
// In the handle-sql.php script, use $_POST['where'];
// Callback function: called after php script is completed
// Where 'data' is the echo'ed data from the PHP script
});
You should try to POST or GET it with an HTML <form>. They send to PHP, but otherwise javascript is run client-side.
I'm not quite sure about what you're asking but if I'm not mistaken you want to send a variable from javascript to php. You can easily do that with ajax. Here's an example;
$.ajax(
{
type: "POST",
url: "your_php_name.php",
data: {
'where' : where
},
dataType: "html",
success: function(answer)
{
//fires when php finishes it job.
}
});
And on php side, you can read the variable with $_POST['where'];

Passing objects from tomcat Listener to Javascript function [duplicate]

This question already has answers here:
How should I use servlets and Ajax?
(7 answers)
Closed 6 years ago.
I'm trying to send objects from a Java Listener (not the Javascript eventListener!) to a Javascript application without success.
The requirement is: a Javascript application running in the browser should ask to a Listener an object (such as an array) at the launch and the Listener responses giving the array. There are many ways to do this. What are the most relatively secure and efficient ones?
I will try now to do an example. The Javascript function asks thorough jQuery the array directly a JSON file (by HTTP GET request) and storing in this way its content in the 'data' variable, as follows:
$.get("./myJSONfile.json", function( data ){
// Stuff to do
var myArray = data;
...
}
and this without asking the intervention of any Servlet or Listener. What I have to do I want to use a Listener to pass the content of the JSON file to the Javascript function?
Not sure what exactly you mean, but one usual way to communicate between javascript and server is to write an ajax call to get the data you want, just like below :
var myArray = [];
$.ajax({
url: "someUrltoServer",
async: false,
type: "GET",
error: function (jqXHR, textStatus, errorThrown) {
alert(jqXHR + "-" + textStatus + "-" + errorThrown);
},
success: function (data) {
myArray = data;
}
});

How to get external html page content using jquery or ajax [duplicate]

This question already has answers here:
Loading cross-domain endpoint with AJAX
(9 answers)
Closed 9 years ago.
There's this question a friend asked me today and it's bugging me all day long. I have plowwed tens of forums searching for the right way To get an external html content and show it on my page.
I want to address to http://www.someExternalURL.com and to retrieve all the html from this page.
I tried the following:
$.ajax
({
url: "http://www.someExternalURL.com",
type: "GET",
cache: false,
crossDomain: true,
data: {},
jsonp: 'jsonCallback',
dataType: "jsonp",
success: function (data) {
alert('good');
jsonCallback = data.Result;
},
error: function (e) {
alert(e.responseText);
}
});
Didn't work.
Then I tried:
var all;
$.get("http://localhost:60939/About.aspx", function (my_var) {
alert(my_var);
}
Only that the latter is good only for local pages. AND I NEED AN EXTERNAL
Any help'd be much appreciated.
Thanks in advance
There are many ways to do this, using server-side code will achieve you this in less lines than JavaScript.
Using PHP you can use this:
<?
$url = 'http://www.google.com';
echo file_get_contents($url);
?>
Or using Perl you can use:
#!/usr/bin/perl -w
use strict;
use warnings;
use WWW::Mechanize;
my $mech = WWW::Mechanize->new();
$mech->get("http://www.google.com");
my $content = $mech->res()->content();
print "Content-type: text/html\n\n";
print "<html><head>";
print "<title>Perl HTML Parsing</title>";
print "</head><body>";
print $content;
print "</body></html>";
You can't make requests to external pages in browser if that site don't allow you this. See Cross Origin Resource Sharing
But you can do this in server application.
You can use JSONP only if the external site allows it, by doing a special implementation on how the JSON result is returned.
You can use a url proxy hosted on your website that uses cURL, or whatever means to download the desired content such as
http://YOURSITE.com/get.php?=http://www.EXTERNALSITE.com/json

How to retrieve GET variable from ajax response in jquery [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get URL parameter with jQuery
I have a form that posts data over ajax. The response comes back in the form of GET variables in the URL. For example if there was a failure of writing the form data the return address would be: http://example.com/?error=1
How would I check for this using jquery. Right now when I do a console.log on the msg variable I just get the html output of example.com (which I guess makes sense). I need the GET variables though. How would I achieve this?
$('#wp_email_capture').submit(function(e){
var email = $('#wp-email-capture-email').val();
$.ajax({
type: "GET",
url: "/",
data: "wp_capture_action=1&wp-email-capture-email=" + email
}).done(function( msg ) {
console.log(msg)
});
e.preventDefault();
});
AJAX by default will return the content from the request. What you want are the headers.
If you are being given a return address of http://example.com?error=1, then it means this is being returned as a redirect header.
To get the header information from an AJAX request, that has been answered here:
jQuery and AJAX response header

Categories

Resources