posting form data from iframe src - javascript

login.php
<iframe id ="login_iframe" name="login_iframe" src ="iframe.php"></iframe>
and in my iframe.php
<html>
<head>
</head>
<body>
<form id="login_submitform" method="post" action="post.php" target="_top" />
<input type="text" name="customer" value=""/>
<input type = "text" name="phone" value=""/>
<input type= "submit" value="submit" />
and in my post.php comes the result
$customer = $_POST['customer'];
$phone = $_POST['phone'];
?>
RESULT
customer = empty string
phone= empty string
what am i doing wrong here? how can i get the form values to post.php. pls assist

Everything looks fine, things to be noted here :
You can only access data of iframe in same domain, your login.php, iframe.php and post.php must be in same domain, here assuming you are testing on localhost
please restrict your form fields, with required. you are good to go.
<form id="login_submitform" method="post" action="post.php" target="_top" />
Customer: <input type="text" name="customer" value="" required/>
Phone: <input type = "text" name="phone" value="" required/>
<input type= "submit" value="submit" />
</form>
It will not submit if value are empty.
post.php
<?php
echo '<pre>';
print_r($_POST);
echo $_POST['customer'] .'<br>';
echo $_POST['phone'] .'<br>'
?>

Related

how to prin a message in screen and then redirect to other page php/js

i have a form and i connected with php to a database.It works perfect but i 'd like to show me o pop up message that is succesfull login .
<form id = "quiz" name="quiz" method="post" action="insert.php" >
name: <input id="name" type="text" name="name" placeholder="π.χ. George" required="">
<label for="name"> </label>
faculty: <input type="text" name="faculty" placeholder="Enter your Faculty" required="">
<input class="btn btn-one" type="submit" value="Submit" />
</form>
insert.php is the php file that connect the form fields with the database.
then try this in this file:
echo "<script>alert('message successfully sent');</script>";
header('Location: main.html');
$stmt->close();
$conn->close();
but i get an error.If i delete echo "alert('message successfully sent');"; works perfect but i would like to have a message that's successfull.
This will work
echo "<script>alert('message successfully sent');
window.location.href='main.html'
</script>";

Hiding php code from URL

Is there I can hide the PHP information in the URL without reloading the page?
I have the current HTML code:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="get">
<label>
<input pattern=".{3,}" required title="3 characters minimum" name="url" class="urlinput" type="text">
</label>
<input name="submit" class="urlsub" type="submit">
</form>
And when I click submit this happens
http://example.com/?url=example&submit=Submit
Is there any way I can keep it as example.com while the information still passes to php?
Full code:
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label>
<input pattern=".{3,}" required title="3 characters minimum" name="url" class="urlinput" type="text">
</label>
<input name="submit" class="urlsub" type="submit">
</form>
<?php
IF($_POST['submit']) {
include('include/functions.php');
// IP OF THE USER
$ip = $_SERVER['REMOTE_ADDR'];
// URL TO PUT IN Database
$url = $_POST['url'];
// MAKE SIMPLE URL EG: HTTPS://WWW.GOOGLE.COM -> GOOGLE.COM
$surl = surl($url);
// RUN SCRIPT
CIURLE($surl, $ip);
}
?>
Change from using GET method to using POST method
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label>
<input pattern=".{3,}" required title="3 characters minimum" name="url" class="urlinput" type="text">
</label>
<input name="submit" class="urlsub" type="submit">
</form>
<?php
if($_POST['submit']) {
echo 'Posted url: '.$_POST['url'] . '<br>';
echo 'Remote IP:'.$_SERVER['REMOTE_ADDR'] . '<br>';
}
?>
Use "POST" instead of "GET". It's explained in the link.
http://www.w3schools.com/Php/php_forms.asp

how to give name in auto submit form javascript

This is my code that I am using to submit form with post value
<form action="<?php echo DOMAIN; ?>contact/booking-form.php" method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="email" value="<?php echo $email; ?>" />
<input type="submit" name="submit" id="submit" />
<script>document.getElementById('submit').submit();</script>
</form>
Can anybody help me to pass name="submit" value of submit button to another page?
A submit button is only going to be a successful control if it is used to submit the form (and even then only if it has a name and a value … which yours does not).
If you want submit=submit in your form data when you submit the form with JavaScript, then don't use a submit button to put that data in the form in the first place. Use a hidden input.
<input type="submit">
<input type="hidden" name="submit" id="submit" value="submit">
Then you have two other problems.
First, submit is a method of form elements, not inputs. So you need to change your script to call the right element.
<script>document.getElementById('submit').form.submit();</script>
Second, if a form has a control called submit then that will clobber the submit method. So you need to get one from a different form (not supported in old versions of Internet Explorer):
<script>
var form = document.getElementById('submit').form;
var submit_method = document.createElement("form").submit;
submit_method.call(form);
</script>
<form id=submit action="<?php echo DOMAIN; ?>contact/booking-form.php" name="form1" method="post">
<input type="text" name="name" value="<?php echo $name; ?>" />
<input type="text" name="email" value="<?php echo $email; ?>" />
</form>
<script >
document.form1.submit()
</script>
there is no sumit button in your code. first add in html
<input type="submit" value="submit" id="submit"/>
<script>
document.getElementById("submit").value = "newSubmitButtonValue";
</script>

send data to php JSON way from html form

I'm trying to send POST data using JavaScript. I have data in HTML form :
<form name="messageact" action="">
<input name="name" type="text" id="username" size="15" />
<input name="massage" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
after press submit key i want to send name and massage to my PHP file ! a wrote this for my JavaScript in html : post.php , with post method
$(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {text: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
and for my php i wrote this :
if(isset($_SESSION['name'])){
$text = $_POST['text'];
....
....
but in PHP i couldn't receive anything ! please help me to solve this problem ! and how can i change it to JSON way ?
You don't need javascript for send data to server just submit the form and add the destination php in action
<form name="messageact" action="post.php">
<input name="name" type="text" id="username" size="15" />
<input name="massage" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
for testing post.php you could use this code
<?php
if(isset($_POST['name'])){
echo $_POST['name'] . <br />;
echo $_POST['massage'] . <br />;
echo $_POST['submitmsg'] . <br />;
?>
if you want ajax behavior try this
$(document).ready(function(){
//If user submits the form
$("#submitmsg").click(function(){
var clientmsg = $("#usermsg").val();
$.post("post.php", {submitmsg: clientmsg});
$("#usermsg").attr("value", "");
return false;
});
Why do you need to store name in the $_SESSION?
If you have used $_SESSION by mistake change this
if(isset($_SESSION['name'])){
$text = $_POST['text'];
....
....
to this and it should work
if(isset($_POST['text'])){
$text = $_POST['text'];
....
....
The $_POST and $_SESSION are 2 different variables. You might wanna take a look at this and this
Try this:
<form name="messageact" method="post" action="post.php">
<input name="name" type="text" id="username" size="15" />
<input name="message" type="text" id="usermsg" size="63" />
<input name="submitmsg" type="submit" id="submitmsg" value="Send" />
</form>
$("#submitmsg").click(function(e){
var name = $("#username").val();
var usermsg = $("#usermsg").val();
$.post("post.php", {'name': name,'usermsg':usermsg});
return false;
});
i do it for java :
//If user submits the form
$('#edit').submit(function(e){
e.preventDefault();
$.ajax({
type:'POST',
url:'post.php',
dataType:'json',
data:$('#edit').serialize(),
success:function(data) {
console.log("success");
$('#feedback').html(data).fadeIn().delay(3000).fadeOut();
},
error: function( jqXHR,textStatus,errorThrown ){
console.log(textStatus);
}
});
});
and for form :
<form id="edit" action="" method="POST">
<input type="text" name="name" >
<input type="text" name="phone" >
<input type="text" name="address" >
<input type="submit" name="submit" value="Send">

Two actions two submit button in php?

I've form tag like this
sample name:register.php page
<form id="formElem" name="formElem" action="form10.php" method="post">
<input id="pd" name="pd" type="text" AUTOCOMPLETE=OFF />
<input id="pd1" name="fname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="mname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="lname" type="text" AUTOCOMPLETE=OFF />
6 more input boxes
<button name="submit" type="submit">Register</button>
<button name="preview" type="submit">Preview</button>
</form>
I'm sending this info to next form10.php page and displaying all the 10 input values on that page
I'm using $pd= htmlentities($_POST['pd']); $fname= htmlentities($_POST['fname']); to fetch values from form tag and such 10 variables and I'm echoing those entered value
on form10.php file after successful submit button.
like i entered fname, mname, lname came from form tag and displayed on form10.php page.
first name <?echo $fname?>
but now problem is user can see the next page (form10.php) after entering only 10 textboxes values inside form tag.
but I want to give preview option to user so that user can preview that next page either filling any of 1 to 10 textbox values. means he has filled fname and lname but not rest of 8 fields and he clicks on preview button I want to open form10_preview.php which same as form10.php but as user has entered only fname and lname so echo only those values which he as supplied.
Now problem is how can i can have two submit button and two actions in one form?
I think it is better to control form submit rules clientside. Remove the action from your form, and change the button type to be button :
<form id="formElem" name="formElem" action="" method="post">
<input id="pd" name="pd" type="text" AUTOCOMPLETE=OFF />
<input id="pd1" name="fname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="mname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="lname" type="text" AUTOCOMPLETE=OFF />
6 more input boxes
<button id="register" type="button">Register</button>
<button id="preview" type="button">Preview</button>
</form>
Then let javascript control the flow of the submitting :
var formElem = document.getElementById('formElem'),
btnSubmit = document.getElementById('register'),
btnPreview = document.getElementById('preview');
function formSubmit() {
switch (this.id) {
case 'register' :
formElem.action='post10.php';
break;
case 'preview' :
formElem.action='preview10.php';
break;
}
formElem.submit();
}
btnSubmit.onclick = formSubmit;
btnPreview.onclick = formSubmit;
You could have the form point to its own page and handle each submit value separately. At the top of the file with the form, you'll need to start the output buffer and a session. This allows the use of header() to redirect, and storage of session variables.
<?php
ob_start();
session_start();
?>
The form will point to itself by removing the action attribute:
<form id="formElem" name="formElem" method="post">
<input id="pd" name="pd" type="text" AUTOCOMPLETE=OFF />
<input id="pd1" name="fname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="mname" type="text" AUTOCOMPLETE=OFF />
<input id="pd2" name="lname" type="text" AUTOCOMPLETE=OFF />
6 more input boxes
<button name="submit" type="submit">Register</button>
<button name="preview" type="submit">Preview</button>
</form>
We process each of the buttons via their name in the POST array:
<?php
if(isset($_POST['submit'])){
foreach ($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
header("Location: form10.php");
}
if(isset($_POST['preview'])){
foreach ($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
header("Location: form10_preview.php");
}
?>
And at the very end of the file, we flush the output buffer:
<?php ob_end_flush(); ?>
So, essentially the form has one action, which is to submit the values to itself. Finally, both form10.php and form10_preview.php will need session_start(); at the top of the file to access the Session variables we've created, like so:
<?php
session_start();
$inputs = array("pd", "fname", "mname", "lname", etc...);
foreach ($inputs as $input) {
echo $_SESSION[$input];
}
?>

Categories

Resources