Php send post vars in form - php

if i put this : NO WORKS
<?php
if($_POST['send']=="ok")
{
print $_POST['opt']['nombre']
}
?>
<form action="" method="post" style="margin:0px;">
<input type="text" name="opt['nombre']" value="Hello" />
<input type="hidden" name="send" value="ok">
</form>
If i Put this : WORKS
<?php
if($_POST['send']=="ok")
{
print $_POST['opt']['nombre']
}
?>
<form action="" method="post" style="margin:0px;">
<input type="text" name="opt[nombre]" value="Hello" />
<input type="hidden" name="send" value="ok">
</form>
Why Happend this , the only change it´s in input file this opt['nombre'] by opt[nombre]
I don´t understand why happend this, it´s possible fix this problem because i want put opt['nombre'], and i think it´s the right
When works the result i get it´s "Hello" but only change this symbol inside tags as [''] by []
DIFFERENCES WORKS AND NO WORKS WHEN SEND POST FORM :
SEND FORM AND DON´T GET HELLO
<input type="text" name="opt['nombre']" value="Hello" />
SEND FORM AND GET HELLO
<input type="text" name="opt[nombre]" value="Hello" />
DIFFERENCE PUT INSIDE [] QUOTES AS 2 OR DON´T PUT, THANK´S

var_dump($_POST) and take a look at the keys. You'll see the difference. The single quotes in the HTML name attribute become part of the string key in the PHP array when you do it the first way. You'd have to access it with the single quotes as part of the string to get it.
print $_POST['opt']["'nombre'"];
Or better yet, just do it the second way, so your PHP code won't have to use a silly key like that. :-)

Related

How to send a get form wthout losing get parameters

I have a form located at a url containing get parameters,my form is also using this method.When the form is submitted it rewrites the previos get parameters.
Is there a simple way to rewrite only my form parameters?
I have in mind a Javascript solution ,however I want to know if there is a simpler way?Using HTML/PHP perhaps?
As far as I know, u u are not interested in using JS, then using form's hidden element is only way u have like this-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<input type="hidden" name="country" value="Norway">
<input type="submit" value="Submit">
</form>
<p>Notice that the hidden field above is not shown to a user.</p>
The question is how u can use it with PHP, right?
The solution is here-
//In PHP
if( isset($_GET['fromPerson']) )
{
echo $fromPerson;
}
So combined HTML and PHP code will be like this (assuming a get element from prevous page is named fromPerson)-
<form action="demo_form.asp">
First name: <input type="text" name="fname"><br>
<?php
if( isset($_GET['fromPerson']) )
{
echo '<input type="hidden" name="country" value=".$_POST['fromPerson'].">';
}
?>
<input type="submit" value="Submit">
</form>
Lets say you get a parameter p1 from a get request, it should look like this:
http://server.com/?p1=123
In your form, you can add hidden fields that would have the same effect when you submit, like this:
<form method="GET">
<input type="hidden" value="<?php echo $_GET["p1"]; ?>" name="p1">
</form>
That way you can resend the variables as many times as you need.
I'm not sure I understand your question... Can you post your code?
I assume you mean something like this?
in index.php
<input type="hidden" name="id" value="<?php echo $id; ?>" />
in return.php
Edit

Need to pass a variable in anchor tag

I am very new to php, I am having a problem passing data from textbox to a php variable to use it in an anchor tag. Below is the code i am using.
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
I want to pass value from searchbutton to anchor tag
Hello"
If you are using jquery in your project and want to do this on the front end, you can do:
<a id="someLink" href="http://www.example.com/?q=var" target="_blank">Hello"</a>
$('#someLink').attr('href', "http://www.example.com/?q=" + $('q').val());
With php you'd only be able to set the entered value of q on a post. (meaning when someone submits the form)
i.e.
Hello"
IF you need to populate the link href without a page refresh, you'll need to use javascript, if you want it to be populated after a form post, you can use php.
Be aware though that the link would need to be on the page set in your forms action attribute to populate the link
You should be aware that you take precautions when echoing out form submissions, however the level of questions suggests you've got more to learn before that. (No offense intended)
<form id="searchform" action="fetchvalues.php" method="get">
<input name="q" id="q" type="text" />
<input name="searchbutton" id="go" type="submit" value="" />
</form>
On your fetchvalues.php page
<?php
$val = $_GET['q'];
?>
then
Hello
You can do like this
<?php
if(isset($_REQUEST['searchbutton']))
{
?>
Hello
<?php
}
?>

Get Search keyword in URL

<form action="">
<input placeholder="SEARCH" name="search_input" type="text"/>
<input type="submit" name="search_submit"/>
</form>
If people search by "Keyword Item" I want URL will be http://mydomain.com/search?keywords=Keyword%20Item
How can I do it? I know needs configure in form action, get etc.
Thanks in advance.
Update
When I am trying with this code
<form action="http://search.golfoutletsusa.com/search?" method="get">
<input placeholder="SEARCH" name="Keywords" type="text"/>
<input type="submit" name="search_submit"/>
</form>
The URL is: http://search.golfoutletsusa.com/search?Keywords=85&search_submit=Submit+Query
I just want "&search_submit=Submit+Query" will be removed from URL.
<?php echo $_GET["keywords"]; ?>
You will need to change the name of the text field from search_input to keywords though.
You should also consider using an id attribute along with the name. And as the other answer says, form action and method should be set correctly.
Solution:1
You can add the following code at the top of your search.php (or, whatever the processor file):
<?php
if(isset($_GET["search_submit"]))
{
$keywords = $_GET["Keywords"];
header("Location: search.php?Keywords=$keywords");
}
?>
OR
Solution:2
You can omit the name of submit button if it is not really necessary to give it a name. So instead of
<input type="submit" name="search_submit"/>
just use
<input type="submit" />
Set action of the form to search.php and method to get.
Then change the name of your input element to keywords.
But still the url won't be - http://mydomain.com/search?keywords=Keyword%20Item
It will be - http://mydomain.com/search.php?keywords=Keyword%20Item
Simply put everything in the form properties, only you have to select a file which will receive all this, /search will not suffice:
<form action="search.php" method="get">
<input type="text" name="keywords" placeholder="SEARCH" />
<input type="submit" name="submit" />
</form>
EDIT: In the search.php file you would get the variable contents with the get global variable like this:
$search_query = $_GET['keywords'];
After that you simply write the rest of the code to do the search... Note that this would lead to an URL like http://www.example.com/search.php?keywords=query

Dynamically add to array in PHP?

I am trying to take input from a form, add it to an array, and print_r that array to the screen.
My problem is that the input from the form only replaces the first (and only) element in the array.
<form action="" method="POST">
<input type="text" name="text" />
<input type="submit" name="sub"/>
</form>
<?php
$a = array();
if( isset($_REQUEST['text']) && !empty($_REQUEST['text'])){
array_push($a, $_REQUEST['text']);
print_r($a);
}
?>
One theory of mine is that $a keeps getting re-assigned on the first line of PHP code ($a = array();), but I'm not sure how to fix it. I have looked around, but can't find an answer.
You are correct. The array does get reinitialized each time the form is posted. What you'll want to do is have your array as a more persistent data source.
You might consider using a session and the $_SESSION variable.
session_start();
if (!is_array($_SESSION['a'])){
$_SESSION['a'] = array();
}
$_SESSION['a'][] = $_REQUEST['text'];
You might also consider writing this data to a small text file that you could then read at the start of the script.
Another option would be to write the data to a $_COOKIE.
You are mixing client and server execution...
If you want an array of text you should use something like this:
<form action="" method="POST">
<input type="text" name="text[]" />
<input type="text" name="text[]" />
<input type="text" name="text[]" />
<input type="submit" name="sub"/>
</form>
If you want more entry of text being added you should inject more input with javascript
You are not replacing anything, what you're doing is adding the value of $_REQUEST['text'] to the array, which was empty before.

Can i Mix $_get and $_post?

in order to edit my entries i want to:
<form id="pregunta" name="pregunta" class="form_pregunta" method="post" action="pregunta.php?id=26">
<h2>Titulo de la pregunta</h2><input name="q" id="q" class="q" value="este es mi títiulo " type="text">
<h2>Describe tu pregunta</h2>
<textarea name="texto" id="texto" style="width: 98%;"><p>esta es mi descripcion</p></textarea>
<h2>Etiquetas</h2>
<input name="tags" id="tags" onmouseover="mostrar_tooltip('nube_e','','0','70','')" onmouseout="ocultar_tooltip('nube_e')" value="dos,tres,una,">
<input name="responde_a" style="display: none;" id="responde_a" value="0">
<button name="pregunta" id="pregunta" type="submit">form_edit_question_button</button>
</form>
And then in file.php
i'd like to $_get['id'] and $_post['inputs']
but when i go:
if(isset($_POST['edit_pregunta'])){
echo 'lalalalalalalalalalalalalalala';
post_edit_pregunta();
}
it won't ever enter :S. is that normal or i'm missing something... i wanted not to have a hidden input with the id of the post i want to edit..
I'm not 100% sure, but forms don't send their name when submitted, much less their id.
You could do the following instead:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
<input type="hidden" name="edit_pregunta" value="anything">
... //inputs here
</form>
and your if should now enter.
It looks like you're checking for your form's "id" attribute. This is not sent when the form is submitted, only values in <input>, <select>, <textarea> and <button> are sent.
You should check for one of those.
Edit: Your button name is "pregunta", so that is the POST variable you should be checking for, eg
if(isset($_POST['pregunta'])){
Just to comment in general on mixing params in the form's "action" and inputs, you can mix them as long as the form method is "post". You cannot set GET params in the form's action and use the "get" method
<!-- Good -->
<form action="proc.php?id=123" method="post">
<input name="foo" value="foo">
<input type="submit">
</form>
<!-- Bad -->
<form action="proc.php?id=123" method="get">
<input name="foo" value="foo">
<input type="submit">
</form>
There should be no problem at all with having get and post variables in the same request, but are you sure your syntax is correct? If this is normal php, shouldn't you write
<form id="edit_pregunta" method="post" action="file.php?id=<?php echo $this->id; ?>">
... //inputs here
</form>
[Edit]
The problem is (if I'm correct and this is standard php) that you generate a form that looks something like this:
<form id="edit_pregunta" method="post" action="file.php?id='$this->id'">
... //inputs here
</form>
This will make id look like this: '$this->id' (including the '-signs). When what you want is something like this:
<form id="edit_pregunta" method="post" action="file.php?id=51">
... //inputs here
</form>
Then $_GET['id'] would be 51.
[Edit2]
Also, I think you need to change
if(isset($_POST['edit_pregunta'])){
with
if(isset($_POST['pregunta'])){
If I'm not mistaken the name of a form doesn't get sent to the server, however, the name of the submit-button does, but I might be wrong about that part.
Yes you can, I've done it several times.
Probably something else is wrong with your code.
Is there any control with name="edit_pregunta" or is it just the id of the form? IDs are not sent to the server.
Simply adding the id to the form will not create the $_POST['edit_pregunta'] you verify.
Instead, inside the form tag, add an <input name="foo" />; in the php script verify $_POST['foo']
While the HTTP spec doesn't disallow query parameters in POST methods, it is somewhat unusual. You'd be better off using a hidden input field in the form to pass any non-user values up to the script.
That said, the syntax for your form is wrong. You need to use "echo" to insert the value of $this->id into the action.
Use input type="submit" in place of button tag.
You need name for form submission and activate php script!
HTML Code:
<form action="change.php" method="POST">
<input type="password" name="p1" class="change_text" placeholder="New Password"/></br>
<input type="password" name="p2" class="change_text" placeholder="Re-Password"/></br>
<input type="submit" name="change" value="Change Password" id="change" />
</form>
PHP Code:
<?php
if (isset($_POST['change']) {
$p1=$_POST['p1'];
}
?>

Categories