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.
Related
I have a billing page which fetches data from the cart.
I am having a problem submitting these values to the database. How do i correct my php to get it to submit to the database correctly?
For example, i have a transactions table in my database, and i want all payment details to go there, which can later be fetched and outputted in the admin section as a receipt of order.
I cannot get the name, address, email, phone to submit correctly. It comes up with 0 in the database, how do i fix this?
How can i specifically use AJAX when the button is clicked to show a loading bar/page and then the success message?
I also get the error:
Notice: Undefined index: command in F:\xamppnew\htdocs\web\billing.php on line 5
Code:
<?php
include "storescripts/connect_to_mysql.php";
session_start();
if($_REQUEST['command']=='update'){
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$address=$_REQUEST['address'];
$phone=$_REQUEST['phone'];
$item_id = $_SESSION['item_id'];
$quantityorder = $each_item['quantity'] = $_SESSION['quantity1'];
$cartTotal = $_SESSION['cartTotal'];
// Add this product into the database now
$sql = mysqli_query($link,"insert into transactions values('','$item_id','$quantityorder','$cartTotal','$name','$address','$email','$phone')");
die('Thank You! your order has been placed!');
}
?>
<!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="style/style.css" type="text/css" media="screen" />
<title>Billing Info</title>
<script language="javascript">
function validate(){
var f=document.form1;
if(f.name.value==''){
alert('Your name is required');
f.name.focus();
return false;
}
f.command.value='update';
f.submit();
}
</script>
</head>
<body>
<div align="center" id="mainWrapper">
<?php include_once("template_header.php");?>
<div id="pageContent">
<div style="margin:24px; text-align:left;">
<br />
<form name="form1" onsubmit="return validate()">
<input type="hidden" name="command" />
<div align="center">
<h1 align="center">Billing Info</h1>
<table border="0" cellpadding="2px">
<tr><td>Product ID:</td><td><?php echo $item_id = $_SESSION['item_id'];?></td></tr>
<tr><td>Total Quantity:</td><td><?php echo $each_item['quantity'] = $_SESSION['quantity1']; ?></td></tr>
<tr><td>Order Total:</td><td>£<?php echo $cartTotal = $_SESSION['cartTotal'];?></td></tr>
<tr><td>Your Name:</td><td><input type="text" name="name" /></td></tr>
<tr><td>Address:</td><td><input type="text" name="address" /></td></tr>
<tr><td>Email:</td><td><input type="text" name="email" /></td></tr>
<tr><td>Phone:</td><td><input type="text" name="phone" /></td></tr>
<tr><td> </td><td><input type="submit" value="Place Order" /></td></tr>
</table>
</div>
</div>
<?php include_once("template_footer.php");?>
</form>
</body>
</html>
did you use Mysqli to connect ?
if yes, change mysql_query to mysqli_query
and you need to add 'name' attribute to the submit button
and do this
if(isset($_POST['submit'])){....
It is a good practice to use addslashes over your values before concat them in an sql query
$name = addslashes($_REQUEST['name']);
$email = addslashes($_REQUEST['email']);
$address = addslashes($_REQUEST['address']);
$phone = addslashes($_REQUEST['phone']);
how to input a number range, from 2 text fields, this is my code is seems to be not working, the range function i want it to be inserted to mysql database but this code is just to test if i can get the 2 textfields to connect to the range function. anyone can help me solve this problem?
<?php
$from = '.from';
$to = '.to';
$number = range('$from','$to');
print_r ($number);
?>
<!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>New Waybill</title>
<link rel="shortcut icon" href="../img/icon.ico">
<link rel="stylesheet" type="text/css" href="../css/forms.css" />
</head>
<body>
<form id="form3" name="form1" method="post" action="">
<p>From:
<input type="text" name="from" id="form_number" class="from" value="0"/>
- To:
<input type="text" name="to" id="form_number" class="to" value="50" />
</p>
<p>Waybill Booklet:
<select name="waybill_booklet" id="form_list">
</select>
</p>
<p>
<input type="submit" name="button" id="form_button" value="OK!" />
</p>
</form>
</body>
</html>
Replace your php code with this:
<?php
if (!empty($_POST)) {
$from = $_POST['from'];
$to = $_POST['to'];
$number = range($from,$to);
print_r($number);
}
?>
To access a form post in PHP you use the $_POST superglobal. More info found here: http://www.php.net/manual/en/reserved.variables.post.php.
I've put a check to see if it's empty or not at the start, so when the page first loads it won't attempt to run the PHP code. Presumably you'll then replace the print_r with whatever you need to insert the data into your database.
I read all questions related to $_GET and $_POST. I didn't find a solution for my case. The code is very simple, it should be working, but I get empty field when using $_GET.
Here is my code :
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Colors</title>
</head>
<body>
<h2> Project</h2>
<form method="get" action="display.php">
R:<input type="text" name="r" size="3"/> <p/>
G:<input type="text" name="g" size="3"/><p/>
B:<input type="text" name="b" size="3"/><p/>
<input type="submit" value ="Show me">
</form>
</body>
</html>
and php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Colors</title>
</head>
<body>
<h2>Color Sampler </h2>
<?php
$r=$_GET['r'];
$g=$_GET['g'];
$b=$_GET['b'];
$rgb=$r . ',' . $g . ',' . $b;
?>
R:<?php echo $r; ?>
G:<?php echo $g; ?>
B:<?php echo $b; ?>
<p/>
<div style ="width:150px;height:150px;
background-color:rgb(<?php echo $rgb;?>"/>
</body>
</html>
and this is what I get:
Project 2 Color Sampler
R: G: B:
so it is empty, like it doesn't see the values he needs to return.
What is wrong?
You can delete 'method="get"' from cause it uses be default.
Also, try to get params from $_REQUEST. Good luck!
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
I've been learning some PHP and MySQL from a book that teaches you how create a simple database driven site. In the book's examples, we're creating a joke database that store author names, joke text, date and id. Progressing I've been taught how to use includes in my main controller, index.php. I'm stuck at a part where they tell me to create a search feature for the joke database, coding as follows:
This is the first part of the controller called 'index.php' all it does is display the search form.
// Display search form
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
include 'searchform.html.php'; //CHANGE 1
?>
The next part of the controller builds the SQL and then sends it to jokes.html.php, fairly simple... no problems here.
if (isset($_GET['action']) and $_GET['action'] == 'search')
{
include $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
//Build SQL statement and output results into an array code here
}
include 'jokes.html.php'; //CHANGE 2
exit();
}
How would you modify the code above if the your searchform.html and jokes.html are just the single html file? I find it inconvenient using 2 files for searching.
My first attempt (I've merged searchform and jokes into "jokesearch.html.php") was to include 'jokesearch.html.php' in CHANGE 1 and again in CHANGE 2, however that didn't help... it just reloaded the page.
2nd attempt was to use header('Location: .')... no luck here too it just reloaded.
EDIT: By popular demand, I'll include the two html files.
searchform.html.php:
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/helpers.inc.php'; ?>
<!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" xml:lang="en" lang="en">
<head>
<title>Manage Jokes</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Manage Jokes</h1>
<p>Add new joke</p>
<form action="" method="get">
<p>View jokes satisfying the following criteria:</p>
<div>
<label for="author">By author:</label>
<select name="author" id="author">
<option value="">Any author</option>
<?php foreach ($authors as $author): ?>
<option value="<?php htmlout($author['id']); ?>"><?php
htmlout($author['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="category">By category:</label>
<select name="category" id="category">
<option value="">Any category</option>
<?php foreach ($categories as $category): ?>
<option value="<?php htmlout($category['id']); ?>"><?php
htmlout($category['name']); ?></option>
<?php endforeach; ?>
</select>
</div>
<div>
<label for="text">Containing text:</label>
<input type="text" name="text" id="text"/>
</div>
<div>
<input type="hidden" name="action" value="search"/>
<input type="submit" value="Search"/>
</div>
</form>
<p>Return to JMS home</p>
</body>
</html>
jokes.html.php
<?php include_once $_SERVER['DOCUMENT_ROOT'] .
'/includes/helpers.inc.php'; ?>
<!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" xml:lang="en" lang="en">
<head>
<title>Manage Jokes: Search Results</title>
<meta http-equiv="content-type"
content="text/html; charset=utf-8"/>
</head>
<body>
<h1>Search Results</h1>
<?php if (isset($jokes)): ?>
<table>
<tr><th>Joke Text</th><th>Options</th></tr>
<?php foreach ($jokes as $joke): ?>
<tr valign="top">
<td><?php htmlout($joke['text']); ?></td>
<td>
<form action="?" method="post">
<div>
<input type="hidden" name="id" value="<?php
htmlout($joke['id']); ?>"/>
<input type="submit" name="action" value="Edit"/>
<input type="submit" name="action" value="Delete"/>
</div>
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
<p>New search</p>
<p>Return to JMS home</p>
</body>
</html>
From a MVC perspective your initial setup is the right way to go.
Your controller collects and processes data and sends it to views (your .html.php files).
It is good practice to separate defferent elements into different views. So a search box or search results go in a different view than the jokes.
Putting both logical different elements in one view file makes maintenance harder.
Regards,
Erwin Vrolijk
snow.nl
maybe something like this
if (isset($_GET['action']) and $_GET['action'] == 'search') {
// search result
} else {
// show search form
}