JavaScript enable button not working in php - php

I have used Javascript to enable or disable a button, depending on the input value. I'm new to PHP, so I don't know why this isn't working. Here's the PHP code:
<?php
session_start();
include_once('header.php');
include_once('functions.php');
$_SESSION['userid'] = 1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Microblogging Application</title>
</head>
vbody>
<?php
if (isset($_SESSION['message'])){
echo "<b>". $_SESSION['message']."</b>";
unset($_SESSION['message']);
}
?>
<form method='post' action='add.php'>
<p>Your status:</p>
<textarea name='body' rows='5' cols='40' wrap=VIRTUAL></textarea>
<input type="text" name="age" />
<input type="text" name="poscode" />
<input type="submit" name="submit" value="Post" disabled="disabled"/>
</form>
<script type="text/javascript">
$('input[name="age"], input[name="poscode"]').change(function(){
if ($(this).val())
{
$("input[name='submit']").removeAttr('disabled');
}
});
</script>
<?php
$posts = show_posts($_SESSION['userid']);
if (count($posts)){
?>
<table border='1' cellspacing='0' cellpadding='5' width='500'>
<?php
foreach ($posts as $key => $list){
echo "<tr valign='top'>\n";
echo "<td>".$list['body'] ."<br/>\n";
echo "<small>".$list['stamp'] ."</small></td>\n";
echo "</tr>\n";
}
?>
</table>
<?php
}else{
?>
<p><b>You haven't posted anything yet!</b></p>
<?php
}
?>
</body>
</html>
How do I change it so when the value of the input = "foo", the button is now clickable.
Thanks in advance
-Ben

I can't make sense of your question, but if you're asking what I think you're asking (which is "how do I enable the button if the form is populated on page load?"), change your script to the following:
$.ready(function(){
$('input[name="age"], input[name="poscode"]').change(function(){
if ($(this).val())
{
$("input[name='submit']").removeAttr('disabled');
}
}).change();
});
I added the change call to trigger the change event handler, and wrapped it in jQuery's ready handler.

Try using the attr function like below
$('#element').attr('disabled', true);
Or
$('#element').attr('disabled', false);
Because different browsers treat the disbaled attribute differently, using true and false lets jquery handle the cross browser issues

Related

unable to post values of multiple check boxes created form while loop

My question maybe has been asked many times before but i couldn't apply the given solutions on my project yet
I'm trying to post only the selected children check-boxes not the parents
my page code is:
<?php $db = mysqli_connect('localhost', 'root', '123', 'test'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="jquery.min.js"></script>
<script src="cbFamily.js"></script>
</head>
<body>
<form name="myform" method="post" action="test.php">
<section class="demo2" style="margin-top:2px;">
<?php
$query=mysqli_query($db,"SELECT * FROM tblmainjobs");
while ($row = mysqli_fetch_assoc($query)):
?>
<section>
<h3><label><input type="checkbox" /> <?php echo $row["mainjob"]; ?>
</label></h3>
<?php
$query2=mysqli_query($db,"SELECT userjob FROM users WHERE
'".$row["mainjob"]."' = users.mainjob GROUP BY users.userjob");
while ($row2 = mysqli_fetch_assoc($query2)):
?>
<div class="children">
<label><input type="checkbox" name="checkbox" value="<?php echo
$row2["userjob"]; ?>"/> <?php echo $row2["userjob"]; ?></label>
<?php endwhile; ?>
</section>
<?php endwhile; ?>
<script type="text/javascript"> <!-- this function for selecting all
children checkboxes once the parents checkbox bieng selected -->
$("h3 input:checkbox").cbFamily(function (){
return $(this).parents("h3").next().find("input:checkbox");
});
</script>
</section>
<br>
<div><input class="submit" type="submit" name="submit" value="submit"/>
</div>
</form>
</body>
</html>
my page looks like this
test.php
my entire project is attached here
https://www.sendspace.com/file/6ikcaa
Use array:
<input type="checkbox" name="checkbox[]" value="<?php echo
$row2["userjob"]; ?>"/> <?php echo $row2["userjob"]; ?>
notice the [] in the name attribute.
You could use javascript to disable the "parent" checkboxes before they get posted to the server.
Add a class to your parent checkboxes to make for an easy jquery selector:
<input type="checkbox" class="parent" />
Then some javascript to disable the checkboxes:
$(".parent").prop("disabled",true);

Notice: Undefined index Radio & Textbox

I have created two radio button and the scenario is as below.
1. Non-Dist checked, the textbox should be read-only
2. Dist checked, the textbox enable to fill in
However, I faced and error stated as below when Non-Dist checked and submit the form.
Notice: Undefined index: cust_edc_code in
C:\xampp\htdocs\spa317\inv.php on line 49
Please advice what do I missed. Thanks in advance.
CODE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create SPA Invoice</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<form action="" method="post" name="form_spa_inv">
<table class="tb_create_spa_inv">
<tr>
<td>Status* : </td>
<td>
<!--To set status's radio button and save it in database-->
<input type="radio" name="cust_status" id="cust_status1" class="status_dis_text" value="Non-Distributor" <?php if (isset($cust_status) && $cust_status=="Non-Distributor") echo "checked";?> />Non-Dist
<input type="radio" name="cust_status" id="cust_status2" class="status_dis_text" value="Distributor" <?php if (isset($cust_status) && $cust_status=="Distributor") echo "checked";?> />Dist
<!--EDC code for distributor-->
<input type="text" name="cust_edc_code" size="10" />
<script>
$(function() {
$('input[class=status_dis_text]').change(function()
{
if ($(this).is(':checked')) {
$('input[name=cust_edc_code]').attr('disabled', 'disabled');
$(this).next().removeAttr('disabled');
}
});
$('input[class=status_dis_text]:first').attr('checked', 'checked').change();
});
</script>
</td>
</tr>
</table>
<p style="text-align: center;"><input type="submit" name="Submit" value="Create">
</form>
</body>
</html>
<?php
//including the database connection file
include_once("config_db.php");
if(isset($_POST['Submit']))
{
$cust_status=$_POST['cust_status'];
$cust_edc_code=$_POST['cust_edc_code'];
//insert data to database
$insert_cust=mysql_query("INSERT INTO tb_cust(cust_status,cust_edc_code) VALUES('$cust_status','$cust_edc_code')");
}
?>
<script>
$(function() {
$('input[class=status_dis_text]').change(function() {
if ($(this).is(':checked')) {
$('input[name=cust_edc_code]').attr('disabled', 'disabled');
$(this).next().removeAttr('disabled');
}
});
$('input[class=status_dis_text]:first').attr('checked', 'checked').change();
});
</script>
That jquery code seems to disable your input with name 'cust_edc_code', hence it is not getting sent to the server.
If this line isn't doing something really really useful, remove it:
$('input[name=cust_edc_code]').attr('disabled', 'disabled');
or replace it with this:
$('input[name=cust_edc_code]').attr('readonly', 'readonly');
Setting disabled on an input field prevents it from being sent to the server. If you only want to prevent the user from modifying the field, use readonly.

Return php value from ComboBox

i create a combobox with sql values but how i know what the value selected?
this is my code, have a lot of scrap but is my tests :)
I link to send the selected option to another php file already created.
<?php
require_once('auth.php');
require_once('config.php');
require_once('no-cache-headers.php');
require_once('functions.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Nova Mensagem</title>
<link href="Formatacao.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Bem-vindo <?php echo $_SESSION['USERNAME'];?></h1>
<form id="regForm" name="regForm" method="post" action="verificarMensagem.php">
<table width="300" border="0" align="center" cellpadding="2" cellspacing="0">
<tr>
<?php
mysql_connect('localhost','comunicat','comunicat');
mysql_select_db('Comunicat');
$iduser =$_SESSION['SESS_MEMBER_ID'];
$query="Select * from Usuarios where id <> '$iduser'";
$resultado=mysql_query($query);
echo '<select name=”Nome”>';
while($linha=mysql_fetch_array($resultado))
{
echo '<option value="' . $linha['ID'] . '">' . $linha['Nome'] . '</option>';
}
echo '</select>';
?>
<textarea rows="4" cols="50" name="mensagem" id="mensagem">
</textarea>
<td> </td>
<td><input type="submit" name="Submit" value="Enviar" /></td>
</tr>
</table>
</form>
</body>
</html>
You can get value of select element in php by using $_POST[name-of-element]:
<?php
echo $_POST['Nome'];
?>
That works also with checkboxs,radios,etc
When the form that contains your "combobox" is submitted, you can get the selected value from your combobox with the line of code below:
$val = $_POST['Nome']; // if the form was submitted using post method
$val = $_GET['Nome']; // if the form was submitted using get method
NB
Do not use mysql_* no more, it is officially deprecated. Use mysqli or PDO instead.

Phonebook in php without using MySQL

I just wanted to create a simple phonebook using php...I used the following code....but one entry overwrites another plz help...I want to do this without using MySQL
<?php
session_start();
if(isset($_SESSION['views']))
{
$_SESSION['views']=$_SESSION['views']+1;
}
else
{
$_SESSION['views']=1;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Phonebook</title>
</head>
<body>
<form action="index1.php" method="post" style="border:thin">
Name: <input type="text" name="varname" style="border:dotted" />
<br/>
Roll Number:<input type="text" name="varroll" style="border:dotted" />
<br />
Phone Number: <input type="text" name="varno" style="border:dotted" />
<br/>
<input type="submit" name="submit" value="Register" /><br/>
</form>
<?php
$test1[$_SESSION['views']]=$_POST['varname'];
$test2[$_SESSION['views']]=$_POST['varroll'];
$test3[$_SESSION['views']]=$_POST['varno'];
for($j=1;$j<=$_SESSION['views'];$j++)
{
echo $test1[$j]." ".$test2[$j]." ".$test3[$j];}
echo "<br/>";
echo "No. of page views=".$_SESSION['views'];
?>
</body>
</html>
You could write it to a text file with | seperating each value or you could use an ini or xml file
You cannot just use $_SESSION, as it will be emptied when the user closes the browser.
Best to do something like this (sample untested code)
//loading
$data = unserialize( file_get_contents( 'mydata.txt' ) );
//editing
$entry = array();
$entry['roll']=$_POST['varroll'];
$entry['name']=$_POST['varname'];
$data[] = $entry;
//saving
file_put_contents( 'mydata.txt', serialize($data) );

Problem with PHP 'PHP_SELF'

I am having a bit of trouble. It does not seem to be a big deal, but I have created a page that the user can edit a MySQL database. Once they click submit it should process the php within the if statement and echo 1 record updated. The problem is that it does not wait to echo the statement. It just seems to ignore the way I wrote my if and display the whole page. Can anyone see where I went wrong.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<?php require("serverInfo.php"); ?>
<?php
$res = mysql_query("SELECT * FROM cardLists order by cardID") or die(mysql_error());
echo "<select name = 'Cards'>";
while($row=mysql_fetch_assoc($res)) {
echo "<option value=\"$row[cardID]\">$row[cardID]</option>";
}
echo "</select>";
?>
Amount to Add: <input type="text" name="Add" />
<input type="submit" />
</form>
<?php
if(isset($_POST['submit']));
{
require("serverInfo.php");
mysql_query("UPDATE `cardLists` SET `AmountLeft` = `AmountLeft` + ".mysql_real_escape_string($_POST['Add'])." WHERE `cardID` = '".mysql_real_escape_string($_POST['Cards'])."'");
mysql_close($link);
echo "1 record Updated";
}
?>
<br />
<input type="submit" name="main" id="main" value="Return To Main" />
</body>
</html>
if(isset($_POST['submit']));
1) Should not have a semicolon after it.
2) $_POST['submit'] is not set. You have to set a name on your submit button and give it a value. Just setting the type to 'submit' does not return a value for $_POST['submit'] in PHP.
You've got a ; after your if statement.
I noticed that you have two submit buttons and I assume that you are using the first one.
Try giving it a name="submit" and a value too.
Of course it doesnt. PHP runs in the server side, not in browser!
Open your page source. There is no PHP. Nothing to wait.
You need another page to send your form to.
And it is a big deal. It's a cornestone of understanding how the web does work.

Categories