submitting form using javascript, saving it to $_POST - php

I'm trying to use an a tag to submit a form but the PHP doesn't seem to react:
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
echo 'dog';
else
echo 'cat';
$currentPage = 'test';
echo <<<eod
<form method='post' action='$currentPage.php' id='sign_in' style='margin: 0; padding: 0;'>
Username: <input type='text' name='username' style='width: 90px;'>
Password: <input type='text' name='password' style='width: 90px; border-right: 1px solid #bbb'>
<a id='sign_in_submit' name='submit'>Sign in</a>
</form>
eod;
?>
<script>
var form = document.getElementById('sign_in');
document.getElementById("sign_in_submit").addEventListener("click", function () {
form.submit();
});
</script>
</body>
</html>
tried three methods of submission via JS but none of them seem to work

Your submit a tag would not be included in the post data, so check for the existence of one of the input tags, say $_POST['username']

try in javascript this:
document.sign_in.submit();
and make a submit button like this
<input type="submit" id='sign_in_submit' value="Sign in" name='submit'

I have just tested your code. You are checking if submit field/button is sent over the form, but you do not have anything named submit in your form. You have got username and password, so changing your if(isset($_POST['submit'])) to if(isset($_POST['username'])) or if(isset($_POST)) will fix your issue.

Related

How to skip part of php when webpage reloads on form input submit?

I have below a short program. When I press the submit button, the button is supposed to turn green. This works as intended. However, when I press the submit button, the whole page reloads and gives an alert that I only intends it to do when the page initially loads and not every time when I press the submit button. Every time I press the submit button, the only thing that I intends is to have the button turn green. Is there a way to prevent the first line of php from executing when I press the submit button? Much appreciated.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles/normalize.css">
<style type="text/css">
.sub
{
background-color: #1f5a7c;
border: none;
font-weight: bold;
cursor: pointer;
}
</style>
</head>
<body>
<form method="POST" >
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="sub">
</form>
</body>
</html>
<?php
echo "<script type='text/javascript'>alert('Start')</script>";
if(isset($_POST["TestButton"]))
{
echo "<script type='text/javascript'>TestButton.style.background='green';</script>";
}
?>
That php will run every time .. You can use an else to prevent this.. IE
<?php
if(isset($_POST["TestButton"]))
{
echo "<script type='text/javascript'>TestButton.style.background='green';</script>";
}else{
echo "<script type='text/javascript'>alert('Start')</script>";
}
?>
Try this:
<?php
if(!isset($_POST['TestButton'])) {
echo "<script type='text/javascript'>alert('Start')</script>";
}
<?php>
You can use PHP conditional statements to skip the part of code.
You can wrap states of the form in if...else statement.
<?php
if(isset($_POST["TestButton"])) {
?>
<form method="POST" >
<!-- Here you can change it as you want to -->
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="sub green someother_class">
</form>
<?php
else {
?>
<form method="POST" >
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="sub">
</form>
<?php
}
?>
Or you can make it short by adding a variable and changing it's value accordingly-
<?php
if(isset($_POST["TestButton"])) {
$classes = "sub green some_other_class";
else {
$classes = "sub";
}
?>
<form method="POST" >
<!-- Here you can change it as you want to -->
<input type="submit" name="TestButton" id="TestButton" value="Test Button" class="<?php echo $classes; ?>">
</form>

Align Form in PHP

How can I center align a form thatI created?
$form = <<<END
<form method='post' action=''>
Adresa IP host : <input type='text' name='host'><br><br>
<input type='submit' name='submit' value='Connect'>
</form>
END;
echo $form;
$t = new TELNET();
if (!empty($_POST)){
$host = $_POST['host'];
echo("CONNECT:".$t->Connect($host, $name, $pass)."<br>");
echo("LOGIN:".(int)$t->LogIn());
echo("<br>Status Interfete:<br>");
$interfaces_status = ($t->GetOutputOf("show interface status"));
foreach ($interfaces_status as $value) {
echo "$value <br>";
I want the form to be on the center of the page.
In css:
form {
width: 800px;
margin: auto;
}
You need to done this using css. Please put form in one div and apply css according to div class.
<div class="frmCntr">
<form method='post' action=''>
Adresa IP host : <input type='text' name='host'><br><br>
<input type='submit' name='submit' value='Connect'>
</form>
</div>
and then add css in your css file like this:
.frmCntr{
width:80%; margin:auto;
}
or
form {
width:80%; margin:auto;
}
You can use margin:auto.
<form method='post' action='' style='margin:auto'>
If you not set a width for your form, you have to do it to get margin working in this case.
<form method='post' action='' style='margin:auto; width:400px'>

How to get value from php to html tag?

My first HTML file has a form a with a radio button. I want the second file (which is PHP) to print a message about what they choose. With that message thought, I would like to have some text formating (size, color, etc.).
file.html:
<html>
<body>
<form action="file.php" method="post">
<input type="radio" name="favorite" value="rbOne">
<input type="radio" name="favorite" value="rbTwo">
<input type="submit" value="Submit">
</form>
</body>
</html>
file.php:
<html>
<head>
<style>
body{
background-color:white;
}
p{
color:white;
font-size:50px;
font-weight:bold;
}
</style>
</head>
<body>
<?php $choice = $POST_['favorite']; ?>
<p align="center">Thanks for voting for <?php echo "$choice" ?>!!</p>
</body>
</html>
When I click the Submit button, it going to the file.php page, but it displays this:
Thanks for voting for !!
It makes $choice blank.
You need PHP opening and closing tags:
<?php echo $choice;?>
Also, $POST_['favorite'] should be $_POST['favorite']

Submit button not submitting

<div class = 'buttons'>
<button type="submit" class="regular" name="save">
<img src="elephant.png" alt=""/>
Memory
</button>
</div>
This is the code for a submit button
however when i try validate the form using php, the button does not seem to be submitting
<?php if( isset($_POST['save']) && $_POST['save'])
{
$submit = $_POST['save'];
echo "lol";
}
else
{
echo "lola";
}
Submit buttons only work in forms. Try to wrap the whole thing in a form tag and try again.
Your submit button doesn't have any value to send when posting the form. The <button> element does not send its element's content as its submit value. You still have to add a value attribute to the element in order for it to have a submitted value. Since you don't, it's sending an empty string, which is causing your conditional statement to fail:
if( isset($_POST['save']) && $_POST['save'])
Since $_POST['save'] is empty, that second part returns false and thus it goes to your else block.
Here is the code that you want:
<form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div class="buttons">
<button type="submit" class="regular" name="save" value="Save">
<img src="elephant.png" alt="Elephant" />
Memory
</button>
</div>
</form>
As written already your code lacks the wrapping. While submit button as a button might work without the form, then catching the _POST data surely doesnt.
Also, your solution for image-button could be better, maybe something like this:
<style type="text/css">
input[type="submit"] {background: url('elephant.png') right no-repeat; padding: 10px; padding-left: 30px; font-size: 14px; font-weight: bold;} /* or call this .regular */
</style>
<?
if ($_POST['save']) { // the trigger
echo '<h1>Cooking with fire now!</h1>';
}
?>
<form method="POST" action="">
<div class="buttons">
<input type="submit" name="save" value="Memory" class="regular" />
</div>
</form>
Note that class = "buttons" with the spaces, is incorrect syntax!
You should include your code within <form method="post" action="path/to/your/php/processing/file"> and </form> tags
Replace your button code with <input type="submit" class="regular" name="save" />
Remove isset($_POST['save']) && part from condition of if as you don't have set value for your button.

dynamically changing target of a form

I am trying to display my login inside an iframe which loads in the middle of the start page with gray backgroud, however i have a problem dealing with the target of the iframe, things should work as follows:
1- if user logged in successfully home page should load in the parent window
2- if error, error message should be displayed in the iframe itself.
my code:
<div id="cover5" onclick="javascript:cover5();">
<iframe name="warning" src="SignIn.php" id="warning" onclick="javascript:cover5();">
</iframe>
</div>
SignIn.php
<html>
<head>
<script type='text/javascript'>
function valid(){
if(document.getElementById('username').value.toString().length>=1 || document.getElementById('password').value.toString().length>=1){
return true;
}
else{
alert("The Combination of username and password are Incorrect !");
return false;
}
}
</script>
<script type='text/javascript'>
function ChangeTarget() {
document.getElementById("login").prop("target", "_parent")
}
</script>
<script type='text/javascript'>
function ChangeText(text) {
document.getElementById("label1").innerHTML= text;
}
</script>
<title></title>
</head>
<body>
<form name="login" id='login' target='_self' method='post' accept-charset='UTF-8'>
<fieldset>
<legend>SignIn</legend>
<input type='hidden' name='submitted' id='submitted' value='1'/>
<label for='username' >Email:</label>
<input type='text' name='email' id='username' maxlength="50" /> <br />
<label for='password' >Password:</label>
<input type='password' name='password' id='password' maxlength="50" /> <br />
<br /> <label id="label1" style="color: red"></label> <br />
<input type='submit' name='Submit' value='Login'/> <br />
<a href='resetPassword1.php'> Forgot/Reset Password? Click Here</a>
</fieldset>
</form>
however, the target is not changed by method ChangeTarget() which is called inisde php code, so the problem now is that everything load inside the iframe or everything inside the parent window. anyone knows how to solve the problem?
server side script:
mysql_select_db("mydb",$check);
$email = $_POST['email']; //username that will be submit from a form
$password = $_POST['password']; //password that will be submit from a form
// if(mysql_num_rows($temp_privileges_id)== 1) //if the id found that means that the user is already assigned to a conference
// {
echo '<script type="text/javascript">',
'ChangeTarget();',
'</script>';
header('Location: main.php'); // transefer to home page with variable username
}
There is no method prop. Either set the value of the target attribute directly
document.getElementById("login").target = "_parent";
or use setAttribute
document.getElementById("login").setAttribute("target", "_parent");
.prop() is a jQuery function. If you're going to do it in jQuery, do this:
$("#login").prop("target", "_parent");
If you're not going to do it in jQuery, do it like this in straight JavaScript:
document.getElementById("login").target = "_parent";

Categories