1

I've just started with php and js (jquery) and need help. I'm trying to do smth like betting service.
1. On html page user choose racecoast, using jquery I send its ID to php file, and it loads timetable: like this:

while($row = mysqli_fetch_array($result)) {  if ($row['date_time'] > date("Y-m-d h:i:sa")){
    echo "<tr>";
    echo "<td colspan='6' class='race' style = 'font-size: 14pt;' id = '".$row['raceID']."' >" . $row['raceID'] . "</td>";

it works. 2. Then, in the same php file I use

echo "<script type='text/javascript'>  
$(document).ready(function(){
    $('.race').click(function() {
      var elid = $(this).attr('id');  

and js perfectly gets the ID of racecoast. But I can't send elid value back to php.

I've tried to use simple $_[GET], sessions, cookies, to set value on some attrib in my html and then get it, created one more php file, to send information there by load(...) function. Maybe, I did something wrong?..

I feel that my script should be in php file, but how to be then?

my last try, and more code:

while($row = mysqli_fetch_array($result)) { // if ($row['date_time'] > date("Y-m-d h:i:sa")){
echo "<tr>";
echo "<td colspan='6' class='race' style = 'font-size: 14pt;' id = '".$row['raceID']."' >" . $row['raceID'] . "</td>";
echo "<td colspan='6' class='race' style = 'font-size: 14pt;'id = '".$row['raceID']."'>" . $row['date_time'] . "</td>";
echo "<td colspan='6' class='race' style = 'font-size: 14pt;'id = '".$row['raceID']."'>" . $row['special_title'] . "</td>";
echo "</tr>";//} }

echo "</tbody>                  
</table>";

echo "
 <script type='text/javascript'>
$(document).ready(function(){

    $('.race').click(function() {
      var nid = $(this).attr('id')
      alert(nid);
      document.cookie = 'newid=' + nid;
         });});
        </script>";

    $choise = $_COOKIE["newid"];
    echo $choise;

1 Answers1

1

To give you a short example, using $.post():

jQuery:

$(document).ready(function() {
  $('.race').click(function() {
    var elid = $(this).attr('id');

    $.post('path/to/your/phpfile.php', {
      'elid': elid
    });

  });
});

And to recieve the data in your PHP-file:

$elid = $_POST['elid'];

Some sidenotes:

Is it really necessary to echoing the total javascript in PHP? Would be better to create a seperate JS-file for that.

But without seeing your whole script it's hard to say if there are other issue present.

empiric
  • 7,825
  • 7
  • 37
  • 48
  • Just added more code. Can I use the same file path in post? – FinsterEule May 13 '16 at 09:34
  • @FinsterEule could be working but I would highly recommend to seperate your javascript from your php. Please have a look at the questions which is marked as a duplicated to get into the whole concept. – empiric May 13 '16 at 09:39
  • Thanks, I decided to open new tab on table click. It will be much easier. – FinsterEule May 13 '16 at 14:23