Jquery and PHP sessions - php

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 :)

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'.

Related

How to send value of radio button to other page

I have a table with radio buttons to get the row values and 2 buttons
1 button.)For printing data , which moves to "notice.php"
2 button.)For row details,which stays on the same page.
<form action="" method="POST">
<table border="1" >
<tr>
<th>sourceID</th>
....
<th>Status</th>
</tr>
<tr>
<td><input type="radio" name="ID[]" value="<?php echo $tot; ?>" /></td>
<td>1</td>
....
<td>open</td>
</tr>
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
<input type="submit" name="details" value="details" />
<?php
if(isset($_POST['details']))
{
$n=$_POST['ID'];
$a=implode("</br>",$n);
echo$a;
}
Notice.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$n=$_POST['ID'];
}?>
The problem here is: My code is working perfectly fine with the button details.
But it doesnt work with issue, i.e after selecting radio button and clicking on the issue notice button :it gives Undefined index: ID in D:\XAMPP\notice.php.
kindly help
Your details button is a submit button, so it submits the form. However your other button is just a regular button and you use javascript to send the browser to notice.php. As such, it does not post any data to notice.php.
You could include the data on the query string and send it that way, e.g.:
location.href="notice.php?id=<?=$tot?>"
Or you could also have the issue button post the page, and then have your receiving page check which submit button was used. If the issue button was used you could then have the php code post to notice.php.
Using the following code is the exact same as having a link:
<input type="button" name="issue" value="Issue Notice" onClick="location.href='notice.php'" />
As in, this will not change the form action and submit the POST data to your new page.
You would need something like:
<form method="post" action="" name="unique-form-name">
<input type="radio" name="ID[]" value="<?php echo $tot; ?>">
<input type="button" id="unique-btn-name" value="Issue Notice">
</form>
<script type="text/javascript">
document.getElementById('unique-btn-name').onclick = function(){
document['unique-form-name'].action='notice.php';
document['unique-form-name'].submit();
}
</script>
Then, once you get the data to notice.php, you'll have to use the data as an array (you won't be able to echo the data):
$IDs = $_POST['ID'];
echo '<pre>',print_r($IDs),'</pre>';
<input type="radio" name="ID" value="<?php echo $tot; ?>" />
Your error is the name attribute.
Also the other button is not related to the form at all. You may want to use ajax here.

How to make the text field's value blank depending on a condition using jQuery?

I'm using PHP, MySQL, Smarty and HTML for my website. Now the scenario is I'm having a group of two radio buttons(viz. Yes and No are their values). I'm showing and hiding the textboxes depending on the value of selected radio button. The Hide/Show is working perfectly. The main issue is when I submit the form with radio button selected having value "No" the dependent text fields get hidden but the $_POST[] contains the textfield values present within them, if any. I want to make the values blank of these text fields as the user selects radio button with value "No". Can anyone help me in this regard? Thanks in advance.
For your reference I'm putting here the code snippet from my Smarty template as well as the code for Hide/Show functionality.
First the code from Smarty template is as follows :
<table>
<tr height="30">
<td align="right" width="300"><label><b>{'Do you want to use call to
action
?'|signal_on_error:$error_msg:'newsletter_call_to_action_status'}</b>
<strong style="color: red">*</strong></label></td>
<td> <input type="radio"
name="newsletter_call_to_action_status"
id="newsletter_call_to_action_status" value="1"
onclick="select_option(this.value);"
{if $data.newsletter_call_to_action_status=='1' } checked {/if}> Yes
<input type="radio" name="newsletter_call_to_action_status"
id="newsletter_call_to_action_status" value="0"
onclick="select_option(this.value);"
{if $data.newsletter_call_to_action_status=='0' } checked {/if}> No
</td>
</tr>
<tr id="action_link_no" {if $data.newsletter_call_to_action_status==
'1' }style="display:;" {else}style="display:none;"{/if}>
<td colspan="2"> </td>
</tr>
<tr class="action_link_yes" height="30"
{if $data.newsletter_call_to_action_status== '1' }style="display:;"
{else}style="display:none;"{/if}>
<td align="right" width="300"><label><b>{'Enter call to action
text'|signal_on_error:$error_msg:'newsletter_call_to_action_text'}</b>
<strong style="color: red">*</strong></label></td>
<td> <input type="text" name="newsletter_call_to_action_text"
id="newsletter_call_to_action_text"
value="{if $data.newsletter_call_to_action_status == '0'}{else}{$data.newsletter_call_to_action_text}{/if}"
maxlength="50" class="inputfield">
</td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
<tr class="action_link_yes" height="30"
{if $data.newsletter_call_to_action_status== '1' }style="display:;"
{else}style="display:none;"{/if}>
<td align="right" width="300"><label><b>{'Enter call to action
link'|signal_on_error:$error_msg:'newsletter_call_to_action_link'}</b>
<strong style="color: red">*</strong></label></td>
<td> <input type="text" name="newsletter_call_to_action_link"
id="newsletter_call_to_action_link"
value="{if $data.newsletter_call_to_action_status == '0'}{else}{$data.newsletter_call_to_action_link}{/if}"
class="inputfield">
</td>
</tr>
</table>
Now the jQuery code to perform Hide/Show functionality is as follows:
{literal}
<script language="javascript" type="text/javascript">
function select_option(plan){
if(plan == '0'){
$('#action_link_no').fadeOut('fast');
$('.action_link_yes').fadeOut('fast');
}else{
$('#action_link_no').fadeIn('fast');
$('.action_link_yes').fadeIn('fast');
}
}
</script>
{/literal}
Change the markup to use classes and remove the inline JS:
<input type="radio" name="newsletter_call_to_action_status" class="newsletter_call_to_action_status" value="1" {if $data.newsletter_call_to_action_status=='1' } checked {/if}>Yes
<input type="radio" name="newsletter_call_to_action_status" class="newsletter_call_to_action_status" value="0" {if $data.newsletter_call_to_action_status=='0' } checked {/if}>No
Then do this:
$(function() {
$('.newsletter_call_to_action_status').on('change', function() {
var state = $('.newsletter_call_to_action_status:checked').val() == '1';
$('#action_link_no, .action_link_yes')[state?'fadeIn':'fadeOut']('fast');
});
});
How about changing the value attribute to empty when a particular radio button is checked. Am assuming that 'newsletter_call_to_action_text' and 'newsletter_call_to_action_link' are the IDs of the input fields you want to blank, add the code below
$('#newsletter_call_to_action_text, #newsletter_call_to_action_link').val('');
Your javascript code should now look like this.
<script language="javascript" type="text/javascript">
function select_option(plan){
if(plan == '0'){
$('#action_link_no').fadeOut('fast');
$('.action_link_yes').fadeOut('fast');
$('#newsletter_call_to_action_text, #newsletter_call_to_action_link').val('');
}else{
$('#action_link_no').fadeIn('fast');
$('.action_link_yes').fadeIn('fast');
}
}
</script>
or you can also use this
<script language="javascript" type="text/javascript">
function select_option(plan){
if(plan == '0'){
$('#action_link_no').fadeOut('fast');
$('.action_link_yes').fadeOut('fast');
$('#newsletter_call_to_action_text, #newsletter_call_to_action_link').attr('value', '');
}else{
$('#action_link_no').fadeIn('fast');
$('.action_link_yes').fadeIn('fast');
}
}
</script>

How to submit a form from home domain to sub domain?

I want to submit a Form from home page to sub domain page.
Here is my code
html - home page (main domain)
<table>
<tr>
<td>Name</td>
<td><input type="text" name="txtName" id="txtName" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail" id="txtEmail" /></td>
</tr>
<tr>
<td><input name="btnSubmit" id="btnSubmit" value="Submit" type="button"></td>
</tr>
</table>
<form id="getDetails" method="post" action="http://customers.liyyas.com/">
<input type="hidden" name="act" value="Users" />
<input type="hidden" name="hdnName" id="hdnName" />
<input type="hidden" name="hdnEmail" id="hdnEmail" />
</form>
Script
<script type="text/javascript">
$(document).ready(function(){
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("getDetails").submit();
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
});
});
</script>
Sub domain page - user.php
<?php
$act = formatstring($_POST['act']);
switch($act)
{
case "Users":
$Name=$_POST['hdnName'];
$Email=$_POST['hdnEmail'];
print($Name);
exit();
}
?>
In sub domain I am printing the value but its not printing
Is it possible to submit a form from home domain to sub domain?
You need to change the action attribute of the form element from
http://customers.liyyas.com/
to
http://customers.liyyas.com/customers.php
I also assume you know that according to this code
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("getDetails").submit();
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
});
The form will submit BEFORE the values of hdnName and hdnEmail are changed? That may also be a bug for you to quickly switch solve by switching around a few lines. The reason this may be a bug is that when your form submits the page will be reloaded meaning the user will never get to see the new values inserted via JavaScript.
The fix could be
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
document.getElementById("getDetails").submit();
});

how to show textbox if radio button value is checked from database?

I have 3 radio buttons
<input type="radio" name="preferedpaymt" value="CheckMailed" id="CheckMailed1" <?php if($paymnt=="CheckMail") {?> checked="checked" <?php }?> />
(a)Check Mailed
<input type="radio" name="preferedpaymt" value="PayPal" <?php if($paymnt=="PayPal") {?> checked="checked" <?php }?> />
(b)By PayPal
<input type="radio" name="preferedpaymt" value="WireTransfer" id="WireTransfer" <?php if($paymnt=="wireTransfer") {?> checked="checked" <?php }?> />
(c)Wire Transfer To Bank A/c$12
But the value of radiobutton is checked from database . With these 2 tables are associated
if Radiobutton 2 is checked table1 1 should populate id tb1
here the problem is when a value of radio button is checked from database the table does not get populate if you only click it gets populated i want it automatis how can i do it?
jquery code, which I am using
<script type="text/javascript">
jQuery(function () {
jQuery("input[name=preferedpaymt]").change(function () {
if ($(this).val() == "PayPal") {
jQuery("#ppl").slideDown()
} else {
jQuery("#ppl").slideUp();
}
});
});
</script>
Or you culd use:
var chk_val = jQuery("input[name='preferedpaymt']:checked").val();
alert(chk_val);
EDIT: how to use it!
Your inputs:
<input type="radio" name="preferedpaymt" value="CheckMailed" id="CheckMailed1" <?php if($paymnt=="CheckMail") {?> checked="checked" <?php }?> />
(a)Check Mailed
<input type="radio" name="preferedpaymt" value="PayPal" <?php if($paymnt=="PayPal") {?> checked="checked" <?php }?> />
(b)By PayPal
<input type="radio" name="preferedpaymt" value="WireTransfer" id="WireTransfer" <?php if($paymnt=="wireTransfer") {?> checked="checked" <?php }?> />
(c)Wire Transfer To Bank A/c$12
Then lets say you got one tables for each payment choise. ex:
NOTE All tables has the same classname this is because i want to hide all of them by default. The id attribute is set to "table_paymentmethod".
<table class="payment_table" id="table_CheckMailed" height="200" bgcolor="#00FF33" width="100px;" >
<tr>
<td>
You checked Checked mail
</td>
</tr>
</table>
<table class="payment_table" id="table_PayPal" height="200" bgcolor="#FF0000" width="100px;" >
<tr>
<td>
You checked Paypal
</td>
</tr>
</table>
<table class="payment_table" id="table_WireTransfer" height="200" bgcolor="#CCC" width="100px;" >
<tr>
<td>
You checked wiretransfer
</td>
</tr>
</table>
Now to the part where jquery comes in handy.
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('.payment_table').hide(); // Hide all tables by default
var chk_val = jQuery("input[name='preferedpaymt']:checked").val(); // Grab the value of the selected radio
jQuery('#table_'+chk_val).slideDown(1000); // Slides down the right table on page load.
});
</script>
Well there you go, should do the trick for you. Im not the best at explaining but it should be pretty straightforward.

Enable/Disable Submit Button based on radio buttons

I have a form layed out like this:
<form action="join-head-2-head.php" method="POST" enctype="application/x-www-form-urlencoded">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcy" value="y" />
<label for="tandcy">I Have Reviewed The Rules And The Terms & Conditions And Agree To Abide By Them</label></td>
</tr>
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcn" value="n" checked="checked" /><label for="tandcn">I Do Not Agree To The Terms And Condtions And/Or Have Not Read The Rules</label></td>
</tr>
<tr>
<td width="100"><input name="player1" type="hidden" value="<? $session->username; ?>" /></td>
<td><input type="submit" name="join" id="join" value="Take Available Slot" /></td>
</tr>
</table>
</form>
What I am hoping to do is disable the submit button if id="tandcn" is selected, and enable it when id="tandcy". Is there an easy way to do that using javascript?
example http://jsfiddle.net/sWLDf/
$(function () {
var $join = $("input[name=join]");
var processJoin = function (element) {
if(element.id == "tandcn") {
$join.attr("disabled", "disabled");
}
else {
$join.removeAttr("disabled")
}
};
$(":radio[name=player1rules]").click(function () {
processJoin(this);
}).filter(":checked").each(function () {
processJoin(this);
});
});
$(":radio[name='player1rules']").click(function() {
var value = $(this).val();
if (value === "n") {
$("#join").attr("disabled", "disabled");
return;
}
$("#join").removeAttr("disabled");
});
Example on jsfiddle
Lots answers based on jquery (which I recommended to use). Here your form with javascript
<script type="text/javascript">
function disable(id){
document.getElementById(id).disabled = 'disabled';
}
function enable(id){
document.getElementById(id).disabled = '';
}
</script>
<form action="join-head-2-head.php" method="POST" enctype="application/x-www-form-urlencoded">
<table border="0" cellspacing="4" cellpadding="4">
<tr>
<td colspan="2"><input name="player1rules" type="radio" id="tandcy" value="y" onclick='enable("join")' />
<label for="tandcy">I Have Reviewed The Rules And The Terms & Conditions And Agree To Abide By Them</label></td>
</tr>
<tr>
<td colspan="2"><input name="player1rules" onclick='disable("join")' type="radio" id="tandcn" value="n" checked="checked" /><label for="tandcn">I Do Not Agree To The Terms And Condtions And/Or Have Not Read The Rules</label></td>
</tr>
<tr>
<td width="100"><input name="player1" type="hidden" value="<? $session->username; ?>" /></td>
<td><input type="submit" DISABLED name="join" id="join" value="Take Available Slot" /></td>
</tr>
</table>
</form>
However more elegant way is use jquery.
$(document).ready(function(){
if($('#tandcy').is(':checked')){
$('#join').attr('disabled','disabled');
}
if($('#tandcn').is(':checked')){
$('#join').removeAttr('disabled','disabled');
}
$('#tandcn').click(function(){
$('#join').attr('disabled','disabled');
});
$('#tandcy').click(function(){
$('#join').removeAttr('disabled','disabled');
})
});
Try this....
you need jquery for this,....
$("#tandcn #tandcy").livequery('change', function(event){
if ($('#tandcn').is(":checked"))
{
$('#join').hide();
//or
$('#join').attr("disabled", false);
}
else($('#tandcy').is(":checked"))
{
$('#join').show();
//or
$('#join').attr("disabled", false);
}
});

Categories