0

I have a bit of code to record a certain action

$('#record').click(function(){
$.get("../confirm.php", { "id": '<?php echo $record; ?>' }, function(data){});
});

Which works fine, but i don't want the page to be full of javascript and such as i have other things like this on it too, so i am trying to move it to a js file.

In the php file

<script type="text/javascript">var record = "<?= $record ?>";</script>
<script type="text/javascript" src="jsfile.js"></script>

In the js file

$('#record').click(function(){
$.get("../confirm.php", { id: record}, function(data){});
});

But it doesnt want to play ball, any ideas how to do this?

5
  • Don't use the <?= syntax to output code. It being the only difference I can see...I'd say that's also the problem with your code. Check the JS Console of your browser for any errors. Commented Mar 4, 2013 at 12:59
  • can't you wrap it all in functions and use onclick events on the actual buttons so you can pass variables in via the onclick Commented Mar 4, 2013 at 13:00
  • 1
    what gets output in browser source for var record? Commented Mar 4, 2013 at 13:02
  • I dont normally use ?>= syntax but i viewed the source and it outputs ok, $record is set as get id further up in the page Commented Mar 4, 2013 at 13:03
  • need to inspect request in browser and provide more details than won't play ball Commented Mar 4, 2013 at 13:04

2 Answers 2

1

Make 'record' as class of div and assign php variable $record as its id.Use event.target.id to get variable $record from div.

<div class="record" id="<?php echo $record;?>">
 Your code here
</div>

And Use this $('.record').click(function(event){ var record=event.target.id; //your code here });

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

Comments

0

If I may a suggestion to you. Set in the view on the element the ID. Then when the action is clicked, get the id for the data.

Example:

<input type="text" name="inputName" id="<?php echo $record; ?>" class="btnToRecord" />

$('.btnToRecord').click(function(){
    var clickedElement = $(this);
    $.get("../confirm.php", { id: clickedElement.attr("id") }, function(data){});
});

Edit: Correction for $(this) was pointing to the get instead to the element, putting in a var will fix the problem

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.