php code to make a fieldset invisible in html page - php

I have a fieldset in the html page.I need the field set to be invisible during a specific radio button click.
$cat is the radiobutton name,Dessert is the value.This radio button is inside the field set
The following code does not work.
How to do this through php code.I don't need to be done with javascript or jquery.
Here is the Code :
<?
if((!empty($cat) && $cat==="Dessert"))
{
echo "<fieldset id='typeradio' hidden='hidden'>";
echo"success";
} >?
How can i do this ?

You can disable it if you don't want it to work
<fieldset id="typeradio" disabled>
or hide it using CSS
<fieldset id="typeradio" style="display: none;">

Try This
It will only work if you post back your page and set the $cat property there otherwise you will have to do it via Jquery or JavaScript or ajax request. By post back i mean full page reload and server will do all checks and apache server will generate Html for this if condition
<?
if((!empty($cat) && $cat==="Dessert"))
{
echo "<fieldset id='typeradio' style='display:none'>";
echo"success";
} >?

//this example.php
<?
if(isset($_POST['opt'])){
$cat=$_POST['opt'];
if($cat==="Dessert")
{
echo "<form name="frm" action='example.php' method="post"><fieldset id='typeradio' hidden='hidden'><input type='radio' name='opt' value="Dessert" onclick="this.form.submit();"></fieldset></form>";
echo"success";
}
}
else
{
echo "<form name="frm" action='example.php' method="post"><fieldset id='typeradio' ><input type='radio' name='opt' value="Dessert" onclick="this.form.submit();"></fieldset></form>";
}
>?

//there was some quotes mistakes in previous code try this
<?php
if(isset($_POST['opt'])){
$cat=$_POST['opt'];
if($cat==="Dessert")
{
echo "<form name='frm' action='example.php' method='post'><fieldset id='typeradio' hidden='hidden'><input type='radio' name='opt' value='Dessert' onclick='this.form.submit();'></fieldset></form>";
echo"success";
}
}
else
{
echo "<form name='frm' action='' method='post'><fieldset id='typeradio' ><input type='radio' name='opt' value='Dessert' onclick='this.form.submit();'></fieldset></form>";
}
?>

Related

Need javascript confirm box dialogue & delete images when press ok

<?php
require "../db/dbconfig.php";
$gal=mysql_query("select * from gallery");
$numrows=mysql_num_rows($gal);
if($numrows>=1)
{
echo "<form action='delete.php' method='post' name='f2' id='f2'>";
echo '<table id="rqst" style="display:block;" border="0" cellpadding="12" cellspacing="3" width="500px">';
echo "<tr><th>Tick to select</th><th>Images in Gallery</th></tr>";
while($row=mysql_fetch_array($gal)
{
$imgfile=$row['ImgName'];
$Image="<img src=gallery/".$imgfile." width='230px' height='150px'/>";
$img_name=$imgfile;
echo "<tr>";
echo "<td><input type='checkbox' name='imgs[]' value='$img_name'></td><td>".$Image."</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan='3' align='right'>";
echo "<input type='submit' value='Delete' name='del'></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
?>
This is my code....This will display images from gallery and checkboxes associated with them. When I click delete button with unchecked checkboxes an alert should come like this "Please check at least one checkbox"..How to do that??
My next problem is,,when I click delete button after checked checkbox, alert should come like this=" Do you want to delete?? "...If clicked Ok,the image must be deleted else do nothing...Please help ...Thanks in advance....
check this below link for validation using jquery:
http://jsfiddle.net/susheel61/U3Unk/2/
<form id="form">
First name: <input type="checkbox" name="firstname" class="check"><br>
<button>test</button>
</form>
$("button").on("click",function(e){
var status = $(".check").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
});
Yes, you can do it by javascript or jquery to validate whether your atleast one checkbox is select or not. So, for that you need to give a common class for all checkbox as example
<input type="checkbox" name="firstname" class="addchk">
Now in your submit button call a javascript function which validate the matter.
<input type='button' value='Delete' name='del' onclick='delete_checked()' />
Now write a function to validate whether any checkbox is selected.
function delete_checked()
{
var status = $(".addchk").is(":checked");
e.preventDefault();
if(!status){
alert("this is not checked");
}
else
{
// SUbmit yout form
}
}

Immediately update database on click of radio

I have several forms with radio buttons on a page. My goal is to have the database updated immediately on the click of a radio button. Here is what I have put together so far, but I'm not sure where to go from here.
<? include 'dbconnect.inc.php';?>
<script type="text/javascript">function do_submit(){document.forms['decision'].submit();}</script>
<? $result = $mysqli->query("SELECT * FROM items");
while( $row = $result->fetch_assoc() ){ ?>
<form name='decision' method='post' action='action.php'>
<label>Keep:</label><input type='radio' name='dec' value='keep' onchange='do_submit()' <? if($row['Dec1']=='keep'){echo "checked='checked'";} ?> >
<label>Donate:</label><input type='radio' name='dec' value='donate' onchange='do_submit()' <? if($row['Dec1']=='donate'){echo "checked='checked'";} ?> >
<label>Sell:</label><input type='radio' name='dec' value='sell' onchange='do_submit()' <? if($row['Dec1']=='sell'){echo "checked='checked'";} ?> >
<label>Trash:</label><input type='radio' name='dec' value='trash' onchange='do_submit()' <? if($row['Dec1']=='trash'){echo "checked='checked'";} ?> >
<label>Give To:</label><input type='text' name='dec' size='15' onchange='do_submit()' <? if($row['Dec1']!='keep' || $row['Dec1']!='donate' || $row['Dec1']!='sell' || $row['Dec1']!='trash'){echo $row['Dec1'];} ?> >
<input type='hidden' name='Id' value='<? echo $row['Id']; ?>' />
</form>
<? } ?>
action.php
<? $mysqli->query("UPDATE items SET Dec1 = '{$_POST['dec']}' WHERE Id = '{$_POST['$Id']}'") or die(mysqli_error($mysqli)); ?>
Am I anywhere close? Can someone help me through this?
This is untested, but you'd probably want to go with something like this (AJAX + jQuery). Would need quite a bit added to it to pass all your variables through properly.
$(":radio").click(function(){
$("#result").html(ajax_load);
$.post(
loadUrl,
{myvariable: "myvalue", myothervariable: "myothervalue"},
function(responseText){
$("#result").html(responseText);
},
"html"
);
});
Here's a good, supplementary read for you as well: 5 Ways to Make Ajax Calls with jQuery
Bind an ajax onclick event to send the command to your action script.
Inline php is not the way to go.

Close the popup and refresh the parent page

home.php
<?php print"$i->sm_jo<br>$i->sm_custo_name"; ?>
ppc-mngt_transfer.php
<script type="text/javascript">
function refreshAndClose() {
window.close();
window.opener.location.reload(true);
}
</script>
<body onbeforeunload="refreshAndClose();">
<form name='mainform' method='post' action='do.php?ppc/workload'>
<?php
print "<h1>COPY TO WORKLOAD</h1>";
print "<table class='clean_form'>";
print "<tr><td>J.O. Number</td><td><input name='ppc_jo' value='$i->sm_jo' style='width:250'></td></tr>";
print "<tr><td>Customer Name</td><td><input name='ppc_custo_name' value='$i->sm_custo_name' style='width:250'></td></tr>";
print "<tr><td>Description</td><td><input name='ppc_subject' value='$i->sm_subject' style='width:250'></td></tr>";
print "<tr><td>Date Start</td><td><input name='ppc_dpdate' value='$i->sm_dpdate' style='width:250'></td></tr>";
print "<tr><td>Duration</td><td><input name='ppc_proj_duration' value='$i->sm_proj_duration' style='width:250'></td></tr>";
print "<input type='hidden' name='ppc_m_id' value='$id'>";
print "</table>";
print "<tr><input type='checkbox' name='ppc_mark' value='ok' checked/></tr>";
?>
<input type="submit" value="submit">
</form>
</body>
I think my code does this > REFRESH then Close .... What I need is Close after clicking the submit button in the pop-up then Refresh the parent page. Please help me I have a poor knowledge in Javascript.
<form name='mainform' method='post' action='do.php?ppc/workload' onsubmit="refreshAndClose();">
function refreshAndClose() {
window.opener.location.reload(true);
window.close();
}
Just make sure the form is really submitted, if not, we need to tweak it a bit.
I would actually to the window.close() in the do.php file. So u can also handle submission errors, if they occur.

how to send a text box value without submitting form in php?

I want to refresh the same page and display the entered value in text box on the same page after clicking the link or button.
I have the following code:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo " <input type='text' name='myTextField'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$studen_no;
}
?>
Please guide me how to achieve the goal and whats the error my code.
<?php
echo '<h3>Fee Payment</h3>';
if(isset($_POST['myTextField']))
$value=$_POST['myTextField'];
else
$value='';
echo "<form id='myFormId' name='myFormName' method='post'>";
echo " <input type='text' name='myTextField' value='$value'>";
echo "<a href='{$_SERVER['PHP_SELF']}?student_no=myTextField'> Search Student</a>";
You also need a submit button in the form, and a form close tag.
What you want is AJAX. I am just providing you an example of how you can proceed by using jQuery, but you can use any other library / framework or any other way also. You can check this article also for more usability details.
In the main page:-
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'>";
echo "<input type='text' name='myTextField' id='myTextField' />";
echo 'Search Student';
echo "</form>";
?>
<script type="text/javascript">
$('#inline_submit_a').click(function(evt){
$.ajax({
type: "GET",
url: "handler.php",
data: {text:$('#myTextField').val()}
});
evt.preventDefault();
return false;
});
</script>
In the "handler.php" page:-
<?php
if (!(isset($_GET[student_no])))
{
echo "No Student is available.";
}
else
{
$student_no = $_GET['student_no'];
echo "Student NO:".$student_no;
}
?>
You can write all the logic related to the database in the "handler.php" page.
Hope it helps.
You're using a link to submit the form, but you should be using a submit button.
<input type="submit" value="Search student" />
You didn't close the <form> tag.
You're using $_GET[student_no] which will make PHP look for a definition of student_no. Since it's a string, express it as one. $_GET['student_no'] is better.
Why can't you submit the form? Do you want to have a link for searching instead of a button?
You could configure the link to submit the form via javascript and change the form action to GET like this:
<?php
echo '<h3>Fee Payment</h3>';
echo "<form id='myFormId' name='myFormName'
action='{$_SERVER['PHP_SELF']}' method='GET'>";
echo " <input type='text' name='student_no'>";
echo "<a href='javscript:document.forms.myFormName.submit()'>
Search Student</a>";
...

Multiple Checkboxes with same name validator

I have this form with multiple checkboxes with same name and all of checkboxes are read from a database:
echo "<FORM action='check.php' method=POST>"; ?>
<div style='color:#414141; font-size:15px;'>
<?php
while($rows=mysql_fetch_array($result)){
?>
<input name="check[]" type="checkbox" id="checkk[]" value="<? echo $rows['ID']; ?>"><? echo $rows['checkboxname']; ?>
<BR>
<?php
}
echo "</div>";
echo "<input name='' type='submit' id='get' value='Next'>";
echo "</FORM>";
I need a form validation to the next file.When I click on Submit Button(Next),show me the next page only if I checked a checkbox.I preffer a javascript validation method.
You can add a click event listener for the submit button and check if atleast one checkbox is checked and submit if true.
Something like below should work,
Try below,
echo "<input name='' type='submit' id='get' value='Next' onclick='return validateCheckbox()'>";
<script>
function validateCheckbox () {
var checkboxes = document.getElementsByName('check[]'); //selected by name since OP wanted to select by name
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return true; // found 1 checked checkbox
}
}
return false; //none checked, so cancel submit
}
</script>

Categories