PHP echo into form action - php

Have searched around but not found an answer to this. Might be an indicator that this is a silly question, but:
I'm trying to echo some information from the page URL into a form action so that information is carried to the next page. The form and PHP looks like this:
<form method="link" action="surveymaker.php?title=<?php echo $_GET['title'];?>">
The information that the user enters into the form gets added to the url on the next page, but not the "title" that I'm trying to add by echo. I'm fully prepared to accept that I'm going at this the totally wrong way if that's how it is. Can anyone point me in the right direction?
Thanks

You can use a hidden input field to do this. Just add this to your form:
<input type="hidden" name="title" value="<?php echo htmlspecialchars($_GET['title']);?>">
Note the use of htmlspecialchars to sanitize the value from the URL.

This is not a good idea of archiving what you want ,You should use hidden inputs instead.
example place this anywhere inside your <form>:
<input type="hidden" value="<?= $_GET['title']; ?>" name="title"/>

try this. worked for me..
<?php $title = $_GET['title'];?>
<form method="link" action="surveymaker.php?title=<?php echo $title;?>">

Related

Can't make my input fields sticky

I am trying to making sticky form. To be nice feature for end users, especially if they are requiring them to resubmit a form (for instance, after filling it out incorrectly in the first place).
This is how I do it.
<input type="text" class="form-control" name="name" value="<?php if(isset($_POST['name'])) echo htmlentities($_POST['name']); ?>">
Its working for me. But my problem is if I use required attribute for this INPUT, its not echoing VALUE in the input. But still I can see its value is available when I check it using firebug.
NOTE: It also happen when I using a validation plugin like this.
Can anybody know whats the problem here?
Hope somebody may help me out.
Thank you.
try this one
<?php if(isset($_POST['name'])){ echo htmlentities($_POST['name']); }?>

How can I execute php before posting to someone elses script from a form?

I have a contact form which posts to mailchimp - but in certain cases, I also want it to send out an email.
I considered changing the <form action="... from mailchimp to my own page, containing something similar to the following:
<form action="mailchimp_url..." ...>
<? foreach($_POST as $name=>$value){?>
<input type="hidden" name="<? echo $name;?>" value="<? echo $value;?>">
<? }?>
<? //mail the stuff I want somewhere else ?>
I could then just auto execute this on pageload with javaScript.
The Problem is, IF this works, it will be dependant on JavaScript, or will require an additional button click for the user.
Is there a more elegant way to do this?
You can post to your own server first
<form action="your_path" ...>
Then use cURL to post the same data to mailchimp
Here is a good example on how to
this code will be done if you come from another form, submited.
Then your $_POST will be parse. But because $_POST exists only if a form is posted, you can't use it before.
What do you want to do exactly? If you want to add hidden input in your form fontion of specific params, you may use $_GET or maybe $_SESSION

I can't use GET and POST at the same time in PHP

Near the top of my page, I have this:
<?php $id = $_GET['id']; ?>
Then I have some form check conditionals that read from POST:
if (isset($_POST['completeSubmit'])) {
//code
}
And finally, I have an HTML form which looks like this:
<form action="<?php echo $_SERVER['PHP_SELF']."?id=$id"; ?>" name="complete" method="post">
<input type="submit" id="textButton" name="completeSubmit" value="[mark as complete]">
</form>
The page is initially accessed by using GET with an id variable like this:
http://website.com/page.php?id=1
All subsequent form submissions (which get redirected to the same page) fail. I know you can't send both GET and POST in the same request, but seeing as my form is submitting to $_SERVER['PHP_SELF']."?id=$id" using POST shouldn't it work? This is my first time trying this so it is quite possible I've overlooked something trivial.
You can use get and post at the same time, but you shouldn't. If you want to continue to send the ID this is as simple as:
<form ...
<input type="submit" ...
<input type="hidden" name="id"
value="<?php echo htmlspecialchars($_GET['id'], ENT_QUOTES); ?>" />
</form>
Of course you can not use GET and POST methods simultaneously.
However you can use a query string while sending a form using POST method, which being used to populate $_GET array.
To find a certain error you have to provide more info. At least 2 things:
how does HTML form look
what do yo see in the query string after posting the form.
and errr...
do you use any header redirects in the form processing?

Hard-Coding a Form so User Can't Edit It

I would like to hard-code the form below. In other words, I would like to post $submission and $fullurl to index.php as "tweet" without giving the user the option to edit them.
How can I do this?
EDIT: I want the user to still click a button that says "Tweet" to post the variables.
Thanks in advance,
John
<form method='post' action='index.php'>
<br />
<textarea name="tweet" cols="50" rows="5" id="tweet" ><?php echo $submission ?> <?php echo $fullurl ?></textarea>
<br />
<input type='submit' value='Tweet' name='submit' id='submit' />
</form>
<form method='post' action='index.php'>
<p><?php echo $submission ?> <?php echo $fullurl ?></p>
<input type="hidden" name="tweet" value="<?php echo $submission ?> <?php echo $fullurl ?>">
<input type='submit' value='Tweet' name='submit' id='submit' />
</form>
That still doesn't mean the user won't be able to doctor the value through, everything's editable client-side one way or another. Save the to-be-submitted tweet server-side in a session if you need absolutely immutable values.
I'm not entirely sure what you are asking, but it sounds like you just want to have some variables kept serverside without a user being able to edit them, between page loads.
In that case you may wish to learn about Sessions and session variables. These allow you to store stuff in between page loads without a user being able to edit them (but you can still read from them so you can display your variables on the page!)
If you could perhaps rephrase your question some if this isn't the answer you are looking for, we would be better able to assist you.
You could set the disabled
attribute of the <textarea>. But then
it would not be submitted with the
form. So you'd have to make a hidden
input with the value or use sessions to persist the data if it could not be recreated therwise.
You could also use JavaScript to blur the text area on focus. But there are ways around this, i.e. disable JavaScript.
In the end, I think you should reconsider the user interface and user experience. A textarea that isn't editable, probably should be a form element. Just display the data you plan to tweet and allow them to Appove it.
You could just make it completely invisible (style="visibility:hidden"), or disable it (`disabled="true"').
You can also just use document.getElementById('formID').submit(); to submit the form automatically.
Better yet, just make them all hidden inputs, and submit on page load with the above code.

How to save information in inputs inside a form so it doesn't disappear when form is submitted to itself?

I have a picture upload inside a form...
The file is a php file btw...
Problem is whenever this form is filled in, and the user clicks to upload the first picture, the form is submitted to itself and all the fields which the user may have filled in will go blank...
I know of one way to do it, alot of 'isset' in my php code, but is there any simpler or maybe better way I don't know of?
Thanks
You echo back the POST variable on your fields.
<form method="POST">
<input type="text" name="name" value="<?php echo $_POST['name']?>" />
<input type="submit" name="submit" />
</form>
When the form is submitted to self, the same data will be filled.
Well i do not know of anything else. I always use this:
<input type="text" value="<?= isset($value) ? $value : ""; ?>">
I think it is not too much code in the Templates, but it does the Trick.
Alternatively you could use some Frameworks wich abstract everything for you, but i cannot recommend some...

Categories