How to display alert message after submitting form - php

I want to display alert message of language selected. Disply alert message after submission of form.Below the code.I want to display alert message of language selected after.How can i do this?
<?php
global $mysqli;
$lang="Select `prompt_language` FROM `account_detail` WHERE `org_id`='36'";
$res= $mysqli->query($lang) or die($mysqli->error);
$row=$res->fetch_array(MYSQLI_ASSOC);
if($_POST['submit']=="Save")
{
$language=$_POST['pbx_lan'];
if($language!="")
{
$query="update `account_detail` set `prompt_language`= '$language' WHERE `org_id`='36'";
$result = $mysqli->query($query) or die($mysqli->error);
$_SESSION['check_update'] = "1";
setcookie("msg","Language Successfully Updated",time()+5,"/");
header("location:".SITE_URL."index.php?view=view_service");
}
else
{
setcookie("err","No Language Selected.Please Select Language",time()+5,"/");
header("location:".SITE_URL."index.php?view=view_service");
}
}
?>
<div class="dashboard">
<h2>Select Language<h2>
<form action="<?php echo $PHP_SELF ?>" method="post" enctype="multipart/form-data" action="prompts.php" >
<select id="pbx_lan" name="pbx_lan" style="margin-left:5px width=1px" class="input validdid required" >
<option value="">Select</option>
<option value="en" <?php echo $row['prompt_language'] == "en" ? "selected=selected" : ""; ?>>English</option>
<option value="uk" <?php echo $row['prompt_language'] == "uk" ? "selected=selected" : ""; ?>>English(UK)</option>
</select><br></br>
<input type="submit" name="submit" id="sub" value="Save" class="btn" style="margin-left:5px" />
</form>
</div>

Place try the below code
echo '<script>
alert("You have selected '.$language.'");
window.location = "'.$SITE_URL.'index.php?view=view_service."
</script>';
//header("location:".SITE_URL."index.php?view=view_service");

Related

issue with displaying proper output in PHP

I trying to make search option based on a SELECT box and user input (both are mandatory). But in the following code both correct and wrong input are displaying as wrong input. Can someone please explain what is wrong in the code.
Here is HTML
<form action="" method="POST">
<select name="selectOpt">
<option>Select a list</option>
<option value="one">ID</option>
<option value="delName">Dealer Name</option>
<option value="medName">Medical Name</option>
</select>
<input type="text" name="uinput" placeholder="Enter Search Key"/>
<button type="submit" name="submit">search</button>
</form>
PHP
if(isset($_POST['submit'])){
if(!empty($_POST['selectOpt']) && !empty($_POST['uinput'])){
if($_POST['selectOpt']=='one'){
$id = $_POST['selectOpt'];
if (!preg_match("/^[0-9]*$/",$id)){
echo "not valid";
}else{
echo "valid";
}
}
}else{
echo "Enter Value";
}
}
Your pattern /^[0-9]*$/, which is written to require a sequence of zero or more digits, doesn't match any of the possible values for your select box, which are all alphabetic strings.
<form action="save.php" method="POST">
<select name="selectOpt">
<option>Select a list</option>
<option value="one">ID</option>
<option value="delName">Dealer Name</option>
<option value="medName">Medical Name</option>
</select>
<input type="text" name="uinput" placeholder="Enter Search Key"/>
<button type="submit" name="submit">search</button>
</form>
<?php
if(isset($_POST['submit'])){
if(!empty($_POST['selectOpt']) && !empty($_POST['uinput'])){
if($_POST['selectOpt'] == "one"){
$id = $_POST['selectOpt'];
if (preg_match("/^[0-9]*$/",$id)){
echo "not valid";
}else{
echo "valid";
echo $input = $_POST['uinput'];
}
}
else{
echo "PLEASE Enter Proper and Valid Search ";
}
}
else{
echo "PLEASE Enter Value ";
}
}
?>

PHP Why does my select option show empty

When I submit the form I don't get the value of the select option I tried using POST and session but it always show nothing
main.php
<form role="form" method="POST" action="test.php">
<?php if($id == 1 OR $id==2){
echo" <p> No data</p> ";}else{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata )
or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data )) {
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
$_SESSION['data_id']= $data_id;
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
$dataID = isset($_POST['data_id']) ? $_POST['data_id'] : '';
echo "data is $dataID";
Name of your input type select is data and you are accessing it with data_id so you have to use $_POST['data'] instead of $_POST['data_id']
get value of select box using name "data" as you have set name="data" in <select> box in html:
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is".$dataID;
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
echo '<option value="'.$row['data_id'].'">'.$dataName.'</option>';
}
?>
</select>
<?php
}
?>
<button type="submit" class="btn btn-primary">show</button>
test.php
$dataID = isset($_POST['data']) ? $_POST['data'] : '';
echo "data is $dataID";
try this one..
Check below points its may be creating issue.
Check first your <select class="form-control" name="data"> name is data so you can access it using $_POST['data'] not data_id.
Check for <option value="'.$row['data_id'].'"> may be data_id not giving correct value try to check with static value.
I modified this code for you,please use this code.Its work for me,i hope this code will work also for you.
main.php
<form role="form" method="POST" action="test.php">
<?php
if($id == 1 OR $id==2)
{
echo" <p> No data</p> ";
}
else
{
?>
<select class="form-control" name="data">
<?php
$getdata = "SELECT * FROM tbl_data";
$data = mysqli_query($conn,$getdata ) or die(mysqli_error());
while ($row=mysqli_fetch_assoc( $data ))
{
$dataName = $row['data_name'];
?>
<option value="<?php echo $row['data_id']; ?>"><?php echo $dataName ?></option>
$_SESSION['data_id']= $row['data_id'];
<?php
}
}
?>
</select>
<button type="submit" class="btn btn-primary">show</button>
</form>
test.php
<?php
if(isset($_POST['data']))
{
echo $_POST['data'];
}
?>

Database driven select box being empty on submit

I am trying to submit a form value in a database with php. In form a select box value comes from database.
<?php include_once 'header.php';
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
//form validion
if(isset($_POST['submit']))
{
$eid =$_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
$miatm =trim($_POST["miatm"]);
if(empty($miatm) || !preg_match("/^[a-zA-Z0-9 ]*$/",$miatm)) {
$flag=1;
$miErr="Please Enter Valid Id";
}
.............like this
if($flag==0)
{
$sqll="insert into **********";
}
//my form is
<form id="basic" method="post" name="basic">
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
<p class="contact"><label for="bid">Micro-ATM Serial No</label></p>
<input type="text" name="miatm" value ="<?php if (isset($miatm)) echo $miatm; ?>" /> <?php echo $miErr; ?>
<p class="contact"><label for="bid">Micro-ATM TID No</label></p>
<input type="text" name="tid" value ="<?php if (isset($tid)) echo $tid; ?>" /> <?php echo $tiErr; ?>
<input class="buttom" name="submit" id="submit" value="Add Me" type="submit">
Its seems Ok.but when i tried to submit the form if some of one field remain empty then its show blank value in select box.
how can i remain the same selected value in select box even if textbox remain empty.
You need to retain the value of drop down after form submit.
User selected attribute of select option.
<?php
if (isset($_POST['submit'])) {
$eid =$_POST["eid"];
if ($eid=="blank") {
$flag=1;
$idErr="please Select E-MITRA";
}
}
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result)) {
$selected = (isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected="selected"' : '';
?>
<option value="<?php echo $row['uid']; ?>" <?php echo $selected;?>><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
You need to use selected="" or selected="selected" after submission in your select tag as a attribute as:
<?
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
$selected = ((isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected=""' : '');
?>
<option <?=$selected?> value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
if(isset($_POST['submit']))
{
$eid = $_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
?>
</select>
Side Note:
In your question ist two lines are not inside the php, i hope this is type error.

dropdown selected item not printing

I am trying to print the dropdown selected item. I have well displayed the dropdwon list menu. but when i select an option it doesn't print the option. i have tried in many ways. But not yet got! Please help me, this is my following code.
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<?php
echo "<select name=category></option>";
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
echo "</select>";
?>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
You have issues in your code, try this one instead :
<form name="choose" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<?php
$query="SELECT id_cat,name FROM `fs01_metier_cat` ORDER BY `fs01_metier_cat`.`id_cat`";
$result = mysql_query($query);
?>
<select name=category>
<?php
while($nt=mysql_fetch_array($result)) {
echo "<option value='".$nt['name']."'>".$nt['name']."</option>";
}
?>
</select>
<input type="submit" name="submit" value="save category" />
</form>
<?php
if($_GET){
echo 'The year selected is'.$_GET['category'];
}
?>
$_GET['category']
should be
$_POST['category']
Example for javascript:
<html>
<head>
<script type="text/javascript">
window.onload = function() {
var eSelect = document.getElementById('cat');
eSelect.onchange = function() {
document.getElementById("displaytext").innerHTML = "Selected Value: "+this.value;
document.getElementById("displaytext").style.display= 'block';
}
}
</script>
</head>
<body>
<select id="cat" name="cat">
<option value="x">X</option>
<option value="y">Y</option>
<option value="other">Other</option>
</select>
<div id="displaytext" style="display: none;" ></div>
</body>
</html>
​

PHP Form - Error Message Placements

I have created a form which has a number of fields which I am posting to a database.
At the bottom of the form I I have set some simply error checking to make sure all the required fields are complete.
When the user clicks the submit button my error checking take place and if there are errors it will output a string with the errors which it encountered.
These errors are currently displayed below the form but I want them to appear above the form fields. Is there anyway I can do this?
If I move the error checking code above the form field obviosly it does not work as it checks for errors before any of the field have been completed.
Any ideas how I can do this?
Here is the code
enter code here
<! Code to check that the user has logged into to view this page !>
<?php
session_start();
if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: login.php");
}
?>
<!Connection details for connecting to mysql database!>
<!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>Op Tech Database - Add Record</title>
</head>
<!Code to Create drop down menu's!>
<?php
//Code for collectiing values for Student Names drop down drop
$result1=mysql_query("SELECT studentID, studentName FROM students");
$options1="";
while ($row=mysql_fetch_array($result1)) {
$id=$row["studentID"];
$first=$row["studentName"];
$options1.="<OPTION VALUE=\"$first\">".$first.'</option>';
}
//Code for getting tutors names in drop down list
$result2=mysql_query("SELECT staffID, tutorName FROM staff");
$options2="";
while ($row=mysql_fetch_array($result2)) {
$id=$row["staffID"];
$first=$row["tutorName"];
$options2.="<OPTION VALUE=\"$first\">".$first.'</option>';
}
if(isset($_REQUEST['submited'])) {
$errorMessage = "This is the standard error message";
$options1 = $_POST['studentName'];
$options2 = $_POST['tutorName'];
$procedure = htmlspecialchars($_POST['procedure']);
$grade = $_POST['grade'];
$studentReflection = htmlspecialchars($_POST['studentReflection']);
$professionalism = $_POST['professionalism'];
$communication = $_POST['communication'];
$tutorComments = htmlspecialchars($_POST ['tutorComments']);
/*if(empty($_POST['alert']))
{
$_POST['alert'] = "NO";
}
*/
$alert = $_POST['alert'] ;
$studentNameError = "You did not enter the student name";
$error = false;
if(empty($_POST['studentName']))
{
$studentNameError = "You did not enter the student name";
echo "<h3> $studentNameError </h3>";
$error = true;
}
//Code to check that the Tutor Name field is completed
if(empty($_POST['tutorName'] ))
{
echo "<h3>You did not select a tutor name.</h3>";
$error = true;
}
//Code to check that the Procedure field is completed
if(empty($_POST['procedure'] ))
{
echo "<h3>You did not select a procedure.k</h3>";
$error = true;
}
//Code to check that the Grade field is completed
if(empty($_POST['grade'] ))
{
echo "<h3>You did not select a grade.</h3>";
$error = true;
}
//Code to check that the Student Reflection field is completed
if(empty($_POST['studentReflection'] ))
{
echo "<h3>The student did not enter any comments for this procedure. Student reflection is required for each procedure. </h3>";
$error = true;
}
//Code to check if the tick box is checked that the tutor comment is entered
if( !strlen($_POST['tutorComments']) && isset($_POST['alert'] ))
{
echo "<h3>You must enter a reason why you have clicked the alert box</h3>";
$error = true;
}
if($error)
{
exit();
}
?>
<body>
<link rel="stylesheet" type="text/css" href="ex1.css" >
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
<!Create HTML elements!>
<form name="myform" form method="post">
<h1 align="center"><img src="colour_logo_400.jpg" alt="University Logo" width="400" height="185" /></h1>
<h1 align="center">Dental Hygiene Operative Technique Database</h1>
<h2 align="center">Welcome to the Dental Hygiene Operative Technique Database v1</h2>
<p align="left"> </p>
<p align="left">Student Name(*)</p>
<p align="left">
<! Drop Down Menu to get student names from database !>
<SELECT NAME=studentName >
<OPTION VALUE=0 selected="selected" >
<?php if(isset($_POST['studentName'])) echo $_POST['studentName'];?>
<?php echo $options1?>
</SELECT>
<p align="left">Tutor Name
(*)<p align="left">
<! Drop Down Menu to get tutor names from database !>
<select name=tutorName>
<option value=0>
<?php if(isset($_POST['tutorName'])) echo $_POST['tutorName'];?>
<?php echo $options2 ?> </option>
</select>
<p align="left">
<p align="left"><br>
Procedure(*)
<input type="text" name="procedure" value="<?php if(isset($_POST['procedure'])) echo $_POST['procedure'];?>" />
<select name=grade id=grade>
<option value="">Grade </option>
<option value="N" <?php if (isset($_POST['grade']) && $_POST['grade'] == "N") { echo 'selected="selected"';} ?>>N</option>
<option value="B" <?php if (isset($_POST['grade']) && $_POST['grade'] == "B") { echo 'selected="selected"';} ?>>B</option>
<option value="C" <?php if (isset($_POST['grade']) && $_POST['grade'] == "C") { echo 'selected="selected"';} ?>>C</option>
</select>
(*)
<p align="left">
Student Reflection:
(*)<br>
<textarea name="studentReflection" cols="75" rows="5"><?php if(isset($_POST['studentReflection'])) echo $_POST[ 'studentReflection'];?></textarea>
<p align="left">
<SELECT NAME=professionalism>
<OPTION VALUE="">Professionalism
<OPTION VALUE="U" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "U") {
echo 'selected="selected"';} ?>>U</option>
<OPTION VALUE="S" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "S") {
echo 'selected="selected"';} ?>>S</option>
<OPTION VALUE="E" <?php if (isset($_POST['professionalism']) && $_POST['professionalism'] == "E") {
echo 'selected="selected"';} ?>>U</option>
</SELECT>
</SELECT>
<SELECT NAME=communication>
<OPTION VALUE="">Communication
<OPTION VALUE="U" <?php if (isset($_POST['communication']) && $_POST['communication'] == "U") {
echo 'selected="selected"';} ?>>U</option>
<OPTION VALUE="S" <?php if (isset($_POST['communication']) && $_POST['communication'] == "S") {
echo 'selected="selected"';} ?>>S</option>
<OPTION VALUE="E" <?php if (isset($_POST['communication']) && $_POST['communication'] == "E") {
echo 'selected="selected"';} ?>>U</option>
</SELECT>
Alert:
<input type="checkbox" value="YES" name="alert" >
<br>
<br>
Tutor Comments:
<br>
<br>
<textarea name="tutorComments" cols="75" rows="5">
<?php if(isset($_POST['tutorComments'])) echo $_POST['tutorComments'];?></textarea>
<p align="left">
<!Submit buttons for the form!>
<input type="submit" name="mattbutton" class="mattbutton" value="Update Database" name="submit"/>
<input type='button' name="mattbutton" class="mattbutton" value='Logout' onClick="window.location.href='logout.php'">
<input type="hidden" name="submited" value="true" />
<p align="left">
<?php
//Code to turn off error reporting
//error_reporting(0);
//Error Message to display if all the correct fields are not completed.
//Code to connect to the database
$query= "INSERT INTO entry (entryID, studentName , tutorName , procedureName , grade , studentReflection , tutorComments, professionalism , communication , alert ) VALUES ('NULL', '".$options1."' , '".$options2." ' , '".$procedure."' , '".$grade."' , '".$studentReflection."', '".$tutorComments."' , '".$professionalism."' , '".$communication."' , '".$alert."' )";
mysql_query($query) or die ('Error : You are attempting to enter information which cannot be stored or contains code. Please refesh the from and try again<br>' .mysql_error());
echo "<h3>The Database Has been updated. Thanks </h3></b>" ;
}
?>
</FORM>
<p> Enter another procedure
<p> </p>
<p> </p>
</body>
</html>
There shouldn't be any problem having your error checking code "above" the form rendering code. Once the form has been submitted, presumably via POST, you have all of the form variables in an array (the $_POST array) and they can be used regardless of whether or not you decide to re-render the form or not.
You can make a hidden field in your form, for example
<input type="hidden" name="isSubmitted" value ="1">
Then, in your checking routines, you first check if $_POST['isSubmitted'] (or $_GET['isSubmitted']) == 1 If it's true, then you know that user have been submitted your form, and you can make your additional checks

Categories