Extracting data with PHP - javascript

Helo friends, my code:
<form>
<input type="text" placeholder="name">
<input type="text" placeholder="email">
<input type="password" placeholder="pw">
<button type="button" id="insert">Insert</button>
</form>
My JavaScript file:
$('#CadAdmin').click(function(){
$('input').each(function(){
$.post('/require/jp/insert.php',{
dataUser:$(this).val()
},function(res){
alert(res);
})
})
});
My insert.php file:
extract($_POST);
print dataUser;
Here, its show all data, ok but, I want to get separete data, for ex:
//print dataUser
print $name.' '.$email.' '.$pw;

The $_POST variable is an array (see $_POST on php.net). This means you can access the inner content like this $_POST['index'].
For you this would mean that you have to use the form input names with $_POST. This gives you $_POST['name'], $_POST['email'] and $_POST['pw']. These variables contain the input from the form.
Note that just printing/echoing these variables to your website you might have some problems with XSS, you can check this answer or this one (both from other questions) for on how to prevent something like that from happening.
EDIT (after comment):
I suggest you to change (or even remove) your javascript code. Because you are using $('input').each() you are currently sending 3 separate POST requests to /require/jp/insert.php, this means the data is handled separately. If you were to change that, your inputs do not have a name, that is why extract() doesn't work, the data within $_POSTisn't set to a specific 'variable' to use after extract().
I suggest you change your code to something like this:
(somehtmlpage.html?):
<form method="POST" action="/require/jp/insert.php">
<input type="text" name="name" placeholder="name">
<input type="text" name="email" placeholder="email">
<input type="password" name="pw" placeholder="pw">
<input type="submit" id="insert">Insert</input>
</form>
(insert.php):
extract($_POST);
print $name.' '.$email.' '.$pw;
What this does is exact the same, without javascript (and the 3 POSTs). Since your data is now within the 'same' $_POST you can use the names (from the input fields, 'name=....') after extraction.

You need to define the name (not the placeholder) of each input, then serialize the form data before posting it to your insert.php script.

First, add name attribute to your elements to catch their values after form submit:
<input type="text" placeholder="name" name="name">
then you can get those values like this:
$name = $_POST['name'];

Related

Send text field value using href to php

I have to send text field value using href to php is something like below. But it is not correct way. Can anyone please give me any solution?
<input type="text" id="myText" value="Mickey">
<a href="test.php?id=javascript:document.getElementById('myText').value;">
<input type="button" value="Click"></a>
Put content inside a form. You can also change the button type input to a submit type, this way the form is sent automatically on click.
<form method="POST" action="yourURL.php">
<input type="text" id="myText" name="myElement" value="Mickey">
<a href="test.php?id=javascript:document.getElementById('myText').value;">
<input type="submit" value="Click"></a>
</form>
More information on forms: MDN
Whether you use GET or POST as a method, you'll be able to access the content of the form through PHP variables: $_GET, $_POST or the generic $_REQUEST.
More information in the PHP documentation
Note: PHP uses the name attribute of your HTML elements for those variables. Make sure to add this attribute to your HTML elements otherwise you'll have a hard time getting a value from $_REQUEST['myText']. I added the attribute holding the value "myElement" in the above code. It is accessible through PHP by typing $_REQUEST['myElement'].
Content sent through GET method is visible in the URL,
like this: www.example.com/test.php?var1=test&var2=test
<input type="text" id="myText" value="Mickey">
test

convert form get method url in php

How to change the get method url in php. Here is my code
<form method="get" action="offers.php">
Name :<input type="text" name="name"><br>
Subject : <input type="text" name="sub"><br>
<input type="submit" value="Submit">
</form>
and in offers.php file code
<?php
$name = $_REQUEST['name'];
$subject = $_REQUEST['sub'];
?>
After giving the input values shows url "http://localhost/htaccess/offers.php?name=raj&sub=kumar" but i want to show the url is http://localhost/htaccess/raj/kumar. How to solve this issue. how to convert this url. Here raj and kumar are input given values not for default.
Hmm, you have a couple of things going on. First, to get rid of the ?name=raj&sub=kumar string on your URL
Use POST not GET
<form method="POST" action="offers.php">
Name :<input type="text" name="name"><br>
Subject : <input type="text" name="sub"><br>
<input type="submit" value="Submit">
</form>
Next, you have a whole other world of questions in using rewrite rules to make offers.php change to raj/kumar. Try a few of these links to get you there:
Reference: mod_rewrite, URL rewriting and "pretty links" explained
Pretty URLs with .htaccess
How to write htaccess rewrite rule for seo friendly url
The basic premise is this:
offers.php will still be requested until you change your form to look like this:
<form method="POST" action="raj/kumar">
Name :<input type="text" name="name"><br>
Subject : <input type="text" name="sub"><br>
<input type="submit" value="Submit">
</form>
but that won't work until you htaccess and mod_rewrite working for you to "rewrite" that raj/kumar url to be "handled" by offers.php
Change get to post.
Change $_REQUEST to $_POST
Also, put the $_POST data within a conditional, like:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$name = $_POST['name'];
$subject = $_POST['sub'];
} else {
// form was not posted
}

Show a different value from an input that what will be received as php

I'd like to know if there is a way to show in an input of type text a different value than the one send to PHP.
For example, let say you have :
<input type="text" value="John"> that display John.
Can I change the value to "Doe" for the user but keep it to "John" for php?
Can I achieve this using the difference between $.attr('value') and $.val()?
I ran a couple of tests and it seems that I will have to reverse it directly in my controller. Is there an other solution?
Here is a little jSFiddle to play around.
An odd request to be sure but...
You can't change the field's value and just do a simple form submission. The field will dutifully send whatever is in it. There's a few hackery ways around this tho
Option 1 - Hidden fields
So make a field, disable it, and add a hidden field. Disabled fields are never successful, although the user will be unable to change the field value and many browsers will change the styling of the field automatically
<input type="text" name="name" value="John" disabled>
<input type="hidden" name="name" value="Doe">
Option 2 - Change the value on submit
As you mentioned, you can always change the value when the form is submitted. The below listener will capture the form submitt Using jQuery since you asked it that way
$("form").on( "submit", function( event ) {
event.preventDefault();
$('input[name="name"]').val('Doe');
$("form").submit();
});
If you're open to a (pure) PHP solution, sure you can.
Sidenote: It's best to have a serverside method (as a Plan-B), should the end-user disable Javascript.
Given the user will replace the "already shown" value in the input though.
The input needs to hold a name attribute in order for this method to work.
<?php
if(!empty($_POST['name'])){
echo $name = $_POST['name']; // Will echo what was replaced in the input
}
else{
echo $name = $_POST['name']; // Will echo the value that was already set in the input
}
?>
<form method="post">
<input type="text" value="John" name="name">
<input type="submit">
</form>
Note: echo $var = "something"; is valid syntax.
Edit:
It's unclear as to what you want to achieve here. However you can use a hidden attribute and remove value="John" from the input.
The user will need to fill the input.
The following will only output "Doe" if the input was left blank, yet output both if it is filled.
<?php
if(!empty($_POST['name'])){
echo $name = $_POST['name'];
}
else{
echo $name = $_POST['name'];
}
if(isset($_POST['hidden_name_value'])){
echo $hidden = $_POST['hidden_name_value'];
}
?>
<form method="post">
<input type="text" name="name">
<input type="hidden" name="hidden_name_value" value="Doe">
<input type="submit">
</form>
1. you shall add a variable name like
<input type="text" name='John' value="Doe">
in PHP a var_dump('John') will result in 'Doe'
but obviously this is not very clever way, especially in case you have more then one input field, so I suggest
2. using an array like
<input type="text" name='person[John]' value="Doe">
<input type="text" name='person[Fred]' value="Flintstone">
in PHP you can selectively test like
if( isset($_POST['person']['John']) ) {
... do something ...
}elseif( isset($_POST['person']['Fred']) ) {
... do something different...
}
Why not use a data-value attribute for the value you want to use in the PHP?
$("form").on( "submit", function( event ) {
event.preventDefault();
$('input[name="name"]').val($(event).data('value'));
$("form").submit();
});
Something like that?

Can input type = hidden be used to send information to a server?

Say I have
<input type="hidden" name="Content" id="Content_m">
Can I send a specific text to the server ? If so, is it done by adding a value field to form field?
Yes, you can:
<input type="hidden" name="Content" id="Content_m" value="your_text">
yes. to send info use value field like so:
<input type="hidden" value="iwanttosendthis" name="Content" id="Content_m">
Hidden inputs are submitted with the form as normal inputs.
You can retrieve whatever value the hidden input contained from (for example) $_POST in PHP:
$myVar = $_POST["Content"]

How to take HTML form data and write to XML using JQuery?

I have a form that looks like this:
<form method="post" id="aodform">
<label for="a">Animal:</label>
<input class="input" type="text" name="a"/>
<label for="s">Sausage:</label>
<input class="input" type="text" name="s"/>
<label for="g">Bird:</label>
<input class="input" type="text" name="g"/>
<label for="d">Dessert:</label>
<input class="input" type="text" name="d"/>
<input id="submitter" type="submit"/>
</form>
I need to take the entered values in the form and overwrite (replace) the corresponding node's text value in an EXISTING XML file that looks like this:
<aod>
<animal>x</animal>
<sausage>x</sausage>
<bird>x</bird>
<dessert>x</dessert>
</aod>
Now, I know I can use
$("#aodform").submit();
to achieve a form submit in Jquery, but after this I'm lost! I'm trying to figure out how to get those form values, store them as variables in a function that then writes to the XML file (all in Jquery).
I've searched all over the google box and found similar subjects, but not quite similar enough to help my situation. Would someone please help me? Thank you!
P.S. I CAN NOT use any server-side language, like PHP, or else I would.
You won't be able to create or edit a local XML file using javascript/jquery because of security concerns. Imagine going to a website and the webmaster wrote some code doing who knows what and puts this in a file on your computer...
The only way to write to a local file using javascript is to use cookies or HTML5 localStorage.
localStorage will allow you to store string keys and values of arrays and/or property names and values of objects.
If you need an XML file you will have to call a server-side script that has permission to write the file on the server, which you can then access via it's url.
jQuery and Javascript on the client-side cannot persist to the server side. It has no access to write to code. You will have to gain access to a server-side language, or make use of some cloud-based services (you could, for example, persist to Amazon 3S, or a MongoDB or something of that nature, through Javascript API calls, on a cloud service).
While you could use Javascript/jQuery to construct an XML object in the client, you would then have to submit that to some server-side script to save it to a file.
Sorry to be the bearer of bad news, but you will need to get into something a bit more full-featured, if you want to be able to get this done.
You have to send Labels within hidden inputs, etc:
<form method="post" id="aodform">
<label for="a">Animal:</label>
<input type="hidden" name="a_label" value="Animal"/>
<input class="input" type="text" name="a"/>
<label for="s">Sausage:</label>
<input type="hidden" name="s_label" value="sausage"/>
<input class="input" type="text" name="s"/>
<label for="g">Bird:</label>
<input type="hidden" name="g_label" value="bird"/>
<input class="input" type="text" name="g"/>
<label for="d">Dessert:</label>
<input type="hidden" name="d_label" value="dessert"/>
<input class="input" type="text" name="d"/>
<input id="submitter" type="submit"/>
</form>
and then in PHP you access pairs:
$name = "a"; // "s", "g", "d"
$tag = $_POST[$name.'_label'];
$value = $_POST[$name];
$xml_element = "<$tag>$value</$tag>";
OR
use same name as label and then use:
foreach($_POST as $key => $value)
{
$xml_element = "<$key>$value</$key>";
...
}

Categories

Resources