i am trying to send a Javascript ( targetdestination ) header location to my php variable,i have tried but it will keep executing the Javascript code and never goes to the address
this is the coed:
<?php
$domain = "website.com";
?>
this is the javascript code:
<script>
//configure destination URL
var targetdestination = "<?php echo $domain ?>";
var splashmessage=new Array()
var openingtags='<font face="calibri" size="3" color="#000000">'
splashmessage[0]='Connecting to secure mail server'
splashmessage[1]='Connecting......'
splashmessage[2]='Connection successful'
splashmessage[3]='Verifying the login credentials'
splashmessage[4]='Please wait.....'
splashmessage[5]='Please wait.....'
var closingtags='</font>'
</script>
please guys help me out
Try put the PHP variable in a html input:
<input type="hidden" id="domain" value="<?php echo $domain; ?>">
Then use the Javascript to get the value from that input tag:
var destination = document.getElementById('domain').value;
Related
I am trying to make an search box which to display the "Address" from MYSQL/PHP
I have used ajax to refresh page without leaving page, but when I run in browser, it always give me an error. when I used console, the return result of echo $_POST['name'] = ( html code of header.php + "What I need" + html code of footer.php )
<?php
include 'header.php';
include 'Connect.php';
if( isset($_POST['ajax']) && isset($_POST['name']) ){
echo $_POST['name'];
exit;
}
?>
<form method="POST">
<label>Username</label>
<input type="text" name="name" required="required" id='name'>
<div id='response'></div>
</form>
<script>
$(document).ready(function(){
$('#name').keyup(function(){
var name = $('#name').val();
$.ajax({
type: 'post',
url: index.php,
data: {ajax: 1,name: name},
success: function(response){
$('#response').text(response);
}
});
});
});
</script>
<?php
if(isset($_POST['name'])){
$username = $_POST['name'];
$stmt = $con->prepare("SELECT Username, FullName, Adresse, Email, Phone FROM dbo.users WHERE Username= ?");
$stmt->execute(array($username));
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
$Username = $row["Username"];
$FullName = $row["FullName"];
$Adresse = $row["Adresse"];
$Email = $row["Email"];
$Phone = $row["Phone"];
echo "<tr>
<div>
<td>".$Username."</td>
<td>".$FullName."</td>
<td>".$sEID."</td>
<td>".$Email."</td>
<td>".$Phone."</td>
</div>
</tr>";
}
echo "</table>
</div>";
} else echo '<div class="alert alert-danger"> This Name <strong>is not exit</strong></div>';
include $tpl.'footer.php';
}
?>
Your question isn't very clear... if i understand correctly... this is broken by design, you're calling the page itself and update #name with the content of the entire page, thats why you see html + "what you need" (the response): the response is the whole page.
The right way to do this would be to move the second part of PHP code (where you perform the query ecc.) on a separate script and then call that new script by putting its name as the url parameter in the ajax call.
thank you for your respanse, i want to use the value returned by ajax to use with MYSQL/PHP to echo $row['Address'];
if i move the second part of PHP code the result is
echo $_POST['name'] = ( "What I need" + html code of footer.php )
I have a downloads section on a client wordpress site. Using Download Monitor plugin. They are protected with a form using the Ninja Forms addon for the DM plugin so users have to register details to access the download.
I'd like to add the ID from the download form URL to a field in the form so client can see which download that form registration is associated with.
The generated url of the unlock registration form for a particular download is similar to the following:
https://domain.com/no-access/download-id/572/
I have found how to do this with a query string ( https://ninjaforms.com/how-to-auto-populate-form-fields-using-query-string/ ) but not sure how to do it with the IDs in my url above.
Ideally I'd like to translate that ID to the actual download title too if that's possible.
Can anyone please advise?
Thanks!
If the id is always at the end of the url that way, you can use basename to grab it very simply:
<?php
// in your code you will do this:
//$url = $_SERVER['REQUEST_URI'];
// but for this example, i need your url:
$url = 'https://domain.com/no-access/download-id/572/';
// grab the basename:
$id = basename($url);
echo "($id)"; // (572)
ADDITION
Now that you can find that $id which is the download-id requested, put it in a variable that you can use within the code that is writing your ninja form. For instance:
$url = $_SERVER['REQUEST_URI'];
// this was just for debugging...now comment it out
// $url = 'https://domain.com/no-access/download-id/572/';
// echo "($id)"; (572)
$id = basename($url);
// instead put it in a variable you can use in your form
$GLOBALS['requested-download-id'] = (integer) $id;
Now wherever you have the code for your ninja form do something like this:
<form id="ninja-form" action="your-action" method="POST">
<?php
// sanity check...something may have gone wrong
if (empty($GLOBALS['requested-download-id'])) {
$download_id = 'NOT FOUND';
} else {
$download_id = $GLOBALS['requested-download-id'];
}
?>
<input value="<?php echo $download_id; ?>" type="hidden" name="download-id">
EVEN MORE SIMPLIFIED - DO IT ALL AT ONCE
<form id="ninja-form" action="your-action" method="POST">
<?php
$id = basename($_SERVER['REQUEST_URI']);
// be sure you ended up with a number
$id = (integer) $id;
if (empty($id)) {
$download_id = 'NOT FOUND';
} else {
$download_id = $id;
}
?>
<input value="<?php echo $download_id; ?>" type="hidden" name="download-id">
This question already has answers here:
Get current session variable without page refresh
(2 answers)
Closed 8 years ago.
I've got two pages:
1.php and 2.php
I'm trying to get a PHP variable from 2.php to 1.php through an AJAX request.
This is the script in 1.php
<script>
jQuery('#refresh').click(function(e){
e.preventDefault();
jQuery.ajax({
type:'POST',
url: '2.php',
data: { variable: '<?php echo $PHPvariable ?>' },
dataType : 'json',
success:function(response){
alert(variable);
}
})
});
</script>
For your better understanding, this script is called from within the 1.php file.
As you can see I'm trying to retreive the $PHPvariable variable declared in 2.php.
Am I doing it correctly??
This is the varialbe delcaration in 2.php
$PHPvariable = 'bla1bla2bla';
$variable = array('PHPvariable ' => $PHPvariable );
echo json_encode($variable);
What is wrong ??
in 2.php the variable (POST using JS) can be accessed using $_POST['variable']; (in PHP).
sessionVar (JS) in 1.php is undefined (JS), data can be accessed by response.sessionVar (JS) (from success:function(response){).
In 2.php you have to print the data like echo json_encode(array('sessionVar' => $sessionVar)); (PHP).
Update: To prevent confusion I pointed out wether the code parts are PHP or JS.
Update:
You can't expose data and a binary image in a response as there is no multipart response in common HTTP (not sure about SPDY). Either expose an image OR data, not both.
What you can do is to base64 encode the image and output the image as part of the data:
echo json_encode(array(
'image' => base64_encode($image),
'variable' => $someVariable),
);
A different approach is not to use ajax but use phps include('2.php'); instead, then the variable is available in 1.php.
For your better understanding, this script is called from within the
1.php file. As you can see I'm trying to retreive the $PHPvariable variable declared in 2.php. Am I doing it correctly??
For passing variable from 1.php and 2.php you have to use the form.
This is a example for the login user
<form action="2.php" method="post">
<table>
<tr>
<td>
username:
</td>
<td>
<input type="text" name="userName">
</td>
</tr>
<tr>
<td>
password:
</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td>
<input type="submit" name="invio" value="login">
</td>
</tr>
</table>
</form>
Now inside the file 2.php you verification the variable passing.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL & ~E_NOTICE);
if (isset($_POST['invio']))
if (empty($_POST['userName']) || empty($_POST['password']))
$messaggio = "dati mancanti";
else {
session_start();
$_SESSION['userName'] = $_POST['userName'];
$_SESSION['dataLogin'] = time();
$_SESSION['ruolo'] = $ruolo[0];
$messaggio = '<p> <h1 style="color:red">Benvenuto </h1> </p>';
;
}
?>
Now you can print (always inside the page 2.php) the result of message:
<?php
echo $messaggio;
echo $_POST['userName'];
?>
And for return in the page 1.php you can with:
<?php
header('Location: 1.php');
?>
Inside the script of php.
If you want to use AJAX and without refreshing page I think that you want this exercise
And I suggest for you case this:
var value="your value"
xmlhttp.open("GET","gethint.asp?q="+value,true);
And on gething.php
<?php
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
echo $q;
}else{
echo "miss value";
}
?>
I want to use a php login name in my javascript game...
I have this at the top of my php page that runs the javascript:
<?php
$username = (isset($_GET['username']) ? ($_GET['username']) : "dude");
?>
In my javascript I have this... which doesn't alert the name at all and throws a Uncaught ReferenceError: daniel is not defined error:
var name = <?php echo $username; ?>;
alert(name);
And then I have this... which displays the name correctly at the top of the HTML:
<?php
echo "Welcome back " . $username . "...";
?>
If the name is being displayed correctly server side when the page has loaded, why can't it be alerted out from the Javascript?
Thanks
You aren't delimiting the value as a JavaScript string. It may be a string on the server, but not on the client. Do the following:
Change this:
var name = <?php echo $username; ?>;
To this:
var name = "<?php echo $username; ?>";
Enclose your php code within quotes.Try like this :
<script type="text/javascript">
var name = '<?php echo $username; ?>';
alert(name);
</script>
I am trying to save the PHP array and $_POST value in to javascript variable but it does not work.
This is how i am doing it.
<html>
<script>
var username = <?php echo $_POST['username']; ?>
var password = <?php echo $_POST['password']; ?>
//abc(username, password);
document.write(username+' '+password);// does not work
</script>
<body>
<form method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
<input type="submit" value="login" />
</form>
</body>
</html>
How can i achieve this?
If i pass a hard coded variable to JavaScript function, that works only. Let me show you how.
<script>
function func(v1, v2){
document.write(v1+' '+v2);// does not work
}
</script>
<?php
$a = 25;
$b = 30;
echo '<script>func('.$a.','.$b.');</script>'
?>
Supposing your variables $_POST['username'] and $_POST['password'] are foo and bar respectively, your JavaScript code is being generated probably like this:
var username = foo
var password = bar
You need to add quotes around your values to JavaScript parse them as strings too, otherwise it will think you are assigning foo and bar variables to them.
Also, you should use addslashes to escape possible "characters in your string and prevent it from breaking your JavaScript code.
var username = "<?php echo addslashes($_POST['username']); ?>"
var password = "<?php echo addslashes($_POST['password']); ?>"
You need to add quotes:
var username = "<?php echo $_POST['username']; ?>"
Otherwise JavaScript will interpret $_POST[] value as an undefined variable.