if i set
<input type="text" id="myinput" value="<?php echo $origValue; ?>">
and use a function with this
$('#myinput').val('i change the value');
will my php variable value be change like
$origValue="i change the value";
and use that variable to different input like
<input type="text" id="secondText" value="<?php echo $origDate; ?>">
and it will show the same value??
if not, how can i do this??
No - if you change the value on the client side, the original PHP value will not be changed. You seem a little confused about the relationship between client and server side.
The PHP is executed first to generate the HTML response. Once this is complete, the HTML is sent to the client, which then allows JavaScript to work on it. The two (JS & PHP) can never directly affect each other without a request being made back to the server.
Therefore, if you change the val() of the input in JS you would need to post that data back to your PHP page, either using a standard form element or an AJAX request.
Is it really that hard to actually 'try it' and find out yourself? It wouldn't take very long..
No it would not change dynamically. The $origValue is already set when the page loads and cannot be updated with jQuery like that. The variable is set when the page loads and cannot be changed.
Related
I'm using a hidden input tag to pass a variable to a PHP script. This is the tag,
<input type="hidden" name="batch" value="<?php echo $batch; ?>">
The variable $batch is generated on the page upon user input and this variable needs to go a script which is why I have used a hidden input tag.
I feel this isn't very secure as the value left by the variable $batch can be changed using the developer toolbar or firebug.
To make it more clear,
<input type="hidden" name="batch" value="7">
is what I see using firebug. I can change the value of $batch and then submit the form.
Is there a better way to do this?
You can use sessions too. That'd be more secure, in my opinion since it can't be viewed on the client side.
Try this:
$_SESSION['batch'] = $batch;
Once the session is stored, you can access this value in any page you want by writing:
echo $_SESSION['batch']; // Output: 7
I have the following hidden input in a form:
<input type="hidden" name="token" value="<?php echo $token; ?>">
I am posting this input value to another PHP page for processing through a form, however, when I try to read the value of the input field using $_POST["token"], I find it empty. I looked all over the Internet for a solution, but the only one I found is to place everything into one page (the form together with the processing code); but I want the processing code to be in a separate page.
This is the markup:
<form id="registerform" name="registerform" method="post">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
<input type="hidden" name="token" value="<?php echo $token; ?>">
</form>
The problem here is that I can read the values of the first name and last name. but the value of the hidden input is not accessible.
Your approach is correct. If it won't work like this, you probably made some typo or mistake and need to debug your code. Here is one way to do that.
Check if the value $token is correctly rendered into the <input>. Does it have a value in the PHP script or is already empty there? If it is not empty in the PHP script (you can echo it out, to see its value), you need to check the rendered HTML. Does it have any mistakes? It's best to just open the view-source tab to see what PHP actually renders. Somewhere in the rendered HTML, there needs to be a hidden input field with the value set to the same as in the PHP script. Check also the syntax.
If 1 did not yet resolve your problem, you can try to send the form and observe the request in the DevTools of your browser (it's probably best to use Firefox or Chrome for that). To see the request, you should open the DevTools (push F12) and switch to the Network tab. Make sure, you hit Preserve log to keep the logs on the page switch. Click on the row of the page where your data should have been sent to. You can see the parameters on the bottom of the details. Your hidden parameter needs to be there, if not, you are not correctly sending it to the PHP script. → Go back to step 1.
If the error still resists, enter the following line on top of the PHP page where your form is sent to (the page you have in the action attribute of the form): <?php print_r($_POST); ?>. This will print out all data which is received by the PHP script via POST (change it to $_GET, if you are expecting GET data). Submit your form. If the token key is present on the next page (e.g. [token] => "123" is part of the output), you can definitely access it via $_POST['token'].
I have a have in PHP and I have common fields such as 'Name' and 'Surname'.
Now when the user visits the page e.g. http://www.example.com/form.php the form fields 'Name' and 'Surname' are empty.
I would like to now have a link similar to this http://www.example.com/form.php?name=John
so that when the client hits the link the PHP form will now have the name field already filled with 'John' in it.
I know this can be done in HTML but how can I do it in PHP?
Just to let to know I do not own the PHP form - I just want a link from my website to fill the PHP form (which I do not have control over).
Thanks in advance.
Can be done using $_GET
An associative array of variables passed to the current script via the URL parameters.
e.g.:
<? php
if(isset($_GET['name']))
{
$test = $_GET['name'];
}
?>
<html>
<body>
<form>
<input type="text" name="test" value="<?php if(isset($test)){echo "$test";}?>"/>
</form>
</body>
</html>
Note: code isnt tested or anything.. Also, there are possible security risks with getting values from your URL (can be considered user input), so make sure you are aware of that and how to prevent
You could store that value and then when you're about to output the input fields
you just pass along the stored value.
$name = $_GET['name'];
// ... later on
echo '<input type="text" value="'.$name.'"/>';
By using $_GET superglobal
<input name="name" value="<?php echo !empty($_GET['name']) ? $_GET['name'] : '';?>" />
<input name="surname" value="<?php echo !empty($_GET['surname']) ? $_GET['surname'] : '';?>" />
You can use the get method in php to get the name and make use of it
You can retrive this information by the $_GET["name"] function, or $_REQUEST["name"].
Reserver variables
Be carefull with those operations, you might have validation a/o security problem.
Note: if you are not sure that the "name" variable is set or not, you have to use also the
isset function to test it.
You can use the $_GET superglobal, so your input could look like this:
<input type="text" name="name" value="<?php if(isset($_GET['name'])) { echo $_GET['name']; } ?>" />
The $_REQUEST superglobal does a similar thing but I would just use $_GET.
It looks like everyone's answers here assume you are building the form yourself, which doesn't appear to be the case based on your question.
The thing that you want to do may or may not be possible. If the form accepts certain kinds of parameters in certain ways, you may be able to hook in to that functionality and set it up so that when someone clicks a link on your page, that information gets passed to the other page.
One way forms can accept this information is in the form of a "get" request. With this method, values are passed as part of the url, as in your example: http://www.example.com/form.php?name=John. Assuming your page has access to a php variable called $name, you can create a link from your code to build this kind of url like this:
Sign up!
If the page does not accept get parameters in this way (and I have a hard time imagining that they would), you may have to try other techniques to send along the information (assuming that they will even accept it!). The two other ways I imagine you could do this are by passing the value with "post" or creating a cookie for the page. If you tell us what page you are trying to set up this behavior on, we might be able to examine it and give you a better answer.
I'm having a bit of trouble with this one, as basic as it may be. I have a simple form with name, email, comments, etc. that outputs itself to one php page, but I want to have a link that sends it to a second page, for example:
<label for="name">Name:</label><input type="text" name="name" size="20" />
Goes to a second page (second.php) with this code and prints it just fine:
print "<div>Thank you, $name.</p></div>";
But if I try to send $name to a third page (third.php) using similar code it shows up like this:
Thank you, $name.
With the actual variable and not what was stored in $name.
I feel like I'm missing one tiny little thing but I'm not sure what it is. I used this:
$name = $_POST['name'];
To bring it to second.php and this to bring it to third.php:
print 'Click here to proceed.';
Just to see if it would get the same information from second.php, but I don't think it works that way. Is there something else I should be doing on the third page? I have a feeling it's something incredibly insignificant but as I'm learning, I just can't quite get a grasp on it.
You can do it this way.
when you declare
$name = $_POST['name'];
you can use in a header to pass this variable
if(isset($_POST['btnname']))
{
header('Location: second.php?name='.$name);
}
The in your second php
you can get it by this way
Thank you, <php echo $_GET['name']; ?>
Or if you want it to be available to all pages use session
$_SESSION['name'] = $_POST['name'];
=D
you can try it using a hidden input
<label for="name">Name:</label>
<input type="hidden" name="name" value="<? echo $name; ?>" size="20" />
Websites are stateless, which means that the variables only last for a few seconds on the server, then the html is rendered, and sent to the clients browser. The server memory is then freed up to serve other clients.
You have a few options:
1) Using a hidden form field and printing (with php) their name to the hidden form field so when they post again it gets saved (if they post again).
2) Sessions
3) cookies
4) Print it out in the url (i.e. page.php?name=".$name; )
It all depends on how you get to your third page (From a link? A form? A php redirect?)
You can store the name in the second page and again set the $_GET variable before the third page is called. This is because the variables are only valid for that particular request and they need to be set again when another request is made.
I'm newbie in PHP.I want to know that,I taking data by html form and a .php file.
like:
<form id="form1" name="form1" method="post" action="show.php">
<strong>Please Enter the Unique id</strong><br/><br/>
Unique id:
<!-- name of this text field is "tel" -->
<input name="id" type="text" id="id" />
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</html>
Then,I used show.php file to get the 'id'.like:
$id=$_POST['id'];
Is there any way to take input by php code???
Update:
In "C" we take ant input by this way
scanf("%d",a);
is there any way to do so in PHP.I think now all you may be clear what I'm trying to say??
Thanks
Yasir Adnan.
What you are you trying to get is wrong!
HTML:- It is the communicator between the user and the browser. It displays the contents according to the user input or html code.It gets data from user or from html code.
Php :- It is the communicator between server and the browser. It has the capability of collecting from some where else other than the code like mysql data base and then uses html to display the content!
Here you are asking php to do html work which is not correct!!
the html
<input name="sb_id" type="text" id="sb_id" />
php
$id=$_POST['sb_id'];
Well, you do take the input by your php code. Your variable $id took the value of $_POST['id'] which contains the input of the textfield.
After this step you can work with the variable like any other
$id = $_POST["sb_id"]; ?
Remember that $_POST["field_name"] where field_name must be match the name attribute of your <input /> tag.
the id attribute of input tag is not sent to server inside the $_POST array. It`s typically used in client-side.
You can get data in your PHP code through GET and POST parameters. Those parameters are part of the HTTP request.
The GET parameters are in the url :
http://mywebsite.com/id=3&name=test
Then you get them using:
$id = $_GET['id'];
$name = $_GET['name'];
So you can get input data through this way when people visit the URL, call it in AJAX, or call the URL in another application (like a webservice). But no matter how it's called, it's the same for you on the PHP side.
The POST parameters are in the HTTP request, you can't pass them through the URL. You can do that by using an HTML form, or by creating the HTTP request yourself. If you are using Javascript to call your PHP code (and pass data to it), you can use AJAX to do that for example. You, in your PHP code, can get the variables this way:
$id = $_POST['id'];
$name = $_POST['name'];
If you want console-style I/O, you should probably check JavaScript/AJAX. The second one will allow you to write your own wrapper that will help you to process the input by your server "on air".
The problem is, you still need to use $_POST for AJAX. And, which is more important, it's easier (and cheaper for the server) to validate and process input by JS (and to validate and process it further on the server-side after submit).
And if the question is "how can I get the variable from the needed format?", the answer is: try using regexps/parsing the string.
Oh, btw: there IS scanf() in php, and it's called 'sscanf' ('fscanf' for files).