Print values from an SQL database table with PHP - 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).

Related

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.

Creating a button labeled with one PHP item to insert another

Very simply, I'm creating a PHP content database for a website. I want to create buttons next to a text field, with a title drawn from a table. Clicking this button then inserts an equivalent code into the field. These PHP table consists of three columns: key, word and code
So far I have the following code... this DOES populate the button value, but the does not insert the code. I suspect this is due to the use of '' within the PHP code. Am I correct?
Appreciate any guidance
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function insertText(elemID, text)
{
var elem = document.getElementById(elemID);
elem.innerHTML += text;
}
</script>
</head>
<body>
<form>
<textarea id="txt1"></textarea>
<input type="button" value="<?php echo $row_tooltips['word']; ?>" onclick="insertText('txt1', '<?php echo $row_tooltips['code']; ?>');">
</form>
</body>
</html>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function insertText(elemID, text)
{
var elem = document.getElementById(elemID);
elem.innerHTML += text;
}
</script>
</head>
<body>
<form>
<?php
$row_tooltips['word'] = "buttontitle";
$row_tooltips['code'] = "code";
?>
<textarea id="txt1"></textarea>
<input type="button" value="<?php echo $row_tooltips['word']; ?>" onclick="insertText('txt1', '<?php echo $row_tooltips['code']; ?>');">
</form>
</body>
</html>
Changed nothing of your code but predefining the $row_tooltips array and it works completely fine for me. Are you sure, that this array gets filled correctly?
greetz
So you say:
What I'm trying to achieve is a button to insert some HTML code though so when the user hits a button a full link is included into a PHP text field.
Then indeed, if there is an ' in the string that has been echoed, your script breaks. Also with any other htmlspecialchars, especially " your run into problems; So you need to escape that.
Also, htmlcode inside a textarea is standard not possible, so you should probably make it a div (with an textbox inside, if you need that).
Based on your original code, below a code that works ok. I use htmlspecialchars for the plain text, and addslashes for your html with (the ', as encoding them does not work).
<?php
// Remove this, only for testing
$row_tooltips['word'] = "Insert a FooBar";
$row_tooltips['code'] = "<h1>Delicious 'FooBar'</h1><img src=\"http://thumbs.dreamstime.com/x/reep-chocolade-12835946.jpg\" alt=\"foobar\" />";
?>
<html>
<head>
<title>Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function insertText(elemID, text)
{
var elem = document.getElementById(elemID);
elem.innerHTML += text;
}
</script>
</head>
<body>
<form>
<div id="txt1"></div>
<input type="button" value="<?php echo htmlspecialchars($row_tooltips['word'], ENT_QUOTES, 'ISO-8859-1'); ?>"
onclick="insertText('txt1', '<?php echo addslashes(htmlspecialchars($row_tooltips['code'], ENT_COMPAT, 'ISO-8859-1')); ?>');">
</form>
</body>
</html>
Tested in Chrome.

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.

dojo dojo.behavior values not posted

When I run following snippet and click 'submit', 'price' is not posted; is there something I forgot ?
<?php
var_dump($_POST);
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="libs/dijit/themes/claro/claro.css">
<script>dojoConfig = {async: true}</script>
<script src="//ajax.googleapis.com/ajax/libs/dojo/1.8.3/dojo/dojo.js"></script>
</head>
<body class="claro">
<form id="myform" method="post" action="index.php">
<input class="cif" name="price" type="text" value="125.10" />
<input type="submit" value="submit" name="submit">
</form>
<script type="text/javascript">
require(["dojo/ready", "dijit/form/NumberTextBox", "dojo/behavior"],
function(ready, box, behavior){
ready(function(){
behavior.add({
'.cif': function(node) { //assumes "found"
new box({constraints: {pattern: "###,###.00"}, value: dojo.number.format(node.value, {places:2})},node);
}
});
behavior.apply();
});
});
</script>
</body>
</html>
Sorry for this newbie question
Erik
Replace:
var_dump($_POST);
With:
var_dump($_POST['price']);
Recently I ran into similar problem and only this result I found but with no answer. But today I figure it out so I'll share my knowledge.
When you are creating a new widget box you also need to define 'name' attribute otherwise the widget will generate hidden input with no "name" attribute which is required when posting.
Change this line:
new box({constraints: {pattern: "###,###.00"}, value: dojo.number.format(node.value, {places:2}), name: "price"},node);
Trix

How to change dropdown selection into typeahead which using mysql database?

I want to change this code (dropdown select course) into typeahead so i just need to type a few word and then, it will suggest.
<TD width="112"><INPUT type="checkbox" name="chk[]"/></TD>
<TD width="472">
<SELECT name="course[]">
<?php
$sql = "select * from tt_course";
$result = mysql_query($sql);
while($rs = mysql_fetch_assoc($result))
{
$select="";
echo "<option value=\"".$rs['course_id']."\" ".$select.">".$rs['course_name']."</option>";
}
?>
</SELECT>
</TD>
Have a look at http://jqueryui.com/demos/autocomplete/
Its simple and easy to setup and will serve the purpose. You can create an array of values and provide it to Autocomplete or you can make it call your php page and parse the returned JSON data.
As promised, here is an example code to get you going. Please note that this code uses a local javascript variable to prepare suggestions for the autocomplete box. You can easily update that to look for JSON data from your php page and i could easily have made you even that but purpose is to help you learn so i think this way it would be better for you so that you can see how it works and then update it to your needs accordingly. Here you go, you can simply copy paste this code and see it running
<!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" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/black-tie/jquery-ui.css" type="text/css" />
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js'></script>
<script>
$(document).ready(function() {
$( "#AC" ).autocomplete({
source: programmingLang
});
});
var programmingLang = ["ActionScript","AppleScript","Asp","BASIC","C","C++",
"Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell",
"Java","JavaScript","Lisp","Perl","PHP","Python","Ruby","Scala","Scheme"];
</script>
</head>
<body>
<input type=text name="AC" id="AC">
</body>
</html>

Categories