1) Why does the URL show "&submit=Submit" at the end? What should be done to get only the two variables name and age?
2) Does isset function work well with GET method as well? Is there another function or way to handle this?
Result screenshot
<html>
<head>
<title>My first PHP page</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
Name: <input type="text" name="name"/>
Age: <input type="text" name="age"/>
<input type="submit" name="submit"/>
</form>
</body>
</html>
<?php
if ( isset($_GET['submit']) ) { //was the form submitted?
echo "Welcome ". $_GET["name"] . "<br>";
echo "You are ". $_GET["age"] . "years old<br>";
}
?>
The URL is showing variable -> value key pairs. The name= attribute supplies the var name, the element value provides the value. Remove the name= from the submit button. Problem solved.
Yes, isset works with both GET and POST methods.
Related
------------code 1 ------------
<!DOCTYPE html>
<html>
<body>
<form action="#" method="POST">
<input type ="text" value="default text" name="someName">
<input type ="submit" value ="Submit" >
</form>
<?php echo "Entered data is -->".$_POST["someName"]; ?>
</body>
</html>
----------------- code 2 ------------------
<?php
echo $htmlDoc = <<< HTML
<html>
<body>
<input type="button" value="Add Input Field" onClick="addField();">
<p id="addHere"></p>
</body>
</html>
HTML;
echo ' Entered Data is as : '.$_POST["someName"];
?>
<script>
function addField()
{
document.getElementById("addHere").innerHTML
='<form action="#" method="POST">'+
'<input type ="text" value="default text" name="someName">'+
'<input type ="submit" value ="Submit" >'+
'</form>';
}
</script>
code1 works fine
but in code2 after submit text input field disappears why? how to retain it and retain value entered in it?
that's because your form is not by default part of your DOM document.
you are appending your form using Javascript into your DOM document using a specific event onClick="addField();".
and while Form Submission is a process that sends a POST/GET data to the server -Server Side Process- then the server return back with some data -refreshes the page-, so all your DOM changes will be forgotten or restarted so to speak after this submit .
So, to work around this, you may set your form as a part of your DOM document, and control the viability by javascript.
I have simple form:
<div class="form-style-2">
<form action="" name="formular" id="formular" method="GET">
<label for="actual_position"><span>From: <span class="required"></span></span><input name="actual_position" type="text" maxlength="512" id="actual_position" class="searchField"
<?php if(!empty($actual_position)){ ?>
value="<?php echo $_GET['actual_position']?>"
<?php
}else {
?> value = ""; <?php
} ?>/></label>
<label for="final_position"><span>To: <span class="required"></span></span><input name="final_position" type="text" maxlength="512" id="final_position" class="searchField" <?php if(!empty($final_position)){ ?>
value="<?php echo $_GET['final_position']?>"
<?php
}else {
?> value = ""; <?php
} ?>/></label>
<input type="submit" value="Find path" />
And another multiselect in form who gets values form url link and compere with database and get som results. Here is a code:
<table width= "570px">
<tr><td width="200px" style="align:center"><b>Waypoints:</b> <br>
<tr><td width="370px"><select style="float:center; margin-left:5px" multiple id="waypoints">
if(!empty($urls)){
foreach($urls as $url){
if($result = $conn->query("SELECT * FROM $table where $ID = '$url' "));
$atraction = $result->fetch_array(); ?>
<option value="<?php echo $atraction['lat']. "," . $atraction['lon']; ?>"
> <?php echo "<b>".$atrction['City']. ", " . $atraction['Name'];?> </option>
<?php
}
}
?>
</select></td></tr>
<br>
</table>
</form>
And getting ID-s from url code:
if(!empty($_GET[$ID])){
$urls = $_GET[$ID];
foreach($urls as $url){
// echo $url;
}
}
... and after submit, it Post to URL some variables like this:
http://127.0.0.1/responsiveweb/travel.php?actual_position=Paris&final_position=Praha&ID[]=23&ID[]=15&ID[]=55
... very important for me are ID-s values ... but when I change for example actual position and then submit I lost my ID-s and I get something like this: http://127.0.0.1/responsiveweb/travel.php?actual_position=Berlin&final_position=Praha
Can you help me how to get after clicking on submit button full url link? Thanks
I had some trouble understanding your question OP, but I think I understood somehow what you ment, so I decided to try giving you a answer.
I have re-written your code, and tried to make somehow better code-structure. I have also used form method POST in my example, so you can see how you can change the get data on the redirection url.
See my code example here: http://pastebin.com/wQ7QCBmt
I also decided to use the form method POST instead of GET, so you can easily do back-end tasks, and extend your link if neccessary. You could also add more data to the link even when using GET. You could add an hidden input inside your form, example:
<input type="hidden" name="more_data" value="a_value" />
everybody.
I have the following situation:
I have:
http://example.com/ and http://example.com/new
In example.com, I have some forms that I load in example.com/new domain with fancybox iframe.
My form, basically shows some fields for the user to enter his pessoal data, like name, phone and etc... After he submit that, I show some user agreement terms that comes from database and a checkbox for the user to say that he agree with the terms.
After he check and submit, I want to alert some sucess message and the fancybox modal/iframe to close and thats it.
In the form page, i've loaded jquery, and bootstrap. So, when the user agree, I print:
<?php
echo "
<script>
alert('Some success message!');
$(document).ready(function(){
parent.$.fancybox.close();
});
</script>
";
?>
I have three forms, in one, works, in the other two, i get:
Error: Permission denied to access property '$'
The only difference between the form that works and the other two, is that in the form that works, i don't have the agreement terms coming from database, only the checkbox.
I could put my entire code here, but would be a giant question. But if you guys need, I can update.
Sorry for my english and forgive-me if I was not clear.
UPDATE:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php
/* Connect with DB */
require_once('require/conectar.php');
if(!empty($_POST))
foreach($_POST as $k => $v)
$$k = $v;
?>
<script type="text/javascript" src="http://example.com/new/assets/js/jquery.js"></script>
</head>
<body>
<?php if(!isset($agree) and !isset($next)): ?>
<h1>The form</h1>
<form method="post" action="">
<label>Your name:</label>
<input type="text" name="name">
<br>
<label>Your email:</label>
<input type="text" name="email">
<br>
<input type="submit" name="next">
</form>
<?php
else:
$error = (!isset($name)) ? true : false;
$error = (!isset($name)) ? true : false;
if($error)
{
echo '<script>You must fill all fields before submit.</script>';
exit;
}
$qrr = mysql_query("SELECT * FROM `terms`");
$terms = mysql_fetch_object($qrr);
?>
<h1>Terms:</h1>
<?php echo $terms->content; ?>
<form method="post" action="">
<input type="hidden" value="<?php echo $name; ?>" name="name">
<input type="hidden" value="<?php echo $email; ?>" name="email">
<input type="checkbox" value="1" name="accept"> I agree.
<input type="submit" name="agree">
</form>
<?php
endif;
if(isset($agree))
{
/*
Here i mail me the user data.
*/
echo "
<script>
alert('Soliciação Realizada com sucesso!');
$(document).ready(function(){
parent.$.fancybox.close();
});
</script>
";
}else
{
echo "<script>alert('You need to agree with the terms to proceed.');</script>";
}
?>
</body>
</html>
This is a browser security thing. While there's a few ways around it, the best one is probably to use the postMessage API.
On your example.com parent page, add some code like this:
function handleMessageFromiFrame(event) {
alert('Some success message: ' + event.data);
//$.fancybox.close();
}
window.addEventListener("message", handleMessageFromiFrame, false);
And, then on your child example.com/new iframe, add code like this:
var parentOrigin = "*"; // set to http://example.com/ or whatever for added security.
function sendMessageToParent(){
parent.postMessage("button clicked", parentOrigin);
}
$('#submit-btn').click(sendMessageToParent);
Here's an example of it in action:
Parent example.com page: http://jsbin.com/hiqoyevici/1/edit?html,js,output
Child example.com/new iframe: http://jsbin.com/goferunudo/1/edit?html,js,output
When you click the button in the child page, it uses postMessage to notify the parent. Then the parent listens for the message and does whatever action you want.
I am having difficulty passing a variable from a first form to a second form. There are four scripts involved: debug.php, getVar.php, printme.php and scripta.php. Running debug.php and typing "blah" for a "password to continue", select scripta.php from the pulldown and hit "Submit", I expect to see $dbpass="blah" for all of the scripts. I see it for the first page, but after the second pages' "Submit" button is pressed, the value is forgotten once inside of "printme.php". I suspect this has to do with variable scope. Any help is appreciated.
debug.php:
<html>
<body>
<form name="gateway" action= "" method="POST">
<fieldset>
<label>password to continue:</label>
<input type="text" id="dbpass" name="dbpass">
<label>Select Script:</label>
<select name="scriptSelect" id="scriptSelect">
<option value="">Please make a selection</option>
<option value="scripta.php">scripta</option>
</select>
<input name="updateGateway" type="submit" value="Submit">
<input name="resetForm" id="resetForm" type="reset" value="Reset Form">
</fieldset>
</form>
</body>
</html>
<script type="text/javascript">
document.getElementById('scriptSelect').addEventListener('change', function(e){
var selected_value = e.target.value;
document.forms['gateway'].action = selected_value;
alert(selected_value);
});
</script>
scripta.php:
<html>
<body>
<?php require 'getVar.php'; ?>
<form name="secondform" action= "printme.php" method="POST">
<fieldset>
<label>Hit submit to continue:</label>
<input name="updateScripta" type="submit" value="Submit">
</fieldset>
</form>
</body>
</html>
getVar.php:
<?php
if (isset($_POST['dbpass'])) {
$dbpass = #$_POST["dbpass"];
}
echo "you entered $dbpass";
?>
printme.php:
<?php
echo "Inside of printme, you entered $dbpass";
?>
Thanks
In scripta.php, add the line: <input type="hidden" name="dbpass" value="<? echo $dbpass ?>" /> somewhere inside the form tag.
In printme.php, add this line at the top of the page <? $dbpass = $_POST['dbpass'] ?>
There may be other errors in the scripts you have provided. Check back once you have made the above changes.
Mate... that's not how it works... Each request is independent...
In order to pass a value from one script to the other you either use sessions ($_SESSION) or you need to repost the variable.
Also, I don't know what you're trying to accomplish but passing passwords around, in plain text...
Repost variables
In scripta.php add this
<input name="dbpass" value="<?php $dbpass; ?>" type="hidden"/>
to your form. This will send the value contained in $dbpass in a hidden value.
Then, in printme.php you need to retrieve the value so just require that getVar.php script
require 'getVar.php';
echo "Inside of printme, you entered $dbpass";
Using Sessions:
change your getVar.php
session_start();
if (isset($_POST['dbpass'])) {
$_SESSION['dbpass'] = $_POST["dbpass"]; // you don't need that #
} else {
$_SESSION['dbpass'] = null;
}
then in your subsequent scripts, everytime you want to access dbpass just use
session_start();
$_SESSION['dbpass']
example:
<?php
session_start();
$_SESSION['dbpass']
echo "Inside of printme, you entered $dbpass";
I have a home_admin.php file where the user can reject an entry with this code:
echo "
<td>
<a href=changeStatReject.php?id=" . $row['id'] . ">" . "
<img src='media/reject.png'>" . "
</a>
</td>
";
Once the link is clicked, it will direct the user to the changeStatReject.php page with the specific ID of the entry.
My goal is to show a JavaScript PromptBox asking the reason for rejecting before the code is executed.
I use this code:
function MyReason(){
var reason = prompt("Reason:");
document.getElementById("reason").value = reason;
document.getElementById("form").submit();
}
Then I add this to the home_admin.php page:
<form id="form" method="POST" action="changeStatReject.php">
<button onclick="MyReason()">Click Me!</button>
<input type="hidden" id="reason" name="reason"/>
</form>
Even though I get the value from the prompt box, the script for the changeStatReject action is not working anymore.
I think there's a conflict with the <form action="changeStatReject.php"> and <a href=changeStatReject.php?id=" . $row['id'] . ">
Any advice on how to get the prompt box value, save it to the database and at the same time update (reject) the status of the specific entry?
Thanks Mates,
<?php
echo "<td> <a href= "#" onclick='myreason(".$row['id'] . "')>" . "<img src='media/reject.png'>" . "</a></td>";
?>
.............
function MyReason(id){
id.preventDefault();
var reason = prompt("Reason:");
document.getElementById("reason").value = reason;
document.getElementById("id").value = id;
document.getElementById("form").submit();
}
.............
<form id="form" method="POST" action="changeStatReject.php">
<input type="hidden" id="id" name="id" />
<input type="hidden" id="reason" name="reason"/>
</form>
...............
Now you can handle the POST variables in your changeStatReject.php as you want.
add <input type="hidden" name="id" value='id_you_need_to_pass'/> in your form and call changeStatReject.php through this form submit.