how to move previous page and show values in fields? - javascript

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)

Related

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>';

Can't get TextArea value in the Request, always empty

The Result of echo is always:
"Course Description Value is: ...Thanks"
with an empty value for Course_Desc
<?
if (isset($_REQUEST["saveEdites"])) {
$id = $_REQUEST['ProtocolID'];
$Course_Desc = $_REQUEST['Course_Descr'];
$Course_Desc = trim($Course_Desc);
$Course_Desc = stripslashes($Course_Desc);
$Course_Desc = htmlspecialchars($Course_Desc);
echo "Course Description Value is : ".$Course_Desc." ...Thanks";
}
?>
<form method="post" name="implantForm">
<table >
<input type="hidden" name="ProtocolID" id="Protocol">
<tr align="Left">
<td>
<label style="color:#ff6600;font-weight:bold">
Name
</label>
</td>
<td>
<label id="formLbl"></label>
</td>
</tr>
<tr align="Left" style="color:#ff6600;font-weight:bold">
<td>Protocol </td>
<td>
<textarea id="formTXT" rows="4" cols="50" name="Course_Descr" form="implantForm"></textarea>
</td>
</tr>
</table>
<table>
<tr align="center" >
<td>
<input type="submit" name="saveEdites" value="Save changes">
</td>
</tr>
</table>
</form>
Update
I was declaring the form="implantForm"
so I removed it and everything is working now
Please put <form> tag in your program and remove form=implantForm from <textarea>.Here I made some changes in your code then I get textarea value as output
<form>
<table >
<input type="hidden" name="ProtocolID" id="Protocol">
<tr align="Left">
<td>
<label style="color:#ff6600;font-weight:bold">
Name
</label>
</td>
<td>
<label id="formLbl"></label>
</td>
</tr>
<tr align="Left" style="color:#ff6600;font-weight:bold">
<td>Protocol </td>
<td>
<textarea id="formTXT" rows="4" cols="50" name="Course_Descr" ></textarea>
</td>
</tr>
</table>
<table>
<tr align="center" >
<td>
<input type="submit" name="saveEdites" value="Save changes">
</td>
</tr>
</table>
</form>
<?php
if (isset($_REQUEST["saveEdites"])) {
$id = $_REQUEST['ProtocolID'];
$Course_Desc = $_REQUEST['Course_Descr'];
$Course_Desc = trim($Course_Desc);
$Course_Desc = stripslashes($Course_Desc);
$Course_Desc = htmlspecialchars($Course_Desc);
echo "Course Description Value is : ".$Course_Desc." ...Thanks";
}
?>
form="implantForm"
that what was confusing the browser
textarea value was not even sent in the request .removed it and everything is working now
Please remove form="implantForm" from textarea, then it will work.

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?

WP - Add New User Form

I'm creating a plugin where I'm wanting the owner of the company to be able to log in and be able to add employees/contractors and upon creating them, have them added to the wpdb. Right now, I've copied over the the html that was in the source code for the Add New User (I don't want to use the add new user format) and I've got the form there (minus what I'm guessing is the ajax and javascript bits). When I click submit it just returns to the page.
Here is the code that is being called in... I'm wondering if I'm missing some includes or something along those lines, thanks in advance! :)
<?php
function e34s_tc_add_staff()
{
global $wbdb;
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2 id="add-new-user"> Add New Staff Member</h2>
<div id="ajax-response"></div>
<p>Create a new staff member for this site.</p>
<form action="" method="post" name="createuser" id="createuser" class="validate">
<input name="action" type="hidden" value="createuser" />
<input type="hidden" id="_wpnonce_create-user" name="_wpnonce_create-user" value="5ebe2973f8" /><input type="hidden" name="_wp_http_referer" value="/wordpress/wp-admin/user-new.php" /><table class="form-table">
<tr class="form-field form-required">
<th scope="row"><label for="user_login">Username <span class="description">(required)</span></label></th>
<td><input name="user_login" type="text" id="user_login" value="" aria-required="true" /></td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="email">E-mail <span class="description">(required)</span></label></th>
<td><input name="email" type="text" id="email" value="" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="first_name">First Name </label></th>
<td><input name="first_name" type="text" id="first_name" value="" /></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="last_name">Last Name </label></th>
<td><input name="last_name" type="text" id="last_name" value="" /></td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="pass1">Password <span class="description">(required)</span></label></th>
<td>
<input class="hidden" value=" " /><!-- #24364 workaround -->
<input name="pass1" type="password" id="pass1" autocomplete="off" />
</td>
</tr>
<tr class="form-field form-required">
<th scope="row"><label for="pass2">Repeat Password <span class="description">(required)</span></label></th>
<td>
<input name="pass2" type="password" id="pass2" autocomplete="off" />
<br />
<div id="pass-strength-result">Strength indicator</div>
<p class="description indicator-hint">Hint: The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).</p>
</td>
</tr>
<tr>
<th scope="row"><label for="send_password">Send Password?</label></th>
<td><label for="send_password"><input type="checkbox" name="send_password" id="send_password" /> Send this password to the new user by email.</label></td>
</tr>
<tr class="form-field">
<th scope="row"><label for="role">Staff Type</label></th>
<td><select name="role" id="role">
<option value='employee'>Employee</option>
<option value='contractor'>Contractor</option>
</select>
</td>
</tr>
</table>
<p class="submit"><input type="submit" name="createuser" id="createusersub" class="button button-primary" value="Add New Staff Member " /></p>
</form>
</div>
<?php
}

Storing text box values in temporary table

I have a form that contain ten text boxes for entering some data. That form has a submit button and a next button. When I click on the next button, it has to open again ten text boxes, and has to store previous information into a temporary table in a MySQL database or in any other way. And finally, if I click on the submit button, it has to store that complete information into another permanent MySQL database table.
How can I do this?
This is what I tried so far:
<form name="form2" action="addingstuden_data.jsp" method="POST">
<table align="center">
<th colspan="3" class="style30"><font size="5">Adding Students for<%=stream%>-<%=year3%></th>
</table>
<table align="center" border="1">
<tr>
<td class="heading" align="center" >SNo</td>
<td class="heading" align="center" >Student Id</td>
<td class="heading" align="center">Student Name</td>
</tr>
<%
int i;
for(i=0;i<62;i++)
{
%>
<tr>
<td><input type="text" name="SNo" value="<%=i%>" id="r2" size="1"> </td>
<td><input type="text" name="stdid" id="sid" size="4" value=""> </td>
<td><input type="text" name="stdname" id="sname" value=""> </td>
</tr>
<% } %>
</table>
<table width="100%" class="pos_fixed">
<tr>
<td align="center" class="border"><input type="submit" name="submit" value="submit"> </td>
</tr>
</table>
</form>
Try this, Style it according to your need
<form name="myform" method="POST" action="youraction">
<div id="forms">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
<input type="text" name="feilds[]">
</div>
<input type="button" name="next" value="next" onclick="next()">
<input type="submit" name="submit" value="Submit">
</form>
Javascript code
function next()
{
var ele = document.getElementById('forms');
for(var i=0;i<10;i++)
{
var name = document.createElement("input");
name.setAttribute('type',"text");
name.setAttribute('name',"feilds[]");
ele.appendChild(name);
}
}
On server side loop through feilds[] array and get all the values

Categories