How to POST multiple Variable in JavaScript inside PHP? - php

I am trying to pass multiple variable using JavaScript and PHP, but not being able to do that.
echo "<a href=javascript:popcontact('btsdetails.php?uid=" . $row["bs_id_site"] . "&sid=" . substr($row['bs_id'], -1) . "')>" . $row['bs_id'] . "</a>";
Therefore, I am trying to POST "uid" and "sid" using & sign, but it is not working. It is only accepting the "uid".
Can anyone help me to solve this problem?

Here is the answer:
<script type="text/javascript">
function popcontact(uid, sid){
window.location = "btsdetails.php?uid="+uid+"&sid="+sid;
}
</script>
<?php echo $row['bs_id']; ?>

you can use http_build_query
<?php
$params = array("uid"=>$row["bs_id_site"], "sid"=>substr($row['bs_id'], -1));
$url = "btsdetails.php?".http_build_query($params);
?>
<?= $row['bs_id'] ?>

Related

how to get a third variable using a doubled join variable in php

PHP CODE:
$video1="welcome.mp4";
$video2="movie.mp4";
$video3="ends.mp4";
$num_id="1";
$get = '$' . "video" . $num_id;
$file = $get;
echo "<a href=Player.php?file=$file'>Play</a>";
html code results:
Play
HTML - expectancy:
Play
I would do something like this.
$videos = ['welcome.mp4','movie.mp4','ends.mp4'];
foreach($videos as $video){
echo "<a href=Player.php?file=$video'>Play</a><br>";
}
I would do this (If I wanted to be Kool, or confuse a junior developer .. lol )
$video1="welcome.mp4";
$video2="movie.mp4";
$video3="ends.mp4";
$num_id="1";
$get = ${"video".$num_id};
$file = $get;
echo "<a href=Player.php?file=$file'>Play</a>";
Output
<a href=Player.php?file=welcome.mp4'>Play</a>
But I am not sure you are ready to have access to that much power...
Sandbox

Using PHP to determine what HTML to write out

This block of PHP code prints out some information from a file in the directory, but I want the information printed out by echo to be used inside the HTML below it. Any help how to do this? Am I even asking this question right? Thanks.
if(array_pop($words) == "fulltrajectory.xyz") {
$DIR = explode("/",htmlspecialchars($_GET["name"]));
$truncatedDIR = array_pop($DIR);
$truncatedDIR2 = ''.implode("/",$DIR);
$conffile = fopen("/var/www/scmods/fileviewer/".$truncatedDIR2."/conf.txt",'r');
$line = trim(fgets($conffile));
while(!feof($conffile)) {
$words = preg_split('/\s+/',$line);
if(strcmp($words[0],"FROZENATOMS") == 0) {
print_r($words);
$frozen = implode(",", array_slice(preg_split('/\s+/',$line), 1));
}
$line = trim(fgets($conffile));
}
echo $frozen . "<br>";
}
?>
The above code prints out some information using an echo. The information printed out in that echo I want in the HTML code below where it has $PRINTHERE. How do I get it to do that? Thanks.
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno=[$PRINTHERE]; halos on;", "frozen on")
You just need to make sure that your file is a php file..
Then you can use html tags with php scripts, no need to add it using JS.
It's as simple as this:
<div>
<?php echo $PRINTHERE; ?>
</div>
Do remember that PHP is server-side and JS is client-side. But if you really want to do that, you can pass a php variable like this:
<script>
var print = <?php echo $PRINTHERE; ?>;
$("#btns").html(Jmol.jmolButton(jmolApplet0, "select atomno="+print+"; halos on;", "frozen on"));
</script>

Creating JavaScript variable within PHP retrieving in JavaScript

Basically what I have is, there is an index file, that links to a JS file, which then pulls in a PHP file.
So in the PHP file I have (along with other code) :
echo " <script type='text/javascript'>var paging = [ ";
echo "{";
echo "numrows:'" . number_format($numrows) . "',";
echo "pagenum:'" . number_format($pagenum) . "',";
echo "maxpage:'" . number_format($maxpage) . "',";
echo "record_min:'" . number_format($record_min) . "',";
echo "record_max:'" . number_format($record_max) . "'";
echo "}];</script>";
Then, in a JS file I use ajax:
req.open("GET", "server.php", true);
and within the readystatechange I need to retrieve that paging variable you see from the php file because it contains data from the database.
In the JS file I have tried using a variable instead of echo
var m=<?PHP echo $paging; ?>
but that doesn't work either.
Any suggestions or ideas how to get this to work?
I'd use JSON instead of JS. You should be able to pass a PHP array to the json_encode function. See: http://php.net/manual/en/function.json-encode.php
Also, on the PHP side, you may want to modify your response headers to indicate that the response message body is JSON:
header( 'Content-Type: application/json' );
Then, use jQuery to get the JSON from the server: http://api.jquery.com/jQuery.getJSON/
Another possible solution for you if you don't get direct JSON working (scaled down to two variables)
echo '<div id = "numrows">'. number_format($numrows) . '</div>';
echo '<div id = "pagenum">'. number_format($pagenum) . '</div>';
in the php file
req.onreadystatechange = function(){
if (req.readyState == 4 && req.status == 200){
document.getElementById('return').innerHTML = req.responseText;
}
}
req.open("GET", "server.php", true);
req.send();
n["numrows"] = document.getElementById("numrows").innerHTML;
e = document.getElementByID("numrows");
e.parentNode.removeChild(e);
n["pagenum"] = document.getElementById("pagenum").innerHTML;
e = document.getElementByID("pagenum");
e.parentNode.removeChild(e);
in the javascript
<div id = "return" style="display: none;"></div>
in the html
Not the most elegant solution, but fairly straightforward to follow if you can't get anything else working.
Ok because jQuery makes this easy, here is my example:
$paging = array(
"numrows" => number_format($numrows),
"pagenum" => number_format($pagenum),
"maxpage" => number_format($maxpage),
"record_min" => number_format($record_min),
"record_max" => number_format($record_max)
);
$output = json_encode($paging);
header( 'Content-Type: application/json' );
echo $output;
Then in your javascript page using jquery do this:
function myCallBackFunction(data){
alert(data.numrows);
}
$.getJSON('server.php',myCallBackFunction(data));
Hope this helps.
Edited to use array and json_encode

PHP variable-infused link not writing to a variable?

http://www.reecemcmillin.com/albums/
<?php
$uncut = file_get_contents('http://www.google.com/#sclient=psy-ab&hl=en&safe=active&source=hp&q=' . $_POST['band'] . '+' . $_POST['album'] . '+zip+inurl:mediafire');
$strip1 = strstr($uncut, 'www.mediafire.com/?');
$link = substr($strip1, 0, 30);
echo $link;
?>
It doesn't seem to be writing the website content to $uncut. Can somebody help me figure out what's wrong? Thanks.<3
Clients are not supposed to send URI-fragments (the portion of the URI following #) to servers when they retrieve a document. PHP is probably sending a request for the google homepage, effectively: file_get_contents('http://www.google.com/');. If you echo $uncut, that's probably what you'll see you're getting back.
Try a querystring-based URI instead.
<?php
$uncut = file_get_contents('http://www.google.com/search?sclient=psy-ab&hl=en&safe=active&source=hp&q=' . urlencode($_POST['band']) . '+' . urlencode($_POST['album']) . '+zip+inurl:mediafire');

Syntax error help

I seem to have a syntax error and can't see it myself, could someone run over it for me please?
Thanks.
<script>
var acurl_<?php echo $request_data['friendship_id']; ?> = "sn-include/create_bond_accept.php?friendship_id=<?php echo $request_data['friendship_id']; ?>&friend_id=<?php echo $fromuser['id']; ?>";
</script>
Because you got some answers that intended to show you how to improve your code, but actually don't do so (IMO), here is my attempt:
<?php
$acurl = array();
$acurl[$request_data['friendship_id']] = sprintf('sn-include/create_bond_accept.php?friendship_id=%s&friend_id=%s', $request_data['friendship_id'], $fromuser['id']);
?>
<script>
var acurl = <?php echo json_encode($acurl); ?>
</script>
I would not create dynamic variable names. This code would create a JS object, where the properties are the friendship IDs, something like:
{
'42': 'sn-include/create_bond_accept...'
}
You can access these URLs more easily from JavaScript than if you have dynamic variable names.
David, on the bright side, you don't have a syntax error.
If you're developing PHP, I would recommend two things:
Get a better IDE. Dreamweaver is TERRIBLE for working with PHP. I recommend NetBeans (it's awesome and free).
Start breaking up your code into chunks. The big ball of html and PHP is hard to debug.
Check this out:
<?php
// prepare output
$segment = '?friendship_id=' . $request_data['friendship_id'];
$segment .= '&friend_id=' . $fromuser['id'] . '";' . "\n";
$acurl = 'var acurl_' . $request_data['friendship_id'];
$acurl .= ' = "sn-include/create_bond_accept.php';
$acurl .= $segment;
$dnurl = 'var dnurl_' . $request_data['friendship_id'];
$dnurl .= ' = "sn-include/create_bond_deny.php';
$dnurl .= $segment;
?>
<script type="text/javascript">
<?php
echo $acurl;
echo $dnurl;
?>
</script>
Use here doc instead:
<?php
echo <<<JS
<script>
var acurl_{$request_data['friendship_id']} = "sn-include/create_bond_accept.php?friendship_id={$request_data['friendship_id']}&friend_id={$fromuser['id']}";
</script>
<script>
var dnurl_{$request_data['friendship_id']} = "sn-include/create_bond_deny.php?friendship_id={$request_data['friendship_id']}&friend_id={$fromuser['id']}";
</script>
JS;
?>
See http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Categories