Why javascript not showing value in textfield - javascript

<html>
<head>Testing</head>
<body>
<?php
if(isset($_POST['postbtn'])){
echo "<script>alert('I am here')</script>";
echo "<script>document.getElementById('txt_name').value='edit'</script>";
}
?>
<div id="wrap">
<form id="data_entry" method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<div class="rowbox">
<input type="text" class="textbox" id="txt_name" value="">
</div>
<div class="btn">
<input type="submit" id="postme" name="postbtn">
</div>
</form>
</div>
</body>
</html>
I'm new to javascript and php, what I need to know is after submitting the form I can see the alert message "I am here" but I don't see the value that I am posting using the document.getElementById in the textfield for the html form. Why?
Thanks

Because the element has not loaded yet, move your script to the end of the body.
<?php
if(isset($_POST['postbtn'])){
echo "<script>alert('I am here')</script>";
echo "<script>document.getElementById('txt_name').value='edit';</script>";
}
?>
</body>
</html>

Related

How to submit POST requests if form is a value of innerHTML?

Every time I access $_POST on the other page, there is no value. How do we submit post request if the form is in innerHTML?
function edit(firstname){
var firstname_old = document.getElementById("firstname-div")
//Set id="FirstName" to your input field
firstname_old.innerHTML= 'First Name:<input type="text" id="FirstName" name="firstname" value="'+ firstname +'">';
}
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<button type="button" onclick="edit('<?php echo $firstname ?>')">edit</button><br>
<input type="submit" name="submit" value="submit">
</form>
When you are not in "edit-mode" your're putting out just a text.
<?php echo "First Name: $firstname<br>"; ?>
Thist text is inside the form which will be submitted but its not a form-field! To achieve what you want (deliver the content of "firstname" via ajax) you need a "hidden field".
So you put the same value which is shown in the text to a hidden input like this.
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<button type="button" onclick="edit('<?php echo $firstname ?>')">edit</button><br>
<input type="hidden" name="firstname" value="'<?php echo $firstname ?>'">
<input type="submit" name="submit" value="submit">
</form>
What I understand that you want to display a textbox to edit the name on clicking the edit button . Here is one solution for that:-
<script>
$(document).ready(function(){
$("#edit").click(function(){
document.getElementById("firstname-div").style.display = "none" ;
document.getElementById("firstname-div-edit").style.display = "block" ;
});
});
</script>
<HTML>
<form class="" action="summary.php" method="post">
<div id="firstname-div">
<?php echo "First Name: $firstname<br>"; ?>
</div>
<div id="firstname-div-edit" style="display:none;">
<input type="text" name="firstname" value="'<?php echo $firstname ?>'">
</div>
<button type="button" id="edit">edit</button><br>
<input type="submit" name="submit" value="submit">
</form>
</HTML>
Check and let me know if it works for you or not.

how to show login first alert and fade or disable the current page if not login and if login then don't show alert

suppose i have this post a job page
<?php include_once "session.php" ;?>
<div id="wraper">
<?php include_once 'header.php'; ?>
<?php include_once 'navigationjp.php'; ?>
<div id="main">
<div id="content">
<div >
<form class="form" action="" method="post" >
<div class="lbl"><label><h2>Job Title: </h2></label></div>
<input class="inpu" type="input" name="title" required placeholder="HR Manager" /><br>
<div class="lbl"><label><h2>Degree: </h2></label></div>
<input class="inpu" type="input" name="degree" placeholder="MBA (HR)" /><br>
<div class="lbl"><label ><h2>Posts: </h2></label></div>
<input class="inpu" type="input" name="posts" placeholder="03" /><br>
<div class="lbl"><label><h2>Company: </h2></label></div>
<input class="inpu" type="input" name="company" required="" placeholder="Diamond Pvt Ltd Company" /><br>
<div class="lbl"><label><h2>Contact No: </h2></label></div>
<input class="inpu" type="text" name="contact" id="contact" required placeholder="0423121212" /><br><br>
<div class="lbl"><label><h2> </h2></label></div>
<input class="inpu" type="submit" name="submit" value="Post job" />
</form>
</div>
</div>
<?php include_once 'sidebar.php'; ?>
</div>
<?php include_once 'footer.php'; ?>
</div>
and i want that when someone click on form to write something it should check either user is signed in or not , if not signed in then i want to show an alert to user to login first if user is not signed in. how can i do it by using jquery or php ? can somebody tell me the example code please i don't know how to do it?
Well, you have 2 ways to do it, first is check this when the user enter the page above with php, something like this:
<?php if (!$user) { >?
<script>alert("Please log in first")</script>
<?php }?>
this will check the user right after he entered the page, you should provide us with more info about your login method and code so we can help you more specifically.
if you want to check if he is logged in after he already visited the page, you will need to use AJAX for that, php runs once after the page is loaded, so your way to talk with the php code after the page is loaded is with AJAX.
<script>
$("input.inpu").click(function() { //when the user clicked on the input
//do your ajax here.
});
</script>
you should read more about ajax here: http://api.jquery.com/jquery.ajax/

pass variable value in action url php

How to pass variable values into url. Here is my code
<?php
if(isset($_POST['submit'])){
echo $v=$_POST['n'];
} ?>
<form method="post" action="/user/<?php echo $v; ?>">
<input type="text" name="n">
<input type="submit" name="submit" value="Submit">
After giving input value and click on submit shows in url:
if input value is raj after click on submit in url i want to display
/user/raj
The requirement can be achieved by using javascript.
<form id="myForm" method="post" action="" onsubmit="return false">
<input type="text" name="n">
<input type="submit" name="submit" value="Submit">
</form>
using jquery
$("#myForm").on("submit",function(){
var val=$("input[name='n']").val();
$(this).attr("action","<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>/user/"+val);
$(this).submit();
})
Use method="get" in your form tag.

Use PHP to display user inputs

What I'm trying to do is to use PHP to display the user inputs on a separate page, but when I process the form none of the input values display. I'm having to use javascript for input validation and that works fine but the PHP page is not working.
Here is my code:
HTML and Javascript
survey.php:
<!DOCTYPE html>
<html>
<head>
<title>Survey Page</title>
<link rel="stylesheet" type="text/css" href="mystylepage.css" />
<script type="text/javascript">
function validate()
{
if (document.information.name.value == "" )
{
alert("Please provide your name!" );
document.information.value.focus();
return false;
}
if (document.information.email.value == "")
{
alert("Please provide an email!");
document.information.value.focus();
return false;
}
if (document.information.major.value == false)
{
alert("Please enter your major");
document.information.value.focus();
return false;
}
return true;
}
</script>
</head>
<div id="bodycolor">
<body>
<div id="container">
<div id="menu">Menu:
<a class="menu" href="index.htm">Home</a> | <a class="menu"
`href="faculty.htm">Faculty</a> |
<a class="menu" href="courses.htm">
Courses</a> | <a class="gradecalculator" href="gradecalculator.htm">Grade
Calculator</a> | <a class="survey" href="survey.htm">Survey</a>
</div>
</div>
<div id="header">
<h1><center>CSE Center for Health Information Technology</center></h1>
<br>
<div id="links">
<center><a href="https://www.kennesaw.edu/"><img src="KSU Logo.jpg"
alt="Logo" style="width:100px;height:100px;" /></a></center>
<br>
<center><a href="http://ccse.kennesaw.edu/">College of Computing and
Software Engineering</a></center>
</div>
</div>
<h1>Please enter you information below</h1>
<form name="information" method="POST" action="action.php"
onsubmit="return
validate();">
<table width="500" border="0">
<tr>
<td width="150" bgcolor="#99CCFF"><strong>Name</strong></td>
<td><input type="text" name="name" size="20" maxlength="20" /></td>
</tr>
<tr>
<td bgcolor="#99CCFF"><strong>Email</strong></td>
<td><input type="text" name="email" size="20" maxlength="20" /></td>
</table>
<p> Major:
<input name="major" type="radio" value="Information Technology" />
Information Technology
<input name="major" type="radio" value="Software Engineering" />
Software Engineering
<input name="major" type="radio" value="Computer Science" />
Computer Science</p>
<p>
<input type="submit" value="Process" />
</p>
<p id="msg"></p>
</form>
</body>
</html>
Here is the PHP page:
action.php
<!DOCTYPE html>
<html>
<head>
<title>Survey Information Processing</title>
</head>
<body>
<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]?></p>
<p>Email: <?php echo $_POST["email"]?</p>
<p>Major: <?php echo $_POST["major"]?</p>
</body>
</html>
I'm totally new to PHP, so any help is greatly appreciated!
Your form method should be POST.
<form name="information" method="POST" action="action.php" onsubmit="return validate();">
And add a name to the submit button:
<input type="submit" value="Process" name="submit" />
Also, you could add this if condition:
<?php
echo "<pre>";
print_r($_POST); // Check if you're getting any POST data
echo "</pre>";
?>
<?php if (isset($_POST['name'])) { ?>
<h1>Form data</h1>
<p>Name: <?php echo $_POST["name"]; ?>
</p>
<p>Email: <?php echo $_POST["email"]; ?>
</p>
<p>Major: <?php echo $_POST["major"]; ?>
</p>
<?php } ?>
Hope this helps.
So the issue is your method is get but you're trying to access through post
Change
method="GET"
to
method="POST"
On your form.

Calling PHP function wth HTML button

I am developing website using php.I need to call php function using HTML Onclick event. How to do? Here I have given my code.
<?php
function insert() {
echo "in insert ";
$servername = "localhost";
$username = "shwetharao";
$password = "shwetha";
$dbname = "semilab";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$uname = $_POST['usernameu'];
$upass = $_POST['passwordu'];
echo $uname;
$sql = "INSERT INTO login (id, username, password)VALUES ('id','$uname','$upass')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>
<!DOCTYPE HTML>
<head>
<title></title>
</head>
<body>
<div id="main">
<div id="login">
<h2>Create User Account</h2>
<form action="" method="post">
<br><br><br>
<label>UserName :</label>
<br><br>
<input id="name" name="usernameu" placeholder="username" type="text">
<br><br><br>
<label>Password :</label>
<br><br>
<input id="password" name="passwordu" placeholder="**********" type="password">
<br><br><br>
<input name="button" type="button" value=" Create user " onclick="insert();">
</form>
</div>
</div>
</body>
</html>
I need to call function insert()[which is php function] on clicking button.How to achieve this?
Try this
<?php if(isset($_POST['submit'])){
insert();
}
?>
<!DOCTYPE HTML>
<head>
<title></title>
</head>
<body>
<div id="main">
<div id="login">
<h2>Create User Account</h2>
<form action="" method="post">
<br><br><br>
<label>UserName :</label>
<br><br>
<input id="name" name="usernameu" placeholder="username" type="text">
<br><br><br>
<label>Password :</label>
<br><br>
<input id="password" name="passwordu" placeholder="**********" type="password">
<br><br><br>
<input name="submit" type="submit" value=" Create user" >
</div>
</div>
</body>
</html>
you did two mistake first one is php tag at above, you wrote only "?" please change it to '<form action="" method="post">
inside of form you don't write action page name ..for example action = "myFile.php"
PHP: Is only run by the server and responds to requests like clicking on a link (GET) or submitting a form (POST).
HTML & Javascript: Is only run in someone's browser
I'm assuming your file looks something like:
<?php
function insert() {
echo 'I just ran a php function';
}
if(isset($_POST['submit'])){
insert();
}
?>
<!DOCTYPE HTML>
<head>
<title></title>
</head>
<body>
<div id="main">
<div id="login">
<h2>Create User Account</h2>
<form action="" method="post">
<br><br><br>
<label>UserName :</label>
<br><br>
<input id="name" name="usernameu" placeholder="username" type="text">
<br><br><br>
<label>Password :</label>
<br><br>
<input id="password" name="passwordu" placeholder="**********" type="password">
<br><br><br>
<input name="submit" type="submit" value=" Create user" >
</div>
</div>
</body>
</html>
Just change
<input name="button" type="button" value="Create user" onclick="insert();">
to
<input name="button" type="submit" value="Create user">
And paste your php code inside this
<?php if(isset($_POST['submit'])){
//paste you php code here
}
?>
<?php
if($_GET){
if(isset($_GET['insert'])){
insert();
}elseif(isset($_GET['select'])){
select();
}
}
function select()
{
echo "The select function is called.";
}
function insert()
{
echo "The insert function is called.";
}
?>
<input type="submit" class="button" name="insert" value="insert" />
<input type="submit" class="button" name="select" value="select" />
===============================================
<?php
if($_GET['button1']){fun1();}
if($_GET['button2']){fun2();}
function fun1()
{
//This function will update some column in database to 1
}
function fun2()
{
//This function will update some column in database to 2
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
</head>
<body>
<button id="btnfun1" name="btnfun1" onClick='location.href="?button1=1"'>Update to 1</button>
<button id="btnfun2" name="btnfun2" onClick='location.href="?button2=1"'>Update to 2</button>
</body>
</html>
I did it using this code. Hope this helps.

Categories

Resources