Pass Value from HTML Form to PHP Without Submitting the Form

Pass value from html form to php without submitting the form

Yes, it is called AJAX. : )

In jQuery, for brevity:

// or $('#textbox_autopost').blur if you want to do it when the box loses focus
$('#textbox_autopost').change(function(){
$.ajax({
type: "POST",
url: "some.php",
data: {text:$(this).val()}
});
});

if you want to do it via button click

<button id="inlinesubmit_button" type="button">submit</submit>

$('#inlinesubmit_button').click(function(){
$.ajax({
type: "POST",
url: "some.php",
data: {text:$('#textbox').val()}
});
});

You can also do it through an A HREF (or submit button, or or something else wacky:

<!-- backup if JS not available -->
<a href="handler.php" id="inline_submit_a">add comment</a>

$('#inline_submit_a').click(function(evt){
$.ajax({
type: "POST",
url: "some.php",
data: {text:$('#textbox').val()}
});
evt.preventDefault();
return false;
});

If you want to do it on enter:

$('#textbox_autopost_onenter').keydown(function(evt){
if ((evt.keyCode) && (evt.keyCode == 13))
{
$.ajax({
type: "POST",
url: "some.php",
data: {text:$(this).val()}
});
evt.preventDefault();
return false;
}
});

Final, site-ready code:

$(document).ready(function(){
function submitMe(selector)
{
$.ajax({
type: "POST",
url: "some.php",
data: {text:$(selector).val()}
});

}
$('#textbox_button').click(function(){
submitMe('#textbox');
});
$('#textbox').keydown(function(evt){
if ((evt.keyCode) &&(evt.keyCode == 13))
{
submitMe('#textbox');
evt.preventDefault();
return false;
}
});

How can i pass the text field value to php variable without submiting form?

It's fundamentally incorrect requirement. HTML is all client side code whereas the PHP variable is a memory allocated on the server side. You must submit/pass the value to server side using Ajax, JQuery, API call to get the value from textfield to PHP variable on server side.

PHP variable can't be accessed from HTML/JS directly. You must hit the server side code to set the value.

can I get text from html and put it into variable in php without submitting a form?

HTML:

do you have more then 1 image on page? its better if you add ID in image. No need for form and hidden fields for what you want done.

make sure your img has ID like <img id="imageID"...

JavaScript:

var pName= null;
$(document).ready(function(){
$('#imageID').click(function(){
pName= $(this).attr("name");
$.post("project.php", { pNameChange: pName },
function(data) {
// do something here.
});
});
});

above code should work as expected. Now in project.php > $_POST['pNameChange'] should receive the value of pName (image's name attr).

I don't understand what you want when you said $pName available globally on all pages. Please elaborate further, may be look into storing it as cookie/session?


EDIT:

Consider using session to pName value... by simple starting/resuming session in start of file:

<?PHP
session_start();

and then...

to set/update value:

if(isset($_POST["pNameChange"]))
$_SESSION["pName"] = $_POST["pNameChange"];

and then use $_SESSION["pName"] instead of $pName on all pages.

How to pass data from form without a form field? (PHP)

I believe you are looking for

 <input type='hidden' name='username' value='theusername' />

hidden - can only be seen in the source of your HTML document

name - where it will be in the $_REQUEST/$_POST/$_GET ($_POST or $_GET depending on how you are submitting your form) variable on submit

value - the username you want this form to relate to



PRO TIP: Have a way to tell who is trying to update users so you don't have unauthorized people updating your user information. It would be very easy for someone to change the username in the form and try to update someone else.



Related Topics



Leave a reply



Submit