How to access text field value on same page without using $_post? - php

I have one text field with search button.
I want to access that text field's value on button click.
I can't use $_post because i have also one submit button inside.
So how to get that textfield's value on search button click.
my below's entire form is based on that unique value which was entered in that textdield
</head>
<script>
var getData = function()
{
var value = $("#inputData").val();
alert(value);
}
</script>
<body>
<form id="form1" name="form1" method="post" action="pag3.php">
<tr>
<td></td>
<td></td>
</tr>
//Other code for form which textfield should fill from data which match with
enterd value.
At end i have put submit button which will send data on next page form perform insertion

You can use jQuery as follow
Html
<input type="button" name="" id="inputData">
<button onclick="return getData()" >Get Data</button>
script
var getData = function()
{
var value = $("#inputData").val();
return false;
}

Related

isset validation in PHP failing when form submitted through jquery

My if(isset) validation is returning false after I have submitted the form through jQuery ,however works fine when done without jquery. Reason I am using jQuery is because I need to submit multiple forms:
Button
<input class="btn btn-primary" type ="submit" id="myButton"
name="create_record" value="Submit 1">
jQuery:
<script>
$(document).ready(function () {
$("#myButton").click(function (event) {
event.preventDefault();
$("#form1").submit();
// $("#form2").submit();
});
});
</script>
PHP
<?php
if(isset($_POST['create_record'])){
$ecode = $_POST['ecode'];
$ename = $_POST['ename'];
$date = $_POST['date'];
$jobRole = $_POST['jobRole'];
}else{
echo "did not receive anything";
}
?>
Always getting "did not receive anything" . Can someone please help.
The submit button value only gets sent if the form is submitted in the traditional way by a button click. Since you are submitting the form via javascript, you'll need to explicitly include the submit button's value or validate your post data in some other way. If you need the value of the specific button that was clicked, something like this should work:
$("#myButton").click(function (event) {
event.preventDefault();
var el = '<input type="hidden" name="' + $(this).prop('name') + '" value="' + $(this).val() + '">';
$("#form1").append(el).submit();
});
As for your objective of submitting multiple forms at once, I believe it's impossible without using ajax as discussed here. If you need guidance on how to do that, better to open a new question.
Your code, isset($_POST['create_record']) maybe false or it didn't receive any values. If your query is only in one PHP file together with your jQuery, you need to check first your algorithm or use var_dump() for testing. Second, If it didn't work, make an alternative solution for it. Do the proper HTML code when using form or make another PHP file for receiving post purpose only.
<form action="directory_to_another_file" method="POST">
<!-- SOME INPUTS HERE -->
<input type="submit" value="Submit 1" name="create_record">
</form>
Try to test all of your codes.
You have to set form method as "POST" type and if you want to receive the form data in same page then empty the "action" key otherwise give the target link.
<?php
if(isset($_POST['create_record'])){
print_r($_POST);
}
?>
<form action="" method="POST" id="form1">
<input type="text" name="create_record" value="Submit 1"/>
</form>
Submit
<script>
$(function(){
$("#myButton").click(function (event) {
event.preventDefault();
$("#form1").submit();
});
})
</script>
Let me know if it's work for you.

passing form post with WYSIWYG

I am trying to pass form values through post form but nothing shows.
using this HTML5 Text Editor http://suyati.github.io/line-control/
I'm using this method for form and php code
<?php
if(isset($_POST['submit'])){
echo $_POST['txtEditor'];
}
?>
html form i am using
<form method="post">
<textarea name="txtEditor" id="txtEditor" ></textarea>
<input name="submit" type="submit" value="Submit">
</form>
and the java script is
<script src="WYSIWYG-Text-Editor/editor.js"></script>
<script>
$(document).ready(function() {
$("#txtEditor").Editor();
});
</script>
Have checked the doc and apparently you need to set the value by yourself, here is a solution:
Add some code to your initialization code:
<script>
$(document).ready(function() {
$("#txtEditor").Editor();
$('form').submit(function () {
$('#txtEditor').val($('#txtEditor').Editor('getText'));
});
$('#txtEditor').Editor('setText', $('#txtEditor').val());
});
</script>
On form submit we actually set the textarea value with what the user input in the WYSISWYG.
In the next line of code we set the value of the WYSISWYG with what value comes in the textarea (as you requested in your comment).

Force data to a form using post

I know that there is no way to use a query string to pass information into a form that uses the post method, but are there any other options? I am trying to use a link (or button) on one website to link to a page on another. In the process, the URL/button needs to send information to the target. The target website uses post, so I can't just use a query string to input my data. Is there some sort of button attribute that will let me send data to the fields in the target website? Thank you in advance.
EDIT: I should have also mentioned that I cannot use the form tag. For some reason, the environment I am using does not allow for some tags to be used.
This should solve your problem (tested it locally and confirmed form data was present) ... got starting point here https://stackoverflow.com/a/13678453/1946558
<button class="btn" id="go">Go </button>
<script type="text/javascript">
$(document).ready(function () {
$('#go').click(function () {
var form = document.createElement("form");
var input = document.createElement("input");
// where you want to post the data
form.action = "test";
form.method = "post";
// add your fields here
input.name = "id";
input.value = 'some data';
// then append
form.appendChild(input);
document.body.appendChild(form);
form.submit();
});
});
</script>
With a button:
<form action="http://example.com/target.php" method="post">
<input type="hidden" name="foo" value="bar" />
<input type="submit" />
</form>
With a link:
<form id="my_form" action="http://example.com/target.php" method="post">
<input type="hidden" name="foo" value="bar" />
</form>
Submit

HTML form submit show javascript confirm window

I have a HTML form:
<form method="post" action="addticketupdate.php">
....
</form>
Then above the form I have:
<?php
if(isset($_POST["submit"])) {
... do stuff here
}
?>
So the submit button in my form has an id and name of "submit"
I want to make a JavaScript confirm box popup when the submit button is clicked, the statement says: If user clicks "OK" the php code excuted, else do nothing.
How can I do that?
Simply use this:
<form method="post" action="addticketupdate.php" onsubmit="return confirm('Do you want to submit the form?');" >
Option 2: (On submit button click)
$('#submit').click(function() {
var res = confirm('Do you really want to submit the form?');
if(!res){
return false;
}else{
//submits form
}
});
You can do as,
<form method="post" action="addticketupdate.php" onsubmit="return confirm('Need to submit??');">
<!-- your other html input code -->
<button type="submit">submit</button>
</form>
<script>
function validate() {
var result = confirm("Do you want to submit!");
return result;
}
</script>
<form name="form-name" onsubmit="return validate();">
That should be making using dialog box instead alert popup box.
However dialog don't have full support so "Fancy Box" is the best in this case.
FancyBox
So after installizing fancy box into your website you should remove the default button they have there and change it to input type submit and add name it as "submit".

input type image URL value pass through jquery

I have a form with number of input type images. I need to replace the webpage location to url of the image when a user clicks any image.
form is like,
<form action='free.php' id='freeForm' method='post' autocomplete="off">
<?
for($j=1;$j<=5;$j++)
{
?>
<input type="image" src="<?=$img;?>" />
<?
}
?>
</form>
$img and $url values from database.
My jquery is like,
$("#freeForm").submit(function()
{
var Form = { };
Form['inputFree'] = $("#inputFree").val();
Form['freeTOS'] = '1';
$('.anchor-url').click(function(e){
e.preventDefault();
alert($(this).attr('href'));
});
$.post('processFree.php', Form, function(data)
{
if(data == "Success")
{
$("#FreeErrors").html('').hide();
swapToPane('paneSuccess');
return;
}
swapToPane('paneFree');
$("#FreeErrors").html(data).show();
});
return false;
});
Now I am getting 'http://au.yahoo.com' when i click each image. But I want to replace 'http://au.yahoo.com' with URL of each image. How can I pass the PHP variable $url to this form?
Any idea?
Thanks!
This should work, if I understood what you were asking for correctly and though I haven't tried it. Basically you bind the click event to each image, and on click it replaces the window location with the value in the src attribute of the input element being clicked on.
$('input[type=image]').click(function(e){
e.preventDefault();
window.location.replace($(this).attr('src'));
});
EDIT
First you should add a class name to your anchor elements:
<form action='free.php' id='freeForm' method='post' autocomplete="off">
<?
for($j=1;$j<=5;$j++)
{
?>
<input type="image" src="<?=$img;?>" />
<?
}
?>
</form>
Your JS:
$('.anchor-url').click(function(e){
e.preventDefault();
window.location.replace($(this).attr('href'));
});
Notice how I added a class to the anchor links. This can be named almost anything. This gives you the ability to select for those elements with that classname only with jQuery using the $('.any-classname') selector.

Categories