How to use jquery function and isset() together via a submit button? - php

First I have a row and a submit button on a table like this:
<tr id="date">
<td class="left" valign="top">
<label>Some content: </label>
</td>
<td class="right">
<? if ($akis_kademe == "ekle") { ?>
<input type="submit" name="submit5" class="btn" value="Tarih ekle" />
<? } ?>
</td>
</tr>
And isset method...
if(isset($submit5)){
?>
<tr id="ek_takvim">
<td class="left" valign="top">
</td>
<td class="right">
</td>
</tr>
<? }?>
Jquery function...
$(document).ready(function(){
$("submit5").click(function(){
$(ek_takvim).hide();
});
});
All I wanna do that when I click the button I wanna add isset section on my table and also hide the button at the same time.

Try the following
<?php $takvimDisplay = isset($submit5)?"block":"none"; ?>
<tr id="ek_takvim" style="display:<?php echo $takvimDisplay ?>">
<td class="left" valign="top">
</td>
<td class="right">
</td>
</tr>
$(document).ready(function(){
$("input[name=submit5]‌").click(function(){
$("#ek_takvim").show();
$(this).hide();
});
});

I assume you add the row ek_takvim right after the button row and hide them by css or onLoad-Event. Then this will do the rest of the work.
$(document).ready(function(){
$("input[name=submit5]‌").click(function(){
$("#ek_takvim").show();
$(this).hide();
});
});
I like this approach more:
HTML
<input type="submit" name="submit5" onclick="goodFunctionName()" class="btn" value="Tarih ekle" />
JS
function goodFunctionName(){
$("#ek_takvim").show();
$("input[name=submit5]").hide();
}
JsFiddle: https://jsfiddle.net/c0f7bn96/
Talk to me: https://jsfiddle.net/c0f7bn96/#&togetherjs=5BxtqPPlE1

Related

Unable to post values on a page using jquery ajax post method in wordpress custom plugin

I am new to wordpress and i have just created my first ever plugin. This plugin contains a form. Whenever i make jQuery AJAX Post Request to submit the form, i get this error in alert box.
Call to undefined function add_shortcode() in <b>J:\xampp\htdocs\wordpress\wp-content\plugins\auc_result_fetcher\includes\search.php</b> on line <b>22</b><br />
I am using Wordpress 3.8.1. The add_shortcode() function successfully displays the form wherever i want to be displayed. But whenever i make jQuery AJAX Post Request, i get the above error message.
EDIT
My Code Snippet.
add_action('init', function() {
add_shortcode('search_form', 'print_search_form');
});
add_action('init', 'fetch_grades');
add_action('init', 'fetch_search_options');
function print_search_form(){
?>
<div class="auc-search-form">
<div id="error">
<div id="select-class-error"></div>
<div id="select-search-by-error"></div>
<div id="search-field-error"></div>
</div>
<form id="searchForm" method="POST" action="">
<table>
<tr>
<td style="text-align:right;"> <span>Class:</span> </td>
<td><select id="select-class" name="search-class">
<option>Select Class</option>
<?php
$grades = fetch_grades();
foreach($grades AS $v){
echo "<option>".$v->grade_title."</option>";
}
?>
</select>
</td>
<td id="class-select"></td>
</tr>
<tr id="previous-row">
<td style="text-align:right;"> <span>Search By:</span> </td>
<td>
<select id="search-by" name="search-by">
<option>Select Choice</option>
<?php
$grades = fetch_search_options();
foreach($grades AS $v){
echo "<option>".$v->search_title."</option>";
}
?>
</select>
</td>
</tr>
<tr id="search-row">
<td id="search-by-option-label" style="text-align:right;"><span></span></td>
<td id="search-by-field"><input type="text" name="searchBy" id="search" /></td>
</tr>
<tr >
<td ></td>
<td>
<input type="hidden" id="url" name="url" value="<?php echo SEARCH_RESULTS; ?>">
<input type="button" id="search-button" value="Search" />
<input type="reset" name="searchBy" id="reset" value="Cancel"/>
</td>
</tr>
</table>
</form>
</div>
<?php
}
Please help me out. Thanks.

using a div as a radio button

<div>
<table class='accor'>
<tr>
<td>Roll No.</td>
<td>12801</td>
</tr>
<tr>
<td>Name</td>
<td>XXXX</td>
</tr>
<tr>
<td>Class</td>
<td>MS</td>
</tr>
</table>
</div>
i want the above code as a radio button. the above code is running inside a php while loop with the data supplied from a database. (i also need to use checked=checked to match a predefined value.)
i am using the jquery ui buttonset style with the radio.
how can i achieve that?
$().buttonset takes all the input type='radio' elements inside the selector and makes a stylized button set out of them. For each of these elements, the associated label is placed on the display of the resulting button. So to have buttons with labels that aren't just a single line of text, just place the contents inside the label.
<form>
<div id="radio">
<input type="radio" id="radio1" name="radio" />
<label for="radio1">
<div>
<table class='accor'>
<tr>
<td>Roll No.</td>
<td>12801</td>
</tr>
<tr>
<td>Name</td>
<td>XXXX</td>
</tr>
</table>
</div>
</label>
<input type="radio" id="radio2" name="radio" checked="checked" />
<label for="radio2"><div>
<table class='accor'>
<tr>
<td>Roll No.</td>
<td>12801</td>
</tr>
<tr>
<td>Name</td>
<td>XXXX</td>
</tr>
</table>
</div>
</label>
</div>
</form>
JS:
$(function() {
$("#radio").buttonset();
});
try following (embed your code as you want):
$('.accor td').on("click",function(){
$('td').css('background-color','#eee');
var rowNumber = $(this).closest('tr').index();
setSelected(rowNumber);
});
function setSelected(rowNumber){
$('tr').eq(rowNumber).find('td').css('background-color','#ccc');
}
setSelected(2);
working fiddle here: http://jsfiddle.net/ZEwpE/2/
i hope it helps.
or another option to do that is:
$('.accor td').on("click",function(){
$('td').css('background-color','#eee');
var tr = $(this).parent();
$(tr).children().css('background-color','#ccc');
});
Get click event
Get parent (in your case it's )
Set css style to all childs of our
But it's only alternate (very similar) method:
jsfiddle

Change the color of the button when click on the Checkbox-Dynamically created content

I am listing down all the departments in my db like this.
Select * from department
and I am using a pagination to list down departments. Only 8 departments will be listed.
<?php
while($row_department = mysql_fetch_array($result))
{
?>
<?php echo $row_department['department_name'];?>
<div>
<table width="500" border="0" cellspacing="1" cellpadding="1">
<tr>
<td>Department Description</td>
<td> <textarea name="txt_department_description" cols="50" rows="7" id="txt_department_description"><?php echo $row_department['department_description']; ?> </textarea></td>
</tr>
<tr>
<td width="341"> <input type="checkbox" name="option" id="option" onClick="javascript:enableButton();">Click to Delete Department<br></td>
</tr>
<tr>
<td> <input name="enter_department" id="enter_department" type="submit" value="Update Department" /><input name="Reset" type="reset" value="Cancel" />
<input name="delete_department" id="delete_department" type="submit" value="Delete Department" />
</td>
</tr>
</table>
</div>
<?php } ?>
by this page user can Update or Delete selected departments.
Delete button will be enable when user click on the Check Box that above listed.
This javascript function will be called onclick event.
<script>
function enableButton() {
if(document.getElementById('option').checked){
alert('1');
document.getElementById('delete_department').disabled='';
document.getElementById('enter_department').disabled='true';
document.getElementById('enter_department').style.backgroundColor ='Grey';
document.getElementById('delete_department').style.backgroundColor = 'green';
} else {
document.getElementById('delete_department').disabled='true';
document.getElementById('enter_department').style.backgroundColor ='';
document.getElementById('delete_department').style.backgroundColor = '';
}
}
</script>
when we click on the checkbox on first record, function is working, but if we try to click the second record checkbox, function is not working.
Can anyone please look in this?
anything I am doing wrong.

Passing selected value to PHP form using <a> links instead of select/option

I'm creating a dropdown menu where each selection will be wrapped in links. The value selected needs to be passed to a php contact form but I'm not sure how to do this.
Normally, I'd just use the select-option method but I want to style the menu completely from scratch so is it possible to get the same functionality from just a regular html dropdown menu?
The desired values in question are in the 'Who are you?' section...
<div id="contact_info">
<form name="htmlform" method="post" action="./contact.php">
<table>
<caption>CONTACT</caption>
<tr>
<td valign="top">
<label for="your_name">Name:</label>
</td>
<td valign="top">
<input type="text" name="your_name" maxlength="50" size="23">
</td>
</tr>
<tr>
<td valign="top">
<label for="email">Email:</label>
</td>
<td valign="top">
<input type="text" name="email" maxlength="50" size="23">
</td>
</tr>
<tr>
<td valign="top">
<label>Who are you?:</label>
</td>
<td valign="top">
<a>
<p style="background:#ffffff;">I am...</p>
</a>
<a href="">
<p>an artist.</p>
</a>
<a href="">
<p>an educator.</p>
</a>
<a href="">
<p>a student.</p>
</a>
<a href="">
<p>a curator.</p>
</a>
<a href="">
<p>other...</p>
</a>
</td>
</tr>
</table>
<table>
<tr>
<td style="width:120px !important;"></td>
<td valign="top">
<label for="comments">Message:</label>
</td>
</tr>
<tr>
<td style="width:120px !important;">
<input type="submit" value="Submit">
</td>
<td valign="top">
<textarea name="comments" maxlength="1000" cols="44" rows="19"></textarea>
</td>
</tr>
</table>
</form>
</div>
You will need javascript to do this. As you have stated that your are not that fluent in javascript, I will use jQuery here to illustrate what you need to do in an easy way. Note that it can be done in plain javascript or using another library as well.
First you need to add a hidden field that will contain the value you want to submit with the rest of the form and then you need to use javascript to get the value of the selected option / click and put it in the hidden field:
I will also add an ID to the table cell to make selecting the right element and catching the events easier:
html:
<td valign="top" id="I_am_a">
<input type="hidden" name="I_am_a">
<a><p style="background:#ffffff;">I am...</p></a>
<p>an artist.</p>
<p>an educator.</p>
<p>a student.</p>
<p>a curator.</p>
<p>other...</p>
</td>
javascript (assuming jQuery is included):
$("#I_am_a a").on('click', function(e) {
e.preventDefault(); // prevent the default click action
$("#I_am_a input")
.val( $(this).find("p").text() ); // assign the text contents of the
// paragraph tag in the clicked a
// to the input element
});
Something like this should do it.

form 2 button + javascript issue

<form action="index.php?page=checkin" method="post" name="regForm">
<div id="First_1">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellpadding="0" cellspacing="5">
<tr>
<td style="padding: 5px;">
Fullständiga namn:
</td>
<td>
<input name="full_name" type="text" id="full_name" class="required">
</td>
</tr>
<tr>
<td style="padding: 5px;">
Email:
</td>
<td>
<input name="usr_email" type="text" id="usr_email3" class="required">
</td>
</tr>
<tr>
<td style="padding: 5px;">
Sex:
</td>
<td>
<select name="sex">
<option value="male">
Kille
</option>
<option value="female">
Tjej
</option>
</select>
</td>
</tr>
<td>
<input type="submit" id="continue" value="Fortsätt">
</td>
</table>
</td>
</tr>
</table>
</div>
<div id="Next_2" style="display: none">
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<table cellpadding="0" cellspacing="5">
<tr>
<td style="padding: 5px;">
Lösenord:
</td>
<td>
<input name="pwd" type="password" class="required password" id="pwd">
</td>
<td>
En gång till..
</td>
<td>
<input name="pwd2" id="pwd2" class="required password" type="password">
</td>
<td>
<input name="doRegister" type="submit" id="doRegister" value="Register">
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
$(function() {
$("#continue").click(function() {
$.ajax({
type: "POST",
url: "dbc.php",
data: {
check: 'First',
full_name: $('#full_name').val(),
usr_email: $('#usr_email3').val()
},
success: function(msg) {
if (msg == '1') {
$("#First_1").hide();
$("#Next_2").toggle();
} else {
alert(msg);
}
}
});
return false;
});
});
</script>
Thats my form. You fill 3 form elements to start with, then you press the Continue button, and then it sends ajax call to check the fields. If everythings OK (if output = 1 from ajax call), Next_2 gets toggled on, where you enter your password twice. After that you press on the final button Register, which should do "action=index.php?page=checkin", but it doesnt, when i press register it just get blank, and i can see in firebug that it sends another ajax call which i cannot understand because the button doesnt have id="continue" but id="doRegister"
Since the "Continue" button doesn't actually submit the form, try declaring it as a normal button instead of a submit button, like this:
<input type="button" id="continue" value="Fortsätt">
I'm guessing that having two submit buttons on the same form is somehow causing interactions with the handler.
I think you should make the "Continue" button as type="button" and not submit
<input type="button" id="continue" value="Fortsätt">

Categories