After pressing submit, I want it to display a 2nd form - javascript

I'm trying to submit a form, and after pressing the submit button I want the 2nd form to show up while still holding values (I have this part worked out). I was told to use the isset function but i'm unable to get it working. The code works fine (I haven't copied the php stuff in).
<!DOCTYPE html>
<html>
<head>
<title>Prac 2 Task 12</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo" >
<table>
<tr>
<td><label for="customerid">Customer ID (integer value): </label></td>
<td>
<input
type="text"
id="customerid"
name="customerid"
size="10"
value="<?php
echo isset($_POST['customerid'])
? htmlspecialchars($_POST['customerid'])
: ''; ?>"
/>
<?php echo $customerIDError ?>
</td>
<td style="color:red"></td>
</tr>
</table>
<p>
<input type="submit" name = "submit" value="Save Data"/>
<input type="reset" value="Reset Form" />
</p>
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" id="custinfo" >
<table>
<tr>
<td><label for="username">Username: </label></td>
<td>
<input
type="text"
id="username"
name="username"
size="10"
value="<?php
echo isset($_POST['username'])
? htmlspecialchars($_POST['customerid'])
: ''; ?>"
/>
<?php echo $customerIDError ?></td>
<td style="color:red"></td>
</tr>
</table>
<p>
<input type="submit" name = "submit" value="Save Data"/>
<input type="reset" value="Reset Form" />
</p>
</form>
</body>
</html>

you can use jquery for that first you can define
$("#submit").click(function(){
$("#custinfo1").show();
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<title>Prac 2 Task 12</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER[PHP_SELF]; ?> " id="custinfo">
<table>
<tr>
<td>
<label for="customerid">Customer ID (integer value):</label>
</td>
<td>
<input type="text" id="customerid" name="customerid" size="10" value="" />
<?php echo $customerIDError ?>
</td>
<td style="color:red"></td>
</table>
<p>
<input type="submit" name="submit" value="Save Data" id="submit" />
<input type="reset" value="Reset Form" />
</p>
</tr>
</form>
<form method="post" action="<?php echo $_SERVER[PHP_SELF];?>" id="custinfo1" style="display:none">
<table>
<tr>
<td>
<label for="username">Username:</label>
</td>
<td>
<input type="text" id="username" name="username" size="10" value="" />
<?php echo $customerIDError ?>
</td>
<td style="color:red"></td>
</table>
<p>
<input type="submit" name="submit" value="Save Data" />
<input type="reset" value="Reset Form" />
</p>
</tr>
</form>
</body>
</html>
check this code

I rectified your code, just run it
<!DOCTYPE html>
<html>
<head>
<title>Prac 2 Task 12</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<style>
#form2{display: none;}
</style>
</head>
<body>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" id="form1" >
<table>
<tr>
<td><label for="customerid">Customer ID (integer value): </label></td>
<td><input type="text" id="customerid" name="customerid" size="10" value = "<?php echo isset($_POST['customerid']) ? htmlspecialchars($_POST['customerid']):''; ?>" /><?php echo $customerIDError ?></td>
<td style="color:red"></td>
</table>
<p><input type="submit" id="form1submitbtn" name = "submit" value="Save Data"/> <input type="reset" value="Reset Form" /></p>
</tr>
</form>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>" id="form2" >
<table>
<tr>
<td><label for="username">Username: </label></td>
<td><input type="text" id="username" name="username" size="10" value = "<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['customerid']):''; ?>" /><?php echo $customerIDError ?></td>
<td style="color:red"></td>
</table>
<p><input type="submit" name = "submit" value="Save Data"/> <input type="reset" value="Reset Form" /></p>
</tr>
</form>
<script>
$(document).ready(function(){
$("form1submitbtn").click(function(){
$("#form2").show();
});
});
</script>
</body>
</html>

Related

Copy contents of textarea to input

In this table each row has a text area wd. I want to copy the contents of #wd to #wdi.
That is the user types in wd and the wdi value becomes wd value.
I am assuming I need to use onkeyup function but not sure.
<tbody>
while(rs1.next())
{ %>
<tr>
<td>
<textarea id="wd" rows="4" cols="50" name="wd" ></textarea>
</td>
<td>
<form method='POST' action="actonissue.jsp" id='form1'>
<input id='issue_id' name ='issue_id' value='<%=rs1.getString(1)%>' class='disable' type="hidden">
<input id='wdi' name ='wdi' class='disable' type="hidden" >
<button type="submit" >Resolve Issue</button> </form>
</td>
</tr>
<% } %>
</tbody>
You can use keyup function as, note that you need get #wdi within each tr.
$('[name ="wd"]').keyup(function(){
//console.log($(this).val())
$(this).closest("tr").find("#wdi").val($(this).val());
})
$('[name ="wd"]').keyup(function(){
//console.log($(this).val())
$(this).closest("tr").find("#wdi").val($(this).val());
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody>
<tr>
<td>
<textarea id="wd" rows="4" cols="50" name="wd" ></textarea>
</td>
<td>
<form method='POST' action="actonissue.jsp" id='form1'>
<input id='issue_id' name ='issue_id' value='test1' class='disable' type="hidden">
<input id='wdi' name ='wdi' class='disable' type="hidden" >
<button type="submit" >Resolve Issue</button> </form>
</td>
</tr>
<tr>
<td>
<textarea id="wd" rows="4" cols="50" name="wd" ></textarea>
</td>
<td>
<form method='POST' action="actonissue.jsp" id='form1'>
<input id='issue_id' name ='issue_id' value='test2' class='disable' type="hidden">
<input id='wdi' name ='wdi' class='disable' type="hidden" >
<button type="submit" >Resolve Issue</button> </form>
</td>
</tr>
</tbody>
</table>

Problem with javascript with alert are not showing

Hi so i have this sign up to register new users to database but the alert of JavaScript for register users is not working but the for example on the same page to login the user is working... i mean if u fail a password it will show the password is wrong, but if u try to register the user enter in data base but there isn't any alert saying "New record created successfully"
-----------------------------------register to db and alert----------------------
<?php
session_start();
$db=mysqli_connect("localhost","root", "","compras");
if(isset($_POST['signup'])) {
$fname = ($_POST['fname']);
$lname = ($_POST['lname']);
$password = ($_POST['password']);
$email = ($_POST['email']);
$phone = ($_POST['phone']);
$sql;
$password=md5($password);
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "Insert into users(fname,lname,phone,email,password) values ('$fname','$lname','$phone','$email','$password')";
if (mysqli_query($db, $sql))
{
echo '<script type="text/javascript">alert("New record created successfully");</script>';
header("Location:http://localhost/Fly With US/index.php");
}
else
{
echo '<script type="text/javascript">alert("Error Occured");</script>';
header("Location:http://localhost/Fly With US/index.php");
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
?>
---------------------------------main page--------------------------------------
<?php
session_start();
if(isset($_SESSION['username'])){
header("Location: cat_type.php");
}
?>
<html>
<head>
<link rel="stylesheet" href="css/head.css">
<link rel="icon" href="res/flywithus-pt.png" sizes="16x16">
<link href="https://fonts.googleapis.com/css?family=Bgrree+Serif" rel="stylesheet">
<script type="text/javascript" src="scripts/signupForm.js"></script>
<script type="text/javascript" src="scripts/loginForm.js"></script>
<title>Fly With Us | Buy Drones and Computers.</title>
</head>
<body>
<h1><center><img src="res/flywithus-png.png"></center></h1>
<!-- LOGIN -->
<div id="login">
<form name="loginForm" method="post" action="log.php">
<table align="left" width="150%">
<tr>
<td align="center">
<h2>Login</h2>
</td>
</tr>
<tr>
<td>
<input type="text" name="username" id="user" placeholder="Email" required>
</td>
</tr>
<br>
<tr>
<td>
<input type="password" name="password" id="pass" placeholder="Password" required>
</td>
</tr>
<tr>
<td>
<input type="submit" name="login" value="Let me in.." class="click">
</td>
</tr>
<!--
<tr>
<td colspan="2" class="signin">
<input type="image" src="signin_google.png" alt="Login with Google" width="50%" height="50%">
</td>
</tr>
<tr>
<td colspan="2" class="signin">
<input type="image" src="signin_fb.png" alt="Login with FB" width="50%" height="50%">
</td>
</tr>
//-->
</table>
</form>
</div>
<!-- SIGNUP -->
<form name="signupForm" method="post" action="pro.php" id="signup" onsubmit="return ValidateFname() || ValidateLname() || ValidateEmail() || ValidateMobile() ">
<table align="right">
<tr>
<td align="center" colspan="2">
<h2>New Here? Register with us..</h2>
</td>
</tr>
<tr>
<td>
<input type="text" name="fname" placeholder="First Name" required>
</td>
<td>
<input type="text" name="lname" placeholder="Last Name" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="email" name="email" placeholder="Email" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="text" name="phone" placeholder="Contact Number" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="password" name="password" placeholder="Password" required>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" name="signup" value="Sign Up" class="click">
</td>
</tr>
</table>
</form>
</body>
</html>
That's because PHP (header) will still read after JavaScript (alert). In other words, the alert is actually shown but the index.php is being load at the same time so the alert appears not showing.
won't work:
echo '<script type="text/javascript">alert("Error Occured");</script>';
header("Location:http://localhost/Fly With US/index.php");
will work:
echo '<script type="text/javascript">
alert("Error Occured");
window.open("index.php","_self");
</script>';

insert data in to mysql database

i have form when i click submit button it show all the data row by row
that all are work fine but my problem is how can i add this all value to mysql database when
click "submit_to_database" button. please can any one help me
this is my full code
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="custom.css" type="text/css">
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add Sales</title>
<script type="text/javascript">
function add_values(){
if(document.getElementById('edit_guid').value==""){
if( document.getElementById('stk').value!="" ){
if(document.getElementById('stk').value!=0){
sell=document.getElementById('pric').value;
disc=document.getElementById('stk').value;
item=document.getElementById('guid').value;
roll=parseInt(document.getElementById('roll_no').value);
$('<tr id='+item+'><td><lable id='+item+'roll >'+roll+'</label></td><td><input type=text readonly="readonly" value='+sell+'></td><td><input type=text readonly="readonly" value='+disc+' ></td></tr>').fadeIn("slow").appendTo('#item_copy_final');
document.getElementById('stk').value="";
document.getElementById('pric').value="";
document.getElementById('roll_no').value=roll+1;
document.getElementById('guid').value="";
}
}else{
alert('Please Select An Item');
}}}
</script>
<body>
<form name="form1" method="post" id="form1" action="">
<input type="hidden" id="roll_no" value="1" >
<div align="center">
<input type="hidden" id="guid">
<input type="hidden" id="edit_guid">
<table class="form" >
<tr>
<td>price</td>
<td>stk</td>
</tr>
<tr>
<td><input type='text' class='form-control' id="pric" name="pric"></td>
<td><input type='text' class='form-control' id="stk" name="stk"></td>
<td><input type="button" onclick="add_values()" id="add_new_code" value="submit" class="round"></div></form></td></tr>
</table>
<div style="overflow:auto ;max-height:300px; ">
<table class="form" id="item_copy_final" style="margin-left:45px "></table>
</div>
</div>
<div class="mytable_row ">
<form>
<div align="center">
<table>
<td><input type="button" value="submit_to_database" class="round"></td>
</table>
</div>
</form>
</body>
</html>
Update your code -
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<link rel="stylesheet" href="custom.css" type="text/css">
<html lang="en">
<head>
<meta charset="utf-8">
<title>Add Sales</title>
<script type="text/javascript">
function add_values(){
if(document.getElementById('edit_guid').value==""){
if( document.getElementById('stk').value!="" ){
if(document.getElementById('stk').value!=0){
sell=document.getElementById('pric').value;
disc=document.getElementById('stk').value;
item=document.getElementById('guid').value;
roll=parseInt(document.getElementById('roll_no').value);
$('<tr id='+item+'><td><lable id='+item+'roll >'+roll+'</label></td><td><input type=text name="price[]" readonly="readonly" value='+sell+'></td><td><input type=text name="stk[]" readonly="readonly" value='+disc+' ></td></tr>').fadeIn("slow").appendTo('#item_copy_final');
document.getElementById('stk').value="";
document.getElementById('pric').value="";
document.getElementById('roll_no').value=roll+1;
document.getElementById('guid').value="";
$("#pric").focus();
}
}
else{
alert('Please Select An Item');
}
}
}
</script>
</head>
<body>
<form name="form1" method="post" id="form1" action="">
<input type="hidden" id="roll_no" value="1" >
<div align="center">
<input type="hidden" id="guid">
<input type="hidden" id="edit_guid">
<table class="form" >
<tr><td>price</td><td>stk</td><td> </td></tr>
<tr>
<td><input type='text' class='form-control' id="pric" name="pric"></td>
<td><input type='text' class='form-control' id="stk" name="stk"></td>
<td><input type="button" onclick="add_values()" id="add_new_code" value="submit" class="round"></td>
</tr>
</table>
</div>
</form>
<div style="overflow:auto ;max-height:300px;" align="center">
<form method="post" action="test.php">
<table class="form" id="item_copy_final" style="margin-left:45px "></table>
<table>
<tr><td><input type="submit" name="submit_to_database" value="submit_to_database" class="round"></td></tr>
</table>
</form>
</div>
</body>
</html>
Write a php script page, where you post your data and add the value in mysql database.
"test.php"
<?php
mysql_connect("hostname", "database_user", "database_password") or die("Could not connect: " . mysql_error());
mysql_select_db("database_name");
if(isset($_POST["submit_to_database"])){
for($i=0; $i<count($_POST["price"]); $i++){
mysql_query("INSERT INTO `your_table` SET `your_price_field` = '".$_POST["price"][$i]."', `your_stk_field` = '".$_POST["stk"][$i]."'");
}
}
?>

how to insert multiple rows from array values with input

I have my table named i_table with columns:
id name req_qty req_date rcv_qty rec_date
1 metal 1 2014-03-04
2 spring 5 2014-03-04
in my html/php:
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
how can I insert in multiple arrays from an input according to their unique ID from db? pls help me here... huhu
In your form, add the id as the key -
<form action="insert.php" method="post">
<?php $resource2=mysql_query("SELECT * FROM i_table",$con);
while($result2=mysql_fetch_array($resource2))
{
?>
<table>
<tr>
<td>
<input type="text" name="id[<?php echo $result2['id'];?>]" value="<?php echo $result2['id'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="name[<?php echo $result2['id'];?>]" value="<?php echo $result2['name'];?>"/>
</td>
</tr>
<tr>
<td>
<input type="text" name="rcv[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
<tr>
<td>
<input type="date" name="rcv_date[<?php echo $result2['id'];?>]" value="" />
</td>
</tr>
</table>
<?php };?>
</form>
Then on your insert.php where you post your form, get the id in your foreach -
<?php
if(isset($_POST['id'])){
foreach($_POST['id'] as $id=>$value){
$sql = "UPDATE `i_table` SET `name` = '".$_POST['name'][$id]."', `rcv_qty` = '".$_POST['rcv'][$id]."', `rec_date` = '".$_POST['name'][$id]."' WHERE `id` = ".$id."";
mysql_query($sql,$con);
}
}
?>
note the foreach() is just an example. You will want to sanitize your data before inserting/updating to prevent sql injection. see How can I prevent SQL injection in PHP?

how to move previous page and show values in fields?

i make two web page,i want to get data from first page on second page,if user click edit message then move first page and show the entered data of user,here is my code:
first.php
<h1>Compose Message</h1>
<script type="text/javascript" src="<?=MURL?>/js/ckeditor/ckeditor.js"></script>
<script src="//ticket_inspector_new.com/js/tooltip.js" type="text/javascript"> </script>
<form action="" method="post" id="form" enctype="multipart/form-data">
<table class="form">
<tr class="heading">
<th style="width: 25%;">Recipients</th>
<th style="width: 75%;"> </th>
</tr>
<tr>
<td>
<label for="campaigns">Campaigns</label>
</td>
<td>
<select name="events[]" multiple size="10" >
<?
$select = sprintf ("SELECT event_id,event_name
FROM `events`
WHERE (`user_id` = '%s') order by event_name",
$GLOBALS ['mysqli']->real_escape_string ($_SESSION['user_id']));
$res = $GLOBALS ['mysqli']->query ($select) or die ($GLOBALS ['mysqli']->error . __LINE__);
if ($res->num_rows > 0)
{
while($row = $res->fetch_assoc ())
{
?>
<option value="<?=$row['event_id']?>"><?=$row['event_name']?></option>
<?
}
}
?>
</select>
</td>
</tr>
<tr>
<td style="padding-left:95px;">
<label for="fromdate">Registrants From</label>
</td>
<td style="padding-left:10px;">
<input name="fromdate" type="text" value="" class="calendar time" id="fromdate" size="30" />
<label for="todate">To</label>
<input name="todate" type="text" value="" class="calendar time" id="todate" size="30" />
</td>
</tr>
<tr>
<td>
<label for="upload">Upload CSV</label>
<span class="helptip">
Select CSV file for upload.
</span>
</td>
<td>
<input name="uploadcsv" type="file" />
</td>
</tr>
<tr class="heading">
<th style="width: 25%;">Message</th>
<th style="width: 75%;"> </th>
</tr>
<tr>
<td>
<label for="description">Description(optional)</label>
</td>
<td>
<input name="description" type="text" value="" id="description" size="35" />
</td>
</tr>
<tr>
<td>
<label for="subject">Subject</label>
</td>
<td>
<input name="subject" type="text" value="" id="subject" size="35" />
</td>
</tr>
<tr>
<td>
<label for="fromname">From Name</label>
</td>
<td>
<input name="fromname" type="text" value="" id="fromname" size="27" />
</td>
</tr>
<tr>
<td>
<label for="replyto">Reply To</label>
</td>
<td>
<input name="replyto" type="text" value="" id="replyto" size="27" />
</td>
</tr>
<tr>
<td>
<label for="senddatetime">Send Date/Time</label>
<span class="helptip">
Click the calender to select the date you wish ans select time zone from select box.
</span>
</td>
<td>
<input name="senddatetime" type="text" value="" class="calendar time" id="senddatetime" size="30" />
<select name="timezone" id="timezone">
<option value="Pacific/Honolulu">Hawaii-Aleutian Time (Honolulu, no DST)
</option><option value="America/Anchorage">Alaska Time (Anchorage)</option><option value="America/Los_Angeles"
selected="selected">Pacific Time (Los Angeles)</option><option value="America/Denver">Mountain Time (Denver)</option><option
value="America/Phoenix">Mountain Time (Phoenix, no DST)</option><option value="America/Chicago">Central Time (Chicago)
</option><option value="America/Regina">Central Time (Regina, no DST)</option><option value="America/New_York">Eastern Time
(New York)</option><option value="America/Halifax">Atlantic Time (Halifax)</option>
</select>
<script>
var list = document.getElementById('timezone');
var selval = "0";
for(var i = 0; i < list.options.length; ++i)
{
if(list.options[i].value==selval)
{
list.options[i].selected = true;
i=list.options.length;
}
}
</script>
</td>
</tr>
<tr>
<td>
<label for="message">Message</label>
</td>
<td colspan="2">
<textarea name="message" class="ckeditor" id="message" cols="90" rows="15" style="width: 100%;"></textarea>
</td>
</tr>
</table>
<table class="form">
<tr class="heading">
<th style="width:100%; background-color:#C4C4FE; font-size:10px; font-weight:normal;">Emails can take upto 30 minutes to Send.We have zero tolerance for spam messages.Every message sent out is reviewed for spam.Any spam messages sent will result in termination of account.</th>
</tr>
</table>
<p class="center_align">
<input type="submit" class="button arrow" name="submit_skip" value="Continue" />
</p>
</form>
and here is my second page:
<h1>Confirm Message</h1>
<?
$recepients=0;
$count=count($_POST['events']);
?>
<input type="button" class="button edit" name="submit_skip" value="Edit Message" />
<input type="submit" class="button email" name="submit_email" value="Send Message" />
i want when user click on edit massage it moves previous page and values shown in fields
?>
Since you want to send form to a two different scripts I suggest that you use 2 forms:
<form action="formfilling.php">
<?php
//prepare the recived POST to send back:
foreach( $_POST as $key => $value ){
if( is_array($_POST[$key]) ){ //if post is array e.g. your events[]
foreach( $_POST[$key] as $subvalue ){
echo '<input type="hidden"'
.' name="'.htmlspecialchars($key).'[]"'
.' value="'.htmlspecialchars($subvalue).'">'."\n";
}
} else{
echo '<input type="hidden"'
.' name="'.htmlspecialchars($key).'"'
.' value="'.htmlspecialchars($value).'">'."\n";
}
}
?>
<input type="submit" class="button edit" name="submit_skip" value="Edit Message" />
</form>
<form action="submitemail.php">
<?php //possibly show message? ?>
<input type="submit" class="button email" name="submit_email" value="Send Message" />
</form>
And then in formfilling check if input is not empty if not => echo its value
if( !empty($_POST['inputName']) ) echo htmlspecialchars($_POST['inputName']);
And for the array something like
if( !empty($_POST['inputName']) ){
foreach( $_POST['inputName'] as $val ){
//Test if the current option has the value of $val if so:
echo /*OPTION with SELECTED*/;
}
}
ALWAYS USE HTML ESCAPING when printing/echoing $_POST/$_GET
=> dont trust the users!
=> in PHP there is a htmlspecialchars() function
go to the previous page with javascript
window.history.go(-1)

Categories