I want to submit a Form from home page to sub domain page.
Here is my code
html - home page (main domain)
<table>
<tr>
<td>Name</td>
<td><input type="text" name="txtName" id="txtName" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="txtEmail" id="txtEmail" /></td>
</tr>
<tr>
<td><input name="btnSubmit" id="btnSubmit" value="Submit" type="button"></td>
</tr>
</table>
<form id="getDetails" method="post" action="http://customers.liyyas.com/">
<input type="hidden" name="act" value="Users" />
<input type="hidden" name="hdnName" id="hdnName" />
<input type="hidden" name="hdnEmail" id="hdnEmail" />
</form>
Script
<script type="text/javascript">
$(document).ready(function(){
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("getDetails").submit();
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
});
});
</script>
Sub domain page - user.php
<?php
$act = formatstring($_POST['act']);
switch($act)
{
case "Users":
$Name=$_POST['hdnName'];
$Email=$_POST['hdnEmail'];
print($Name);
exit();
}
?>
In sub domain I am printing the value but its not printing
Is it possible to submit a form from home domain to sub domain?
You need to change the action attribute of the form element from
http://customers.liyyas.com/
to
http://customers.liyyas.com/customers.php
I also assume you know that according to this code
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("getDetails").submit();
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
});
The form will submit BEFORE the values of hdnName and hdnEmail are changed? That may also be a bug for you to quickly switch solve by switching around a few lines. The reason this may be a bug is that when your form submits the page will be reloaded meaning the user will never get to see the new values inserted via JavaScript.
The fix could be
$('#btnSubmit').click(function()
{
alert("hai");
document.getElementById("hdnName").value = $('#txtName').val();
document.getElementById("hdnEmail").value = $('#txtEmail').val();
document.getElementById("getDetails").submit();
});
Related
I would like click on submit and the value input in the field to be stored in database.
However, I do not want to use a form action. Is it possible to do it without creating form action with PHP?
<tr>
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
<?php
if(isset($_POST['submit']))
{
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}?>
</tr>
Use Jquery ajax to do this. Try this:
HTML
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<body>
<table>
<tr>
<td><label for="Item name"><b>Finish Product:</b></label></td>
<td><input id="finish_product" type="text" maxlength="100" style="width:100px" name="finish_product" required></td>
</tr>
</table>
<input type="button" value="Save" id="submit" />
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.ajax({
url: '1.php',
data: {finish_product: $('#finish_product').val()},
success: function (result) {
alert(result)
}
});
});
});
</script>
</body>
PHP
(1.php)
Note: Use mysqli_query since mysql_query is depricated in latest versions. And use bind param instead of directly appending values to query.
<?php
if (!empty($_GET)) {
$SQL = "INSERT INTO bom (finish_product) VALUES ('".$_GET['finish_product:']."')";
$result = mysql_query($SQL);
echo 1;
} else {
echo -1;
}
?>
This isn't a form, this is just a series of table elements with various inputs and a submit button. Clean up the code - there's no closing tr tag, and where is the submit button supposed to be?
Add a form element around it.
You need an method attribute to the form - in this case, "post". Without the action attribute, it will default to the same page.
<!-- add form tag -->
<form method="post">
<tr>
<td>
<label for="Item name"><b>Finish Product:</b></label>
</td>
<td>
<input id="finish_product" type="text" maxlength="100" style="width:100px"name="finish_product" required>
</td>
</tr>
<!-- added new row for submit button - you might want to have the td element span two columns? -->
<tr>
<td>
<input type="submit" value="Save" id="submit" />
</td>
</tr>
</form>
<-- end of form -->
<?php
if(isset($_POST['submit']))
{
//see what is being POSTED - comment out this line when you're happy with the code!
var_dump($_POST); exit;
$SQL = "INSERT INTO bom (finish_product) VALUES ('$finish_product')";
$result = mysql_query($SQL);
}
This is a bit weird but this time I have came across something that works on IE and not on other browsers like firefox and chrome..
Here is the issue:
I am dynamically loading a part of a page, that contains a form, into my existing page with jquery .load() method.
I have used .on method to attach event handler for the newly added elements.
But when I click on submit button the form submit method works fine, but it doesnt send any data in post query ( by the way, I have specified form method=POST")
The main issue is that before I fire the .load() method to obtain new elements and replace the existing ones, the .submit() works FINE. IT SENDS THE POST DATA.
But after the dom is replaced, there is no data in POST.
jQuery Code:
$(document).on("click", ".s_edit",function()
{$(this).parents('tr').children('form').submit();});
.s_edit is the form submit button:
PHP/HTML code (CodeIgniter):
<tr>
<form method="post" action="<?php echo base_url();?>index.php/userlist/inline_edit/<?php echo $r['id'];?>">
<td><input class="record_edit" type="text" name="name" id="name<?php echo $r['id'] ?>"/></td>
<td><input class="record_edit" type="text" name="age" id="age<?php echo $r['id']; ?>"/></td>
<td>
<span class="record_edit">
<input id="gen_m<?php echo $r['id']; ?>" type="radio" name="gender" value="m"/>Male<br/>
<input id="gen_f<?php echo $r['id']; ?>" type="radio" name="gender" value="f"/>Female
</span>
</td>
<td><input class="record_edit datepick" type="text" name="joining_date" id="joining_date<?php echo $r['id']; ?>"/></td>
<td>
<div class="record_edit">
<input type="submit" value="save" class="s_edit"/>
<input type="button" class="cancel_edit" value="Cancel"/>
</div>
</td>
</form>
</tr>
Because you have <tr><form><td>, other browsers are stripping the form element out, or re-arranging it in the DOM tree.
I.E. In Firefox,
<table>
<tr>
<form>
<td>a</td>
</form>
</tr>
</table>
goes to
<table>
<tbody><tr>
<form></form>
<td>a</td>
</tr>
</tbody></table>
Hence, your form is no longer wrapping the elements in the td.
Ok..I replaced the table formatting with div and table display properties in css..firefox and mozilla as Benno suggested was getting rearranged except for IE..still it somehow worked before invoking .load method of jquery..but now its working completely fine..
is it possible to write a PHP page say form.php with a php function generalValidate($form) that is able to perform the following scenario :
user browse to form.php
user gets an html form
user fills form and the form is sent back with POST method to form.php
form.php activate generalValidate($form) where form is the just recived form filled by user
generalValidate($form) returns true if this form is valid (for exemple properly filled), false else.
i think that another way to describe my question will be - is there a general way to iterate over an HTML form sent by a client (perhaps a form that the php page itself sent to client in the first place) while activating some code over each of the form values?
dont eat me if its a bad question, im really just trying to learn :)
a code exemple to fill for your convinience :
<?php
function generalValidate($form) {
...
}
if (!isset($_SESSION))
session_start();
else if (generalValidate(...)) {
}
?>
<html>
<head>
</head>
<div>
<p>Register</p>
</div>
<form id="regfrm" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" align="center">
<table align="center">
<tr>
<td>First Name :</td>
<td><input name="fname" value="<?php if (isset($_POST["fname"])) {echo v($_POST["fname"]);}?>" required></input></td>
</tr>
<tr>
<td>Last Name :</td>
<td><input name="lname" value="<?php if (isset($_POST["lname"])) {echo v($_POST["lname"]);}?>" required></input></td>
</tr>
<tr>
<td>Email :</td>
<td><input id="email" name="email" value="<?php if (isset($_POST["email"])) {echo v($_POST["email"]);} else {echo "xo#xo.xo";}?>" required></input></td>
</tr>
<tr>
<td>Password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
<tr>
<td>Retype password :</td>
<td><input name="pw" type="password" value="e" required></input></td>
</tr>
</table>
<input type="submit" value="Register" ></input>
</form>
</body>
</html>
Yes. Although iterating over the fields gives you a little less clarity and makes it more messy when you want to determine how to validate said field (for example, to know if whether the value should be a name or a number or whatever), but you can do it this way:
In your PHP script you could have something like:
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Determines if the form was sent through the POST method
foreach ($_POST as $fieldName => $formValue) {
// Validate $formValue
}
}
What I think you want to ask is if form data can be manipulated without knowing the form's variable names. I say this because you want to have a general purpose function where the code can be reused for any form. Since this may be any form that currently exists or a form you will create in the future you will not know the name of the variables in the form.
This code captures the form's input. All you have to do now is create a function that does whatever to the $name and $item values as they are looped through.
<?php
foreach($_POST as $name => $item) {
print "name::$name item::$item<br>";
}
?>
<html><title>test</title>
<form method="post">
field one: <input type="text" name="one">
<br>
field two: <input type="text" name="two">
<input type="submit" value="go!">
</form>
</html>
Of course, it is possible to have the page in which the original form resides as the recipient of the form dialog. Through the session variables, but mainly through the contents of the button variables you can determine which state your form is currently in (after having clicked a submit button you will get a $_REQUEST array element with the name of the button holding the value of the button).
Take a look at the answer here.
This is actually a canonical question for receiving form data in PHP. There are lots of ways to do it.
I've made the form below. Is it possible to make it that when user enters the number of fields, for example 6, that the table below has 6 rows. It would be great if it would be possible to make it without any submit button (so that the trigger for this action is exiting from the text input box).
Here is the html code of this form:
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text"><br />
Total number of characters: <input type="text">
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<tr>
<td>1</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
<tr>
<td>2</td>
<td><input type="text"></td>
<td><input type="text"></td>
</tr>
</table>
</fieldset>
If this is not possible (without submit button), than in which way would you accomplish the same result? Thank you for any help and/or suggestions.
PHP is server side, it runs only once, when the page is loading. HTML is not a programming language. You could generate the table with PHP, but only if you had a submit button that reloaded the page. If it has to happen because of a user event, it always needs to be done with Javascript.
That means, you will need Javascript to make this work without reloading the page. Ideally, you would use Jquery (Javascript's most popular plugin) to manipulate the DOM.
If you had this input :
<input id="field" type="text">
You could call the on-leave event like this :
$("p").focusout(function()
{
// Delete the previous table, and create a new one, here
});
As for creating the actual table, it isn't complicated, but it is a bit of work. You should read the following reference to start you up :
http://www.tutorialspoint.com/jquery/jquery-dom.htm
You will need to "install" JQuery before-hand, you can simple insert this at the top of your code :
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
Okay here is the post only script you require
<?php
$rows=2;
if(isset($_POST['submit']))
{
if($_POST['submit']=='Update')
{
if(isset($_POST['rows'])) $rows=max($rows, intval($_POST['rows'])); // minimum 2 rows
}
else
{
// process posted data here
// reset post or jump to another page
$_POST=array();
//header("Location:index.php");
//exit();
}
}
?>
<form method="post">
<fieldset>
<legend>Student Information</legend>
Number of fields: <input type="text" name="rows" value="<?php echo $rows; ?>"><br />
Total number of characters: <input type="text">
<input type="submit" name="submit" value="Update"/>
<br>
<br>
<table border="1">
<th></th>
<th>field</th>
<th>number of characters</th>
<?php
for($loop=1;$loop<=$rows;$loop++)
{
echo '<tr>';
echo '<td>'.$loop.'</td>';
echo '<td><input name="field['.$loop.']" value="'.$_POST['field'][$loop].'" /></td>';
echo '<td><input name="chars['.$loop.']" value="'.$_POST['chars'][$loop].'" /></td>';
echo '</tr>';
}
?>
</table>
<input type="submit" name="submit" value="Submit"/>
</fieldset>
</form>
It will default to 2 rows (minimum), and retain the data when you update the rows.
If the rows get reduced, then the end ones disappear
It certainly would be doable with just PHP.
So for example, if you typed in '6' rows you could catch the form post and do something like (template form for within the HTML):
<?php for($i=0; $<=$_POST['rows'];$i++): ?>
<!-- This being your whatever html for the table -->
<tr><td></td></tr>
<?php endfor; ?>
i have problem in javascript email validation,
I wrote code something like,
//emp.php
<form action="<?php echo $editFormAction; ?>" method="post" name="form2" id="form2" >
<table>
<tr valign="baseline">
<td nowrap="nowrap" align="right">Desired Email-ID:</td>
<td><input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/></td>
</tr>
<tr valign="baseline">
<td nowrap="nowrap" align="right"> </td>
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
</tr>
</table>
</form>
//java scrit code for checkMail()
<script language="javascript">
function checkMail() {
var email=document.getElementById('emp_email').value;
var mail = email.value;
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
if(!reg.test(mail) || mail == "")
{
alert("Your email address isn't valid!");
return false;
}
else {
alert("Email address is okay, let's send the form!");
}
}
</script>
Plz help me thanks in advance
The problem is that there is no element with id
emp_email.
Your element has name emp_email. Add an id attribute to the text box.
<input type="text" name="emp_email" value="" size="32" onsubmit="checkMail()";/>
change it to
<input type="text" name="emp_email" id="emp_email" value="" size="32" onsubmit="checkMail()";/>
Edit:
To put javascript validation on submit button change
<input type="submit" value="Insert record" onclick="checkMail();"/>
to
<input type="submit" value="Insert record" onClick="return checkMail();" />
and remove the onsubmit event from the email textbox.
phoenix has the basic error right. But:
<td><input type="submit" value="Insert record" onclick="checkMail();"/></td>
Don't put form validation on a submit onclick. It may be possible to submit the form without clicking the submit button (depending on the browser and number of fields and buttons in the form). Always put it on the form itself:
<form ... onsubmit="return checkMail();">
also:
var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*#[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i');
That's a really bad idea, like most ad-hoc “e-mail validation” regexes it will reject many completely valid e-mail addresses. Your rules for what usernames and TLDs are allowed are particularly, pointlessly, restrictive.
See this infamous regex for how to actually validate e-mail addresses. Then cry, give up, and just do basic checks instead. (Is there an ‘#’ in it? is there a ‘.’ in it? are there no spaces? well fine then.)
(Even the page-long-horror-regex can't cope with non-ASCII e-email addresses via IDN, which it would be nice to support.)
Try moving the javascript function call off the submit button and onto an onblur event on the email field itself.