Notice: Undefined index Radio & Textbox - php

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.

Related

Get sum with php

I have following codes
<html>
<head>
<title>Javascript function </title>
<style type="text/css">
.box
{
width:400px;
background-color:#F0F8FF;
}
h4
{
color:#09F
}
</style>
<script type="text/javascript">
function hello(){
var xx=eval(form1.text1.value);
var yy=eval(form1.text2.value);
form1.text3.value=xx+yy
}
</script>
</head>
<body onLoad="form1.text1.focus()">
<center>
<div class="box">
<h1 style="color:#2c80d3 ">Javascript Function</h1>
<table border="0" cellspacing="1" cellpadding="1" width="25%">
<form name="form1" action="textboxes.php" method="Post">
<tr><td> First</d><td width="20px"><input type="text" name="text1" value=""></td></tr>
<tr><td> Second</d><td><input type="text" name="text2" value="" onBlur="hello()"></td></tr>
<tr><td> Result</d><td><input type="text" name="text3" value="" disabled=""></td></tr>
</form>
</table>
<h4>Enter any digit in text1 and text2 and see result in text3</h4>
<h4>Is it possible to do above with php without submitting FORM?</h4>
</div>
</center>
</body>
</html>
No problem, It works fine.
I used java script to sum two numbers.
Is it possible to add two numbers with php without using any submit button?
If yes then please guide me.
http://i41.tinypic.com/2rfev7m.jpg
If you want to do it without a submit button and in php, you can use an AJAX request in javascript to the server, which will compute to value and then return it to the client.
here is the working sample.
First of all we need to create a html file named add.html.
here is the code of the add.html file…
<!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>Addition</title>
</head>
<body>
<form action="result.php" method="post">
Enter first Integer: <input type="text" name="first" size="5" /><br/>
Enter second Integer: <input type="text" name="sec" size="5" /><br/>
<input type="submit" name="submit" value="Add" />
</form>
</body>
</html>
place it in the pseudo server folder. If your pseudo server is WampServer, then the path of the file will be
C:/wamp/www/add.html
now open your favorite browser and to the address bar type
localhost/add.html
You can see the add.html page. add with number
You can see two text box where you can input the numbers and a submit button. You can input two numbers into the input boxes and press the submit button (The Add button). But nothing will happen. Because the php will do the adding job.
Lets create the php file. You can name it result.php. As I have already declared in the add.html form action is the result.php.
<form action="result.php" method="post">
if you give different name of the php file then please change the form action php name in add.html.
Here is the php code of result.php…
<?php //Starting of php
$first = $_POST['first']; //Getting Value of first integer from add.html
$sec = $_POST['sec']; //Getting Value of Second integer from add.html
$res = $first + $sec; //Adding the two values and placing the added result to 'res' variable
echo 'Added Result:';
echo $first." + ".$sec." = ".$res; //Showing the result to the screen
//Ending of php
?>
save this result.php file in the server path where you have already placed the add.html file.
Now it is the time to test. Open your favorite browser and in the address bar type…
localhost/add.html
enter two numbers and hit the Add button. you will see that the browser will direct you to the result.php page where you can see the added result.
hope this will help you.

Jquery, PHP Calling other form after click

Im just learning javascript the last days (started PHP some months ago).
So, my code make this:
e.g
http://controljuridico.com/video01/
I have three files.
An Html file with the form.
A javascript file with the functions after click on "enviar" (send) button.
A php that process the data.
HTML file (index.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<link href="css/css.css" type="text/css" rel="stylesheet" media="screen" />
<script src="js/jquery.js" type="text/javascript" language="JavaScript"></script>
<script src="js/js.js" type="text/javascript" language="JavaScript"></script>
</head>
<body>
<div id="content">
<h1>Test.</h1>
<h1>Step 1) choose city</h1>
<br />
<br />
<label>test(*)</label>
<hr />
<br />
<form name="form_data" id="form_data">
<label>(*) City</label>
<br />
Medellin
<input type="radio" name="ciudad" value="Medellin" />
Manizales
<input type="radio" name="ciudad" value="Manizales"/>
Cali
<input type="radio" name="ciudad" value="Cali"/>
<br />
<label>(*) Especialidad</label>
<br />
<input type="button" value="Enviar" id="btn_enviar" />
<br />
<br />
<label id="mensaje"></label>
</form>
<div id="resultado" ></div>
</div>
</body>
</html>
js.js File
$(document).ready(function(){
$('#btn_enviar').click(function(){
if( validaRadio( 'ciudad','Ciudad' ) == false) return false;
$.ajax({
type:'POST',
url :'upload.php',
data: $('#form_data').serialize(),
beforeSend : function(){
$('#mensaje').html('Enviando datos...');
},
success: function (data){
$('#mensaje').html('Datos enviados correctamente.');
$('#resultado').html(data);
},
complete: function(){
$('#form_data').slideUp();
$('#resultado').slideDown();
}
});
});
});
Php File (upload.php)
<?php
$sergu = $_POST['ciudad'];
if ($_POST['ciudad'] == "Medellin") {
?>
<label>Ciudad Ingresada:</label>
<br />
<label><?php echo $_POST['ciudad'] ?></label>
<hr />
<?php
}else{
echo "it is not medellin....";
}
?>
So, this works very well but. What if I want this:
After click on "enviar" button also show another similar form at the left of this form.
I mean its just like choosing steps if you choose the step one I need another step and so on and I want that this another step just appear to the left of the previus form.
It is possible? how?
Thanks in advance for your help! I really appreciate it.
You could for example just hide the 'second form' when the page first loads via css like this:
<form id="second_form" style="display: none">...
and when your success function fires you remove the css like so:
success: function(){
...
$('#second_form').show();
}
Short answer: yes.
In jquery, there is .insertBefore(). An easy way to implement what you (kind of) want, is to just echo out the new form in the php, and instead of
$('#resultado').html(data);
Do the following:
$(data).insertBefore('#resultado');
This will insert the new form, echoed out by the PHP, under the previous one. Ofcourse you also have to delete the
$('#form_data').slideUp();
Else, the forms will be hidden.

Print values from an SQL database table with PHP

I am new at PHP/SQL so bear with me if I say something that's obvious or downright wrong.
I have a SQL table (in a database) and I need to take 3 random values (name, race, year), each from a different field in the table, and print it on a website with php. The value requirement "race" will be different depending on radiobuttons (lets say RB1, and RB2) and this all has to happen when
<input type="submit" name="button"> is clicked.
<html> <input type="radio" name="RB1"> Asian </html>
What could I do in this situation?
Example: So if RB1 is selected and the button is clicked I will need to randomly print a "name" with the corresponding "year" (of the name) and it should have a corresponding race of RB1 (which is Asian)
I think this code will help you to understand this-
DB thing, you have to take care for that..im giving you an idea to handle this sort of stuff.
Create one test.php file(this what i did)
Place this 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>index</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("input:radio[name=race]").change(function(){
$.ajax({
type : 'GET',
url : 'getdata.php',
data: {race : $('input:radio[name=race]:checked').val()},
success: function(data){
$('#showdata').html(data);
},
});
});
});
</script>
</head>
<body>
<form action="#" method="post" name="myform">
<input type="radio" name="race" value="RB1" /> Asian
<input type="radio" name="race" value="RB2" /> European
</form>
<div id="showdata"></div>
</body>
</html>
In getdata.php file paste this content -
<?php
$myvalue = $_GET['race'];
if( $myvalue == 'RB1')
{
echo "I am RB1";
}
if( $myvalue == 'RB2')
{
echo "I am RB2";
}
Run the code in browser now (test.php).

JavaScript enable button not working in 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

Undefined index with session

I'm developing a chatbox script, and I have this page that checks if session is set, and if so, the certain elements of code should be hidden with jQuery. Here are my pages:
input.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=iso-8859-1" />
<style type="text/css">
#import "stil.css";
</style>
<title>Untitled Document</title>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.13.custom.min.js"></script>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="postme.js"></script>
<?php
include_once('check.php');
?>
</head>
<body>
<div id="wrap">
<div id="chat">
<div id="main">
</div>
<div id="input">
<form name="form"action="test.php" method="post">
<input type="text" name="tekst" id="msg" size="72" />
<input type="submit" name="dugme" value="posalji" id="dugme" />
</form>
</div>
</div>
</div>
<div id="black">
</div>
<div id="name">
<form name="yname">
<input type="text" name="tekst2" />
<input type="button" name="dugme2" value="Enter" onclick='send()' />
</form>
</div>
</body>
</html>
sesion.php:
<?php
session_start();
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
?>
check.php:
<?php
include('sesion.php');
if (!isset($sesion_n)){
echo "<script type='text/javascript'>$('#black').hide();$('#name').hide();</script>";
}
?>
postme.js:
function send(){
$.post('sesion.php',{ime:yname.tekst2.value},function(val){
if(val!=null) {
$('#black').fadeOut();
$('#name').hide();
alert(val);
}
}
)};
So the problem is that I get this error every time I run the page:
Notice: Undefined index: ime in C:\wamp\www\AJAX\sesion.php on line 3.
So can someone tell me what I'm doing wrong here?
if(isset($_POST['ime']))
{
$_SESSION['ime']=$_POST['ime'];
$sesion_n=$_SESSION['ime'];
echo $sesion_n;
}
it seems that $_POST['ime']; is undefined and that means that you are not posting it i guess.
Are you sure that yname.tekst2.value is the correct way to access the value of the field?
If you have firebug you can check in the "console" tab what parametrs have been posted.
It appears you're loading check.php manually. That'd be a GET request, and will trash your stored value, as _POST won't be set on those pages. Probably won't be the cause of the undefined index problem, but something to consider.
Check that the session's ID value stays constant between requests. If it's different each time, you're getting a brand new blank session on each request.

Categories