Reputation: 369
I'm learning jquery as I code and I need to do something with jquery related to sessions. Code goes like this for html:
<table class="match_table">
<tr id="10"><td class="timez">20:45</td><td>Valencia</td><td>:</td><td>Fulham</td></tr>
<tr id="11"><td class="timez">20:45</td><td>Crvena Zvezda </td><td>:</td><td>Rad</td></tr>
<tr id="12"><td class="timez">20:45</td><td>Vojvodina </td><td>:</td><td>Elche</td></tr>
<tr id="13"><td class="timez">20:45</td><td>Chelsea </td><td>:</td><td>Tottenham</td></tr>
</table>
<div id="triping">
<form id="triping_form">
<table class="popz">
<tr>
<td>1</td><td><input type="radio" name="opklada" value="1" /></td>
<td>X</td><td><input type="radio" name="opklada" value="2" /></td>
<td>2</td><td><input type="radio" name="opklada" value="3" /></td>
</tr>
<tr>
<td>0-2</td><td><input type="radio" name="opklada" value="4" /></td>
<td>3+</td><td><input type="radio" name="opklada" value="5" /></td>
<td>4+</td><td><input type="radio" name="opklada" value="6" /></td>
</tr>
</table>
<span id="toje" style="display:none"><img src="success.png" /></span>
<input type="hidden" name="match_id" id="hidin" />
<input type="submit" class="button-class" id="grauzame" />
</form>
</div>
This is jquery:
<script type="text/javascript">
var current_id;
$('.match_table tr').on('click', function() {
$("#triping").slideUp(300);
$("#triping").slideDown(300);
current_id = $(this).attr('id');
$("#hidin").val(current_id);
$("#toje").hide();
$("#grauzame").show();
});
$("form#triping_form").on("submit", function() {
$.post("ajax.php", { match_id: $("#hidin").val() }, function(rez) {
if(rez == 'ok') {
$("#grauzame").hide();
$("#toje").fadeIn(500);
}
});
return false;
});
</script>
Now, I will now just simulate user login with opening sessions:
session_start();
$_SESSION['user_id'] = 12;
$_SESSION['username'] = 'eston';
I know jquery is not nice, but it works for now. I will improve it later. But the point is, logged in user can click on submit button and he will get confirmation image.
I need to prevent send message to guests, like 'You must be logged in to submit' when he clicks on submit. How to make this work together with sessions and what's the best way? I would probably figure it out but it's better to see what are the best practices. But for now any solution is welcome :)
Upvotes: 0
Views: 70
Reputation: 9340
Simply check whether $_SESSION['user_id']
exists or not. If it's an AJAX request, I usually return json with at least one field named result. It could be anything your jquery code can use, for example 'success' and 'fail'.
Upvotes: 1