Forms inside forms HTML - php

I know this question has been asked before, but read through to see why what I'm asking is different.
My first form is like this where newsearch.php is the name of the php file which the form is included in. I want to listen to any POST calls inside this php file since I have my if(isset($_POST code here.
<form action="newsearch.php" method="post">
So, I'm using bootstrap to layout the page, and I have another form I want to include in a <div class="row"> which is inside the form I mentioned before. What I'm asking is, can I add the form html code somewhere else in the php file and call it to appear inside the <div>?
What I want is a way to do this:
<div class="row">
<div class="col-lg-6" align="center">
<h4>Search For Galleries</h4>
<form action="newsearch.php" method="post">
<br>
<input type="text" name="valueToSearch" placeholder="Search"><br><br>
<input type="submit" name="search" value="Search"><br><br>
</div>
<div class="col-lg-6" align="center">
<h4>Create new Gallery</h4>
<!-- HOW CAN I INCLUDE A FORM HERE? -->
</div>
</div>
As you can see, the first form is not closed. That's coz it's closed way below in the file.
My second form is this:
<form action="inserttogal.php" method="post">
Gallery Name: <input type="text" name="gname" /><br><br>
Gallery Type: <input type="text" name="gtype" /><br><br>
<input type="submit" name="newgal"/>
</form>

Instead of using two <form> constructs (and nested, to boot) why not:
(1) Change the forms to ordinary DIVs
(2) Use jQuery to trap a button click
(3) Read the values into variables (this allows you to do any required field validation without negating the form's default action)
(4) Using javascript/jQuery, construct a composite HTML <form> (combining the two forms) in a variable, then
(5) Use jQuery to append() the form to the bottom of the page, and use
(6) $('form').submit() to submit the composite form.
A bit more work, but it will work. Note that Bootstrap uses/requires jQuery, so the library is already loaded. Might as well use it.
Code example:
Obviously, this example is not appropriate for your application, just showing you what you can do and how to get around your <form> problem, via jQuery/javascript.
$('#btnOne').click(function(){
var fn = $('#fn').val();
var ln = $('#ln').val();
var ag = $('#age').val();
if (ln==''){
alert('Please complete all fields');
return false;
}
var myFrm = '\
<form id="myForm" action="postFile.php" method="post">\
<input name="fname" value="' +fn+ '" />\
<input name="lname" value="' +ln+ '" />\
<input name="age" value="' +ag+ '" />\
</form>
';
$('body').append(myFrm);
$('#myForm').submit();
});
#frmOne{}
#frmTwo{background:wheat;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div id="frmOne">
First Name: <input id="fn" type="text" /><br>
Last Name: <input id="ln" type="text" /><br>
<div id="frmTwo">
Age: <input id="age" type="text" />
</div>
</div>
<button id="btnOne">Form One Clicker</button>
You should also look into AJAX (what it is, why use it -- it's quite simple!) and see if it could be of use. This post contains a link to another post with some simple examples that you should study. I included both posts because each has something you might find informative. Twenty minutes of poring of these examples could save you days of design frustration.

No form can having another form inside, I tried before.
Suggest apply Ajax or XMLHttpRequest (XHR)
<div class="row">
<div class="col-lg-6" align="center">
<h4>Search For Galleries</h4>
<form action="newsearch.php" method="post">
<br>
<input type="text" name="valueToSearch" placeholder="Search"><br><br>
<input type="submit" name="search" value="Search"><br><br>
</div>
<div class="col-lg-6" align="center">
<h4>Create new Gallery</h4>
<a href="#" data-toggle="modal" data-target="#new"><!--bootstrap modal-->
</div>
</form>
<form name="new_form" id="new_form" method="post" action="">
<div class="modal fade" id="new" tabindex="-1" data-backdrop="static" data-keyboard="false" role="dialog">
<div class="modal-dialog" role="document" >
<div class="modal-content">
<div class="modal-body">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span style="font-size:21px;margin:-15px -10px 0 0;display:inline-block" aria-hidden="true">×</span></button>
<h3 style="">create gallery</h3>
<input type="button" name="create" id="create" class="btn btn-default" value="Create Gallery" />
</div>
</div>
</div>
</div>
</form>
</div>
<script type="text/javascript">
$(function(){
$("#create").click(function(){
$.ajax({
type: "POST",
url: "new.php",
data: { data:data },
success:
function(){
alert('success');
},
error:
function(){
alert('Error!');
}
});
});
});
</script>

Related

Ajax Jquery Not Works On Html Form Data Insert To The Server [duplicate]

I've got a form, with 2 buttons
<button>Cancel changes</button>
<button type="submit">Submit</button>
I use jQuery UI's button on them too, simply like this
$('button').button();
However, the first button also submits the form. I would have thought that if it didn't have the type="submit", it wouldn't.
Obviously I could do this
$('button[type!=submit]').click(function(event) { event.stopPropagation(); });
But is there a way I can stop that back button from submitting the form without JavaScript intervention?
To be honest, I used a button only so I could style it with jQuery UI. I tried calling button() on the link and it didn't work as expected (looked quite ugly!).
The default value for the type attribute of button elements is "submit". Set it to type="button" to produce a button that doesn't submit the form.
<button type="button">Submit</button>
In the words of the HTML Standard: "Does nothing."
The button element has a default type of submit.
You can make it do nothing by setting a type of button:
<button type="button">Cancel changes</button>
Just use good old HTML:
<input type="button" value="Submit" />
Wrap it as the subject of a link, if you so desire:
<input type="button" value="Submit" />
Or if you decide you want javascript to provide some other functionality:
<input type="button" value="Cancel" onclick="javascript: someFunctionThatCouldIncludeRedirect();"/>
Yes, you can make a button not submit a form by adding an attribute of type of value button:
<button type="button"><button>
<form onsubmit="return false;">
...
</form>
Honestly, I like the other answers. Easy and no need to get into JS. But I noticed that you were asking about jQuery. So for the sake of completeness, in jQuery if you return false with the .click() handler, it will negate the default action of the widget.
See here for an example (and more goodies, too). Here's the documentation, too.
in a nutshell, with your sample code, do this:
<script type="text/javascript">
$('button[type!=submit]').click(function(){
// code to cancel changes
return false;
});
</script>
<button>Cancel changes</button>
<button type="submit">Submit</button>
As an added benefit, with this, you can get rid of the anchor tag and just use the button.
Without setting the type attribute, you could also return false from your OnClick handler, and declare the onclick attribute as onclick="return onBtnClick(event)".
<form class="form-horizontal" method="post">
<div class="control-group">
<input type="text" name="subject_code" id="inputEmail" placeholder="Subject Code">
</div>
<div class="control-group">
<input type="text" class="span8" name="title" id="inputPassword" placeholder="Subject Title" required>
</div>
<div class="control-group">
<input type="text" class="span1" name="unit" id="inputPassword" required>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Semester</label>
<div class="controls">
<select name="semester">
<option></option>
<option>1st</option>
<option>2nd</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Deskripsi</label>
<div class="controls">
<textarea name="description" id="ckeditor_full"></textarea>
<script>CKEDITOR.replace('ckeditor_full');</script>
</div>
</div>
<div class="control-group">
<div class="controls">
<button name="save" type="submit" class="btn btn-info"><i class="icon-save"></i> Simpan</button>
</div>
</div>
</form>
<?php
if (isset($_POST['save'])){
$subject_code = $_POST['subject_code'];
$title = $_POST['title'];
$unit = $_POST['unit'];
$description = $_POST['description'];
$semester = $_POST['semester'];
$query = mysql_query("select * from subject where subject_code = '$subject_code' ")or die(mysql_error());
$count = mysql_num_rows($query);
if ($count > 0){ ?>
<script>
alert('Data Sudah Ada');
</script>
<?php
}else{
mysql_query("insert into subject (subject_code,subject_title,description,unit,semester) values('$subject_code','$title','$description','$unit','$semester')")or die(mysql_error());
mysql_query("insert into activity_log (date,username,action) values(NOW(),'$user_username','Add Subject $subject_code')")or die(mysql_error());
?>
<script>
window.location = "subjects.php";
</script>
<?php
}
}
?>

search form action url not display php variable

hi can anyone tell me whats problem in this form . its not show varible in url
<form class="navbar-form navbar-left" method="post" action="test.php?q=<?php echo $searchb;?>" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
php code here
if (isset($_POST['searchb'])) {
$searchb = $_POST['searchb'];
}
when something input in form and action url not show any value
test.php?q=
but we echo variable its show value .
First time that form loaded $_POST['searchb'] is empty so action is equal test.php?q= after load form when you submit form then $_POST['searchb'] to be filled
The Part: action="test.php?q=<?php echo $searchb;?>" is first illogical and most importantly unnecessary since you are POSTing your form. It would have been valid if $searchb was pre-defined. However, since it is a part of the Form; it will always be NULL since it was never declared but expected to be dynamically added on Form-Submit, which wouldn't happen. You do it in one of the 2 ways:
OPTION #1 - PASSING q VIA HIDDEN INPUT:
<!-- YOU DON'T NEED THE echo $searchb PART IN YOUR FORM'S ACTION BECAUSE -->
<!-- THAT VALUE IS NOT PART OF THE ACTION AS IT IS NOT EVEN SET AT ALL -->
<form class="navbar-form navbar-left" method="post" action="test.php" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
<!-- ADD THE q AS HIDDEN INPUT ELEMENT WITH A VALUE -->
<input type="HIDDEN" name="q" value="Some value" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
if (isset($_POST['searchb'])) {
$searchb = $_POST['searchb'];
}
OPTION #2: USING GET & SETTING Q TO A PRE-DEFINED VALUE
<?php $param = "some-predefined-value"; ?>
<form class="navbar-form navbar-left" method="GET" action="test.php?<?php echo $param;?>" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<input type="text" name="searchb" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
// BUT REMEMBER TO CHECK INSIDE THE `GET` GLOBAL
if (isset($_GET['searchb'])) {
$searchb = $_GET['searchb'];
}
BETTER OPTION FOR YOUR USE-CASE: USING GET & SETTING Q FROM THE INPUT
<!-- STILL NO NEED FOR SETTING QUERY PARAMETERS MANUALLY-->
<!-- THE GET METHOD WOULD TAKE CARE OF THAT FOR YOU ONCE THE FORM IS SUBMITTED -->
<form class="navbar-form navbar-left" method="GET" action="test.php" role="search" style="padding: 3.5px 90px;">
<div class="form-group">
<!-- NOTICE THAT THE NAME OF THE INPUT FIELD CHANGED TO; q HERE -->
<input type="text" name="q" class="form-control" autocomplete="off" placeholder="Search" />
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
<?php
// INSIDE OF test.php SCRIPT; DO;
// BUT REMEMBER TO CHECK INSIDE THE `GET` GLOBAL
if (isset($_GET['q'])) {
$searchb = $_GET['q'];
}

How to return php code into html body

I am building a simple form script that collects a users email and returns a PIN.
The input sits in standard HTML below:
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
I have the following if statement that checks the database to see if the email already exists, and if the user would like a PIN reminder.
if($num_rows != 0) {//if email found in table
?>
Email already registered, would you like a PIN reminder?
<form method="POST" action="">
<input type="submit" name="srSend" value="Click here to get pin reminder" />
<input type="hidden" name="srEmail" value="<?php echo strtolower($email);?>" />
</form>
<?php
exit;
}
At the moment, this returns the result to the user as a new page; how do I put this in the actual HTML of the body page, so it would actually appear below the original form input in a new <p> element?
This is a piece of cake with jquery.post
include the jquery library in your html head and you'll need a short script to get the php content by ajax
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
$('#yourForm').submit(function(e){
e.preventDefault();
var data=$('#yourForm').serialize();
$.post('yourphp.php',data,function(html){
$(body).append(html);
});
});
});
</script>
</head>
<body>
<p>
<form id="yourForm" class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
</body>
You could able to achieve that without ajax too. Simply use a hidden iframe in your main page, then set the target attribute of your form to the iframe as follows:
<form method="post" action="page.php" target="myIframe">
.....
</form>
<p id="theResultP"></p>
<iframe src="#" name="myIframe" style="visibility: hidden"></iframe>
The question now, How could you make the page loaded in the iframe "page.php" to interact with the opener page to update the p. This may be done as follow:
//page.php
<script>
result = parent.document.getElementById('theResultP');
result.innerHtml = "<b>The message you want</b>"
</script>
I dont know if I get what you asking for,but this is my solution :
<p>
<form class="form-inline" role="form" method="POST">
<div class="form-group">
<label class="sr-only" for="srEmail">Email address</label>
<input type="email" name="srEmail" class="form-control input-lg" id="srEmail" placeholder="Enter email">
</div>
<button type="submit" name="srSubmit" class="btn btn-default btn-lg">Generate PIN</button>
</form>
</p>
<?php
if (isset($message)){
<p><?php print $message ?></p>
?>
<?php
}
?>
and in top of your file write something like this :
<?php
if($_post['srSend']){
$message='write your message here';
}
?>

Zend get value from textbox and add it into search url

I have question,this is the following code.
<form action="" method="post" id="search">
<div class="row collapse">
<div class="ten columns mobile-three">
<input type="text" size="25" name="search" value="<?php echo $this->search ?>"/>
</div>
<div class="two columns mobile-three">
<input type="submit" name="/search/index/keyword/" value="Search"; ?>" class="button expand postfix" id="search_button"/>
</div>
</div>
</form>
when I click the submit button,the url should be
from id/ to id/search/index/keyword/(here's the search key)
HTML
<form action="" method="post" id="search" name="search" onsubmit="submitForm();">
<div class="row collapse">
<div class="ten columns mobile-three">
<input type="text" size="25" name="search" id="search_value" value="<?php echo $this->search ?>"/>
</div>
<div class="two columns mobile-three">
<input type="hidden" value="/search/index/keyword/" id="search_url"/>
<input type="submit" name="search" class="button expand postfix" id="search_button"/>
</div>
</div>
</form>
JS
<script>
function submitForm()
{
var search_button = document.getElementById('search_url').value;
var search_text = document.getElementById('search_value').value;
document.searchForm.action = search_button + '/' + search_text;
document.searchForm.submit();
return false;
}
</script>
PHP
<?php
if (isset($_POST['search'])) {
// add action code here
}
?>
I'd use a server-side language for that. Make a process page (unless you're familiar with AJAX), get the values by $_POST and make the process page redirect.
I'm pretty sure that it's possible using JavaScript as well, but I'm really bad at it so don't count on my solution.
You can use JS to accomplish your goal.
1) make a function that runs when user clicks submit button.
2) function grabs the word from search box.
3) function changes the "action" attribute on the form element
4) form is submitted

Form Submit without Refreshing using Plugin (malsup)

I just created a form submit without refreshing using this plugin : http://malsup.github.com/jquery.form.js
The simple example can be seen at
http://jquery.malsup.com/form/
I don't what I was doing, it was worked fine before until I modified something that I don't remember.
When I click SAVE, it should be processed on the background and it should stay on the same page. But now, it doesn't and it redirect me to the action page (process-001.php)
I created another page using the same method, and it works just fine.
Can you see if I'm doing it wrong?
Here is the form :
<div id="submit-form">
<form action="web-block/forms/process-001.php" id="select-block" class="general-form" method="post" enctype="multipart/form-data">
<div class="input-wrap">
<input class="clearme" name="Headline" value="<?php echo $Headline;?>" id="headline"/>
</div>
<div class="input-wrap">
<textarea class="clearme" name="Sub-Headline" id="subheadline"><?php echo $SubHeadline ;?></textarea>
</div>
<label>Bottom Left Image</label>
<div class="up-mask">
<span class="file-wrapper">
<input type="file" name="Pics" class="photo" id="pics" />
<span class="button">
<span class="default-txt">Upload Photo</span>
</span>
</span>
</div><!-- .up-mask -->
<input type="hidden" name="Key" id="Key" value="<?php echo $Key;?>"/>
<input type="submit" class="submit-btn" value="SAVE" />
<span class="save-notice">Your changed has been saved!</span>
</form>
</div>
The Javascript :
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
// bind 'myForm' and provide a simple callback function
$('#select-block').ajaxForm(function() {
alert("Thank you for your comment!");
});
});
</script>

Categories