Forms html merg values or add an value on send - php

I dont know if this is even possible without javascript but i want an form input and when i send the form it should add some text on the value.
<form method="post" action="https://somesite.com">
<input name="snapname" style="width: 177px; margin-top: 340px;" type="text" placeholder="Your username">
<input type="Submit" name="send">
</form>
So right now the form sends the value that is typed from the user in the browser.
Is there an way for me to add text when i send the form that the user cant see? Do i need to do it in javascript? Or is it possible in HTML? HTML5?
For example lets say the user writes adam
The value of snapname is now adam
When they click submit it will submit it as adam
But what i want is to add text to the value.
So i want mytext+adam to be the value of snapname where mytext is a static value.
I do not want to use javascript if i dont need to. Is it a good idea to use some kind of redirect php script? And let the script add the text and then send the users to the correct webadress with right value?

Use javascript or simply:
$my_var = "My Text" . $_POST['snapname'];
And then use this variable in php.

If you are submitting to an external site your best bet will be to use javascript. You can remove the name from the visible input (so its data is not submitted), add a hidden field with the correct name, then populate the hidden fields value with javascript:
<form method="post" action="https://somesite.com">
<input onkeyup="appendvalue(this.value);" type="text" placeholder="Your username">
<input name="snapname" type="hidden" id="snapname">
<input type="Submit" name="send">
</form>
<script>
function appendvalue(userval){
var append = "somestring";
var hidden = document.getElementById("snapname");
hidden.value = userval+append;
}
</script>

PHP is going to be your best bet here, as Faiz said above create a simple variable and extend the $_POST['snapname'] to include what you need.

Related

Using HTML textfield to pass a GET parameter to PHP file

I want to pass a GET parameter to a PHP file , by data typed in a text field. I want something like this :
<form id="Form1" action="action.php?nameExample=textFieldData" method="get">
the textfield is out of this form that's why I need to affect the value of the textfield into get PHP parameters so I can send it with another data in the same msg
the textfield code :
Username: <input type="text" name="nameExample"><br>
anyway to do it ?
You can't use $_GET parameters in an HTML form action attribute if the <form method='get'. Use <input type='hidden' name='nameExample' value='whatever' />, or better yet, use AJAX without a form.
This should do what you want to. The JS code copies the value of an external input field to a hidden input field.
HTML:
<form action="action.php" method="get" id="Form1">
<input type="text" name="otherData" value="xxx">
<input type="hidden" name="nameExample" value="" id="nameExampleCopy">
<input type="submit">
</form>
Username: <input type="text" name="nameExample" id="nameExampleOriginal">
JS:
document.getElementById('Form1').addEventListener('submit', function () {
var originalElement = document.getElementById('nameExampleOriginal');
var hiddenElement = document.getElementById('nameExampleCopy');
hiddenElement.value = originalElement.value;
});
But you should think about some points:
Is get the right method here? It sounds like post could be better.
Is it semantically a good idea to pass a value from outside the form inside it? We don't know your application, but generally it is a good idea to have all submitted data inside the form. As HTTP pointed out in his comment, sessions could also help with that.

HTML update input textarea everytime "Submit" it clicked

<html>
<script>
function changeText()
{
document.getElementById("input1").value = <?php echo '"'.$_POST['input'].'"'; ?>;
return true;
}
</script>
<form name="mainform" action="" method="post">
<input type="text" name="input" id="input1" />
<input type="submit" onclick = "changeText()" name="Submit" value="Submit!" />
</form>
<html>
i have this code here. can you make it work as intended ?
everytime i click Submit! i want to change the value of the textarea to the last input the user inserted.
PHP code is parsed by a PHP interpreter before any HTML output is sent to the browser.
If your form action is the same page and the same form will be shown before and after submission, then you can let PHP print the value of the input field directly into it.
<input type="text" name="input" id="input1" value="<?php echo htmlspecialchars($_POST['input']);" />
If you're trying to revert the value of this input field whenever a user clicks the submit button, then your code (even if it's prone to code injection) should work but this is useless since the page will be requested again when submit is clicked.
I assume you need to fill in
action=""
By the name of your file, like
action="myFile.php"
Few tips :
NEVER trust the user. The user can manually change the value of the input and send some dangerous values in your $_POST variable. You need to check it using filter_input() by example.
Like #Charles said this is pretty simple problem, use google next time.Here for example

HTML using forms are compulsory ?

i was making a website with PHP,jQuery,AJAX,mySQL which has lot of interaction with user. Now what i was asking that using forms are really necessary ?
what i have done for most of my user inputs are kinda like this -->
simple e.g
<div class="contact-form">
<input type="text" class="textInputs" id="name" placeholder="Enter your name..." />
<button class="submitButton" id="contactSend">Subscribe</button>
</div>
A SIMPLE comment system
<div class="comment-box" d-id="1">
<input type="text" class="textInputs" id="comment" placeholder="Enter comment..." />
<button class="submitButton" id="comment">Comment</button>
</div>
<div class="comment-box" d-id="2">
<input type="text" class="textInputs" id="comment" placeholder="Enter comment..." />
<button class="submitButton" id="comment">Comment</button>
</div>
JQUERY kinda this->
$('#comment').click(function(){
var id = $(this).closest('.comment-box').attr('data-id');
//ajax stuff
}):
Therefore what i wanna ask is whether this kinda structure is good or gonna cause some serious problem ?
Are using <form>'s compulsory ?
If you are submitting form by clicking submit button and posting variable to other pages or the same page then tag is necessary
If you are using form tag to find input values to post using ajax is necessary
If you are using ajax and pulling input values using dom id to post via ajax form is not required, in this case you may also need to do javascript validation using dom ids
Ideally using tag is better and follows standard HTML structure.
It depends upon your requirement and how you going to use the form in your project.
If you have min. no of fields ( 2-3 fields ) in your form. You can directly manipulate the form fields using id or name with JQuery or JavaScript. Otherwise you should have form tag to manipulate the data with more fields in the form.
I would recommend to have form tag in your page to maintain the standard format and use form tag and Ajax submission with Jquery.
Note: Anyway if you are going to get form data using Jquery, better to use the following syntax to get the from values:
// To check the radio button
var isAnsChecked = $("input:radio[name=<FIELDNAME>]").is(":checked");
var radio-value = $("input:radio[name=<FIELDNAME>]:checked").val();
// get Text box values
var text-value = $("input:text[name=<FIELDNAME>]").val();

How can I dynamically change text based on input field?

How can I dynamically change a link based upon an input field in a form. For example, if I input 1.00 into the input field, I want to change the link to this:
donate.php?amount=1.00
Where the amount changes to the amount specified in the input field.
I'm guessing its JavaScript which isn't my strongest point but any help would be awesome. :)
Thanks
markup:
<input type="text" id="amount" onkeyup="changeLink(this);" />
donate now!
Javascript:
function changeLink(inputElement)
{
$('#donateLink').attr("href","donate.php?amount="+inputElement.value);
//console.log($('#donateLink').attr("href"));
}
jsfiddle working example here.
This can be done with html forms:
<form action="donate.php" method="GET" id="donateform">
<input type="text" name="amount" />
<input type="submit" value="Donate" />
</form>
You could also have input entered via a drop down list so they don't enter invalid values. Or you could validate the input with javascript. To use a link to submit the form, you can use javascript:
Donate
You don't need to do anything, just use a form. By using the GET method with a form and naming your input field 'amount', that will already be added to the URL at the time of form submission. Go ahead and try submitting a form when you enter 1.00 into the box. When the page loads, your URL will be donate.php?amount=1.00 like expected. The URL does not need to be changed whatsoever.
If you're using POST, I would merely suggest not doing this. It serves no purpose in that case.

Persist GET vars that created outside and inside a form

Lets say that I have an HTML page "myPage.php" with a form that uses the GET method and calls itself:
<form name="myForm" action="myPage.php" method="get">
<input type="text" name="input1" />
<input type="text" name="input2" />
.
.
.
<input type="submit" />
</form>
And also, in this page and outside the form, I have an anchor that also calls the page itself, but with a GET variable "myVar" added (a GET variable that is not one of the form's variables):
Now... I wish that all the variables will be persisted no matter whether the form was submitted or the link was pressed.
e.g., if a user was pressing the link, the URL will get the 'myVar=100' + the form variables (as if the form was also submitted together with pressing the link), and vise versa - if the user submits the form, the URL gets the form's vars as well as the "myVar", if indeed it was in the URL.
Is there a way?
Thank you :)
Hence, the easy way is to set the links with
Something
also, change the action in your form to
<form action"myPage.php?var=100" method="get" name="form_name" id="form_name">
...
and they will send the same form. Anyhow, you'll deal with form inputs one way or another, right?
EDIT: Well, if it depends on the link clicking, then:
1.) Receive the myVar in PHP
$myCurrval = $GET['myVar'];
2.) Assign it to a JS var
<script type="text/javascript">
var JOHNNY = <?php echo $myCurrval; ?>
</script>
2.) Add a hidden input
<input type="hidden" id="myVar" name="myVar" value="" />
2.) And change the link to
Something
3.) Then, create a JS method called, yeah, processForm(), who will decide if sets the myVal or not.
function processForm() {
if (JOHNNY != ''){
document.getElementById('myVar').value=JOHNNY;
}
}
This way, your myVar value will propagate only if it was received before (it means, the first time you send it, it will persist). Somehow, I would use $_SESSION to keep things between requests! In fact, it is safer and lot easier!!! :)
Hope it helps.

Categories