0

I currently need to use a similar piece of PHP code. Modifying this code is not an option.

<?
session_start();

include '../connection.inc';

$conn = new Connection();

echo "ID:" . $conn->generateID();
echo "\n";
?>

In connection.inc a connection to a database can be made by creating a new Connection(). From there, the database can generate a unique 16-character ID (conn->generateID). Is there a way to retrieve to execute this PHP-file from javascript and extract the 16-character ID from the PHP-echo in javascript?

3
  • 2
    AJAX could probably do it Commented Mar 6, 2013 at 16:03
  • You couuld also print in a script bloc the value if your page is dynamic Commented Mar 6, 2013 at 16:04
  • Robert is correct. And AJAX call is your most viable solution. Though, why don't you just 'include' this file where you need it and use the variable yourself? Oh wait, because you don't want the echo to show up, right? Commented Mar 6, 2013 at 16:04

6 Answers 6

2

There's an alternative to the AJAX call, though it's a little...unusual. Output Buffering.

A little example:

<?php
    ob_start();  //This will stop all echoed output from being sent immediately.
    include("Whereveryourcodeis.php");
    ob_end_clean();  //Discards what has been stored and stops buffering.
?>

After this fragment, you can use $conn->generateID wherever on your page you want, without having written a single line of JavaScript.

I hope this helps a little.

Sign up to request clarification or add additional context in comments.

2 Comments

when I wrote my answer I actually thought of this but then thought nnnaaaah it's too complex. But you are right 100% on this!
I was already using this solution, but it didn't work with a header() function I needed to send later. Right after posting my question, I found ob_get_contents(). Problem solved. Thanks for your help!
0

You should make a call to this handler by using jQuery AJAX and specifying that PHP should return the result and what exactly AJAX should be doing on a successful completion of the AJAX call.

See here:

http://api.jquery.com/jQuery.ajax/

2 Comments

I wasn't familiar with AJAX until now. But it works fine indeed. Thanks for offering this solution!
@user1879060: My pleasure. That's why I do open source development and not that .NET crap. All about making better, cleaner code. If you need help in the future you can always hit me up.
0

Try something like:

<script type="text/javascript">
var dbId = '<?= $conn->generateID(); ?>';
</script>

Comments

0
<?php

session_start();

include '../connection.inc';

$conn = new Connection();

?>    

Then below:

<script type="text/javascript">
var bd_id = '<?php echo $conn->generateID(); ?>';


// JUST TO TEST THAT THE ID IS AVALAIBLE IB JAVASCRIPT
alert(db_id); 
</script>

3 Comments

This by itself won't help him I believe.
@RefugnicEternium do you find this may improve understanding?
Well, the thing is, he said that he must not alter the code...which is what you did, by removing the echoes.
0
  1. Create a JavaScript file that makes an AJAX query to a wrapper PHP file, which calls connection.inc

  2. Within JavaScript, call the AJAX query and parse the response, which will give you the ID

Finally, you should probably use HTTPS for this.

Comments

0

Try something like this

<?php
session_start();

include '../connection.inc';

$conn = new Connection();

print json_encode(array('ID' => $conn->generateID()));

Or if you can't change the above code just load the php response (with ajax call) into js variable and use regular expression to match the ID. Something like:

$.get('url/to/php', function (resp) {
    var matches = resp.match(/ID:(.*)\n/);
    console.log(matches);
})

matches[1] will hold the ID string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.