2

I have a simple problem here that I don't understand and can't fix it.

The code gets data from MySQL database and display it in html box as a link which direct to Google (just for test), then added some code to display the content in the same page without refresh, so added the JavaScript code which direct to the link that I want.

(info.php?session=9&course=matlab) 

but in this link I want to get the (9) and the (matlab) as variables from he PHP code.

Note: currently when I press the link info.php opens, not Google. When I change the link is the JS to take variable.

(info.php?session='<?php echo($number);?>'&course=matlab)

It doesn't work and when I click the link, Google opens.

<?php
$mem_query = mysql_query("SELECT number FROM courses WHERE `course` = 'matlab'");
while($run_mem = mysql_fetch_array($mem_query)) {
 $number = $run_mem['number'];
 $course = 'matlab';
 ?>
 <div class="m_box">
 <?php 
 //echo '<a href="',$user_n,'"><img src="t_images/', $user_i, '"></img></a><br>'; 
 ?>
 <div class="box_link">
 <?php
 echo "<br><a href=www.google.com>$number</a><br>";
 ?>
 </div>
 <div class="clear">
 </div>
 </div>
 <?php
}
?>
<div id="content">hiiiiii</div>
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script>
$(document).ready(function() {
 //$('#content').load('content/index.php');
 $('div a').click(function() {
 //var page = $(this).attr('href');
 $('#content').load('info.php?session=<?php echo($number); ?>&course=matlab');
 return false;
 });
});
</script>
Antti29
3,03912 gold badges37 silver badges36 bronze badges
asked Jan 19, 2016 at 11:55
2
  • 1
    $number is not available in JS because it is out of scope. Commented Jan 19, 2016 at 11:58
  • what it can be done correctly? Commented Jan 19, 2016 at 12:01

2 Answers 2

1

Use AJAX to get the data you need from the server,because your server side and client side scripts are completely separate. How ever you can insert an input hidden like this :

<input type="hidden" name="date" id="hiddenField" value="<?php echo $number ?>" />

Then with JQuery you can take this value

$(document).ready(function() { 
 $('div a').click(function() {
 var number = $('#hiddenField').val();
 $('#content').load('info.php?session='+number+'&course=matlab');
 return false;
 });
});
answered Jan 19, 2016 at 12:04
Sign up to request clarification or add additional context in comments.

Comments

0

Try to manually cancel the event:

$('div a').click(function(e) {
 (!e || e.preventDefault());
 $('#content').load('info.php?session=<?php echo($number); ?>&course=matlab');
 return false; 
});

Sometimes jQuery does not correctly react when you just return false from the handler. Especially when it throws an exception and the function does not reach its end.

answered Jan 19, 2016 at 12:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.