Problem with javascript with alert are not showing - javascript

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

Related

Store data on my XML file while redirecting it to my home page using PHP and Javascript

I have created a PHP page where there is a form which would store data on an XML file.
I have written the code of PHP which could store the data but now I have to redirect it to my homepage also.
But the main error is when I write the Javascript code for redirecting the page, then the data is not stored.
Now I have currently written the code of Javascript in the PHP code itself.
I will provide the code for my page below.
<html>
<head>
<title>Login</title>
</head>
<body bgcolor="#f0f0f0">
<?php
if(isset($_REQUEST['ok']))
{
$xml = new DOMDocument("1.0","UTF-8");
$xml->load("a.xml");
$rootTag = $xml->getElementsByTagName("document")->item(0);
$dataTag = $xml->createElement("data");
$aTag=$xml->createElement("a",$_REQUEST['a']);
$bTag=$xml->createElement("b",$_REQUEST['b']);
$dataTag->appendChild($aTag);
$dataTag->appendChild($bTag);
$rootTag->appendChild($dataTag);
$xml->save("a.xml");
}
echo "<script type=\"text/javascript\">
function Home()
{
window.location.href=\"navbar.php\";
}
</script>
"
?>
<form action="index.php" method="post">
<table cellspacing="20px" style="margin-left:500px;" id="atable">
<tr>
<td>User Name </td>
<td> <input type="text" name="a" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td> Password </td>
<td> <input type="password" name="b" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td>
<input type="button" name="ok" value="Login" onclick="Home()" />
</td>
</tr>
</table>
</form>
</body>
</html>
Try this. You need to use header("Location:navbar.php");
<?php
if(isset($_REQUEST['ok'])){
$xml = new DOMDocument("1.0","UTF-8");
$xml->load("a.xml");
$rootTag = $xml->getElementsByTagName("document")->item(0);
$dataTag = $xml->createElement("data");
$aTag=$xml->createElement("a",$_REQUEST['a']);
$bTag=$xml->createElement("b",$_REQUEST['b']);
$dataTag->appendChild($aTag);
$dataTag->appendChild($bTag);
$rootTag->appendChild($dataTag);
$xml->save("a.xml");
header("Location:navbar.php");
exit;
}
?>
<html>
<head>
<title>Login</title>
</head>
<body bgcolor="#f0f0f0">
<form action="index.php" method="post">
<table cellspacing="20px" style="margin-left:500px;" id="atable">
<tr>
<td>
User Name </td>
<td> <input type="text" name="a" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td> Password </td>
<td> <input type="password" name="b" width="75" /> </td> <!--Inserting textbox in the page with justified width-->
</tr>
<tr>
<td>
<input type="submit" name="ok" value="Login" />
</td>
</tr>
</table>
</form>
</body>
</html>

Force update screen, reboot server and refresh with Javascript

I'm trying to do a web server based application. I'm working on the internal configuration of the server and I want it to be able to be modified by the user. I'm using a raspberry pi 3 as web server.
Right now I'm trying to configure the Static IP, for this I have modified the dhcpc.conf file with php code. After that I want to reboot the raspberry so it takes the new configuration. I am already able to do that, but when I force the reboot, the server doesn't send the information to the web browser and I am getting page not found.
What I want to know is how to force the server to update the web page before the shutdown, and then how (using javascript may be) reload the page after XX seconds (when the raspberry is booted again).
This is my code until now:
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Configuración: Red</title>
<link href="/TrafficVC/ccs/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/configuraRed.js"></script>
<style>
</style>
</head>
<body>
<h1 class="pagesTitle">Configuración: Red</h1>
<form action="" method="post" name="chNetConf">
<table class="tbChNetConf" id="chNetConfTable">
<tr>
<td class="izq">Hostname:</td>
<td class="der"> <input type="text" name="Hostname" <?php echo "value=".gethostname();?>> </td>
</tr>
<tr>
<td class="izq" style="font-weight:bold">Conexión Ethernet: </td>
<td class="der">
<label>
<span>IP Estática</span>
<input type="checkbox" id="chbEthStaticEnable" name="chbEthStaticEnable" onchange="enableEthStatic(this)">
</label>
</td>
<td class="izq" style="font-weight:bold">Conexión Wireless: </td>
<td class="der">
<label>
<span>Habilitar</span>
<input type="checkbox" id="chbWlanEnable" name="chbWlanEnable" onchange="enableWlan(this)">
</label>
<label>
<span>IP Estática</span>
<input type="checkbox" id="chbWlanStaticEnable" name="chbWlanStaticEnable" onchange="enableWlanStatic(this)" disabled>
</label>
</td>
</tr>
<tr>
<td colspan="2" style="vertical-align:top">
<table class="tbChNetConf">
<tr>
<td class="izq">IP:</td>
<td class="der">
<input type="text" id="EthIP1" name="EthIP1" disabled style="width:15%">
.
<input type="text" id="EthIP2" name="EthIP2" disabled style="width:15%">
.
<input type="text" id="EthIP3" name="EthIP3" disabled style="width:15%">
.
<input type="text" id="EthIP4" name="EthIP4" disabled style="width:15%">
</td>
</tr>
<tr>
<td class="izq">Mascara:</td>
<td class="der">
<input type="text" id="EthMask1" name="EthMask1" disabled style="width:15%">
.
<input type="text" id="EthMask2" name="EthMask2" disabled style="width:15%">
.
<input type="text" id="EthMask3" name="EthMask3" disabled style="width:15%">
.
<input type="text" id="EthMask4" name="EthMask4" disabled style="width:15%">
</td>
</tr>
<tr>
<td class="izq">Puerta de enlace:</td>
<td class="der">
<input type="text" id="EthGate1" name="EthGate1" disabled style="width:15%">
.
<input type="text" id="EthGate2" name="EthGate2" disabled style="width:15%">
.
<input type="text" id="EthGate3" name="EthGate3" disabled style="width:15%">
.
<input type="text" id="EthGate4" name="EthGate4" disabled style="width:15%">
</td>
</tr>
</table>
</td>
<td colspan="2";>
<table class="tbChNetConf">
<tr>
<td class="izq">SSID:</td>
<td class="der">
<input type="text" id="WlanSSID" name="WlanSSID" disabled style="width:80%">
</td>
</tr>
<tr>
<td class="izq">Contraseña:</td>
<td class="der">
<input type="password" id="WlanPass" name="WlanPass" disabled>
</td>
</tr>
<tr>
<td class="izq">IP:</td>
<td class="der" >
<input type="text" id="WlanIP1" name="WlanIP1" disabled style="width:15%">
.
<input type="text" id="WlanIP2" name="WlanIP2" disabled style="width:15%">
.
<input type="text" id="WlanIP3" name="WlanIP3" disabled style="width:15%">
.
<input type="text" id="WlanIP4" name="WlanIP4" disabled style="width:15%">
</td>
</tr>
<tr>
<td class="izq">Mascara:</td>
<td class="der" style="table-layout: fixed; width=100%">
<input type="text" id="WlanMask1" name="WlanMask1" disabled style="width:15%">
.
<input type="text" id="WlanMask2" name="WlanMask2" disabled style="width:15%">
.
<input type="text" id="WlanMask3" name="WlanMask3" disabled style="width:15%">
.
<input type="text" id="WlanMask4" name="WlanMask4" disabled style="width:15%">
</td>
</tr>
<tr>
<td class="izq">Puerta de enlaceqe:</td>
<td class="der">
<input type="text" id="WlanGate1" name="WlanGate1" disabled style="width:15%">
.
<input type="text" id="WlanGate2" name="WlanGate2" disabled style="width:15%">
.
<input type="text" id="WlanGate3" name="WlanGate3" disabled style="width:15%">
.
<input type="text" id="WlanGate4" name="WlanGate4" disabled style="width:15%">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan="4" style="text-align:center;">
<input type="submit" value="Guardar" name="submitChNet">
</td>
</tr>
</table>
</form>
<?php
if (isset($_GET['Err']))
{
echo "Error " . $_GET['Err']. ": ";
if ($_GET['Err']=="EthIP")
echo "La dirección IP de la conexión ethernet no es correcta";
if ($_GET['Err']=="EthMask")
echo "La máscara de la conexión ethernet no es correcta";
if ($_GET['Err']=="EthGate")
echo "La puerta de enlace de la conexión ethernet no es correcta";
}
?>
</body>
</html>
<?php
if (isset($_POST['submitChNet']))
{
include('phpCode/chNetConf.php');
}
?>
The file phpCode/chNetConf.php after writing the new file calls another shutdown.php file:
<?php
echo exec('sudo reboot',$output);
?>
I hope I'm clear enough and you can help me.
By the way, there is no need of security. Also, I know this can be done because I took the idea on how my router can be configured.

How to validate password with atleast one numeric, one Uppercase letter and 6-20 characters?

I have added this script to my form and its working with alpha numeric.The problem is when i click on wrong format on click function it will updated the data in database with submit button.How can i block? I need to update data with correct alert function.
function CheckPassword(inputtxt) {
var passw = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
if (inputtxt.value.match(passw)) {
alert('Congratulations You have successfully changed your password!..Plz Logout and signin again.');
return true;
} else {
alert('Wrong Format...!')
return false;
}
}
<script src="check-password-2.js"></script>
<?php
if(isset($_POST['submit']) )
{
$password = $_POST['newpassword'];
$sql = mysql_query("UPDATE tbl_login SET str_password='$password',status='1' where str_username='$user'");
}
?>
<body onload='document.form1.newpassword.focus()'>
<form name="form1" action="" method="post" id="form1" onClick="CheckPassword(document.form1.newpassword)">
<table width="388" align="center"></br>
</<tr>
<td align="right">Enter Username</td>
<td align="center">
<input type="username" name="username" value="<?php echo ucwords($_SESSION['username']); ?> " class="TxtBox" disabled="disabled" />
</td>
</tr>
<h5>Input Password and Submit [6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter]</h5>
<tr>
<td align="right">New Password</td>
<td align="center">
<input type="password" name="newpassword" class="TxtBox" />
</td>
</tr>
<tr>
<td align="center">
<div align="center">
<input type="submit" name="submit" value="submit" />
</div>
</td>
</tr>
</table>
</form>
</body>
Firstly you need to use the onsubmit attribute of the form, not onclick. Secondly, you need to use a return statement within the attribute to provide the value returned by the function to the event handler to prevent or allow the form submission. You also need to fix several errors in your HTML. Try this:
<body onload='document.form1.newpassword.focus()'>
<form name="form1" action="" method="post" id="form1" onsubmit="return CheckPassword(document.form1.newpassword)">
<table width="388" align="center"></br>
<tr>
<td align="right">Enter Username</td>
<td align="center">
<input type="username" name="username" value="<?php echo ucwords($_SESSION['username']); ?> " class="TxtBox" disabled="disabled" />
</td>
</tr>
<tr>
<td colspan="2">
<h5>Input Password and Submit [6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter]</h5>
</td>
</tr>
<tr>
<td align="right">New Password</td>
<td align="center">
<input type="password" name="newpassword" class="TxtBox" />
</td>
</tr>
<tr>
<td align="center">
<div align="center">
<input type="submit" name="submit" value="submit" />
</div>
</td>
</tr>
</table>
</form>
</body>

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