Assign HTML attribute with PHP - php

Hello I'm trying to send one variable with POST (Hello Angel). That is the code:
<form action="dos.php" method="post" name="compra">
<input name="id_txt" type="hidden" value=<?php echo "Hello Angel" ?>/>
<input type="submit" name="Send" value="Send" />
</form>
when in the other page show the variable, only shows up space (only Hello). That is the code:
<?php
if (isset($_POST['id_txt']))
echo $_POST['id_txt']
?>
So, how Can I show all?

Quote your value like this:
<input name="id_txt" type="hidden" value="<?php echo "Hello Angel"; ?>" />
When you don't put quotes around an HTML attribute value it only takes the first word as the value.

You're missing the quotes around the input value
Change:
<input name="id_txt" type="hidden" value=<?php echo "Hello Angel" ?> />
To:
<input name="id_txt" type="hidden" value="<?php echo "Hello Angel" ?>" />

Related

Execute input value in different php file [duplicate]

This question already has answers here:
PHP keep checkbox checked after submitting form
(5 answers)
Closed 2 years ago.
I'm still studying php html, any help is appreciated.
I have code like this:
<form method="post">
<input type="checkbox" id="option1" name="option" value="<?php echo "Hello World!"; ?>" />
<label for="option1"> Do you want to print Hello World? </label> <br />
<input type="checkbox" id="option2" name="option" value="<?php echo "Hello Brother" ?>" />
<label for="option2"> Do you want to print Hello Brother?</label> <br />
<input type="checkbox" id="option3" name="option" value="<?php echo "Hello Human" ?>" />
<label for="option3"> Do you want to print Hello Human?</label> <br />
<br />
<br />
<input type="submit" id="Submit" name="Submit" value="Submit"/>
</form>
<?php
if(empty($_POST["option"])){
echo "You need to choose at least one!";
}
else {
echo "Print successful!";
}
?>
I want to have function that if checked, then print the value in different php file. I also have problem that when I checked and submit, the check mark disappeared.
I want it to be like, if checked then true, print the value. if not checked then false, do not print the value. Any idea? Thank you very much!
You have to use action="#" in form tag and mentioned the page name.
Working Demo: http://phpfiddle.org/main/code/brkr-u9st
<input type="checkbox" id="option1" name="option1" value="<?php echo "Hello World!"; ?>" <?php if(isset($_POST['option1'])) echo "checked='checked'"; ?> />
<label for="option1"> Do you want to print Hello World? </label> <br />
<input type="checkbox" id="option2" name="option2" value="<?php echo "Hello Brother" ?>" <?php if(isset($_POST['option2'])) echo "checked='checked'"; ?> />
<label for="option2"> Do you want to print Hello Brother?</label> <br />
<input type="checkbox" id="option3" name="option3" value="<?php echo "Hello Human" ?>" <?php if(isset($_POST['option3'])) echo "checked='checked'"; ?> />
<label for="option3"> Do you want to print Hello Human?</label> <br />
<br />
<br />
<input type="submit" id="Submit" name="Submit" value="Submit"/>
</form>
<?php
if(empty($_POST["option1"]) && empty($_POST["option2"]) && empty($_POST["option3"]) ){
echo "You need to choose at least one!";
}
else {
if(isset($_POST["option1"])){
echo $_POST["option1"];
}
if(isset($_POST["option2"])){
echo $_POST["option2"];
}
if(isset($_POST["option3"])){
echo $_POST["option3"];
}
}
?>
You need to have your form action attribute pointed to a PHP file. Then that file will receive the data via POST and you can do whatever you'd like with it there. The action attribute looks like this
<form method="post" action="\path\to\phpfile.php">
Also, the reason the check mark and other values disappear is because when you submit the form, it is currently executing the PHP file that code is written in. Essentially resetting everything. You can retain the input values by passing the POST input values into the HTML. It would look like this
<form method="post">
<input type="checkbox" id="option1" name="option1" value="<?php echo "Hello World!"; ?>" <?php if(isset($_POST['option1']){ echo 'checked="checked"';} ?>/>
<label for="option1"> Do you want to print Hello World? </label> <br />
<input type="checkbox" id="option2" name="option2" value="<?php echo "Hello Brother" ?>" <?php if(isset($_POST['option2']){ echo 'checked="checked"';} ?> />
<label for="option2"> Do you want to print Hello Brother?</label> <br />
<input type="checkbox" id="option3" name="option3" value="<?php echo "Hello Human" ?>" <?php if(isset($_POST['option3']){ echo 'checked="checked"';} ?> />
<label for="option3"> Do you want to print Hello Human?</label> <br />
<br />
<br />
<input type="submit" id="Submit" name="Submit" value="Submit"/>
</form>
<?php
if(empty($_POST["option"])){
echo "You need to choose at least one!";
}
else {
echo "Print successful!";
}
?>
Make sure your input values don't have the same name.

How to properly escape tags in input text

looks like ordinary question but could not find correct solution. lets say I have this form:
<form name="form" action="nextpage.php">
<input type="text" name="input_name" value="<?php echo $_POST['input_name'];?>" />
<input type="submit" name="submit" value="Next"/>
</form>
For example if I add "My input text >" as value of the input the nextpage.php is broken.
I tried to use for:value="<?php echo strip_tags($_POST['input_name']);?>" or
:value="<?php echo htmlspecialchars($_POST['input_name']);?>" but none of them works.. Why is that and how to avoid it? Thanks
First, you should sanitize your posted value for security, but that aside, php provides a function that automatically escapes special characters called addslahes http://php.net/manual/en/function.addslashes.php, save your post value to a variable using the addslashes function, then use the variable as your value.
try this
<?php
$_POST['input_name']="ddd"; ?>
<form name="form" method="POST" action="nextpage.php">
<input type="text" name="input_name" value="<?php echo $_POST['input_name'];?>" />
<input type="submit" name="submit" value="Next"/>
</form>
and in nextpage.php
<?php
$name=$_POST['input_name'];
echo $name;
Try this:
<form name="form" action="nextpage.php" method="POST">
<input type="text" name="input_name" value="<?php echo $_REQUEST['input_name'];?>" />
<input type="submit" value="Next"/>
</form>

how to send checkbox value on next page

how to send check box value in to next page without submit button and without action.
here i am going to next page using link so i want that these checkbox value send on next.php
my form is
<form method="post" action="" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
</form>
and here a link by using i am going to next page
go here
To post data from one page to another page you have to use a submit button like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" >
Monthly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);">
Yearly<br /><br />
<input name="pay_type" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);">
Lifetime
<input type="submit" value="Go here" />
</form>
also specify receiving page in action attribute of <form>
In another page (next.php).
You have to access your posted values using $_POST['name'];
eg.
echo $_POST['pay_type'];
EDIT
In your code, I can see that you want to send multiple values for checkbox pay_type, for that you have to write it as an array (name="pay_type[]")in HTML to send multiple values.
So, the complete code will looks like:
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;">
<input name="pay_type[]" type="checkbox" value="<?php echo $monthly; ?>"id="chk_Box102" onClick="toggle_2(this);" > Monthly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $yearly; ?>" id="chk_Box103" onClick="toggle_2(this);"> Yearly<br /><br />
<input name="pay_type[]" type="checkbox" value="<?php echo $lifetime; ?>" id="chk_Box104" onClick="toggle_2(this);"> Lifetime
<br/>
<input type="submit" value="Go here" />
</form>
Code in next.php:
<?php
echo "<pre>";
print_r($_POST['pay_type']); //it will print complete array of values for pay_type checkbox
echo "</pre>";
?>
Log1c ツ as said before you need a button.. :). What is also possible to use an attribute onClick:
Add an ID at your form tag (id="myForm") (dont forget the target page next.php in action)
<form method="post" action="next.php" name="Logreg" style="padding-left:35px; padding-bottom:35px; font-size:14px;" id="myForm">
and then the onClick attribute
go here
jsfilleDemo

Having issue with my html form in a php page

I have tried to put this code in my webpage but I have this error :
Parse error: syntax error, unexpected '?' in E:\MyServer\htdocs\...(line 1 from this script)
CODE :
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
this code is in a .php page
Thanks.
Try to format it properly:
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
How to get the value you pass through your form:
Welcome <?php echo $_POST["name"]; ?><br>.
EDIT
Also you should add this in delete.php file:
if(isset($_POST['name'])){ $name = $_POST['name']; }
See : http://www.w3schools.com/php/php_forms.asp
You need to drop the double quotes around the variable name and move them to the actual element property.
<form action='delete.php?name="<?php echo $contact['name']; ?>"'
^ ^
It then becomes
<form action="delete.php?name=<?php echo $contact['name']; ?>"
<form action="delete.php?name=<?php echo $contact['name']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
This should work ..
Do you need to send the data via GET and POST to delete.php?
<form action='delete.php' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
With this you can then just do $_POST['name'] to get your contact name...
Sorry if I have missed something but cannot see why you would need it passed twice

import html value in a php function

I have this code that fetches information in a file and shows the result. That all works as expected. What I'm not able to do is to export the button value in the function when it is pressed so the value of the pressed button will transfer to a variable in my php function. How can I do that?
Here the code:
<h2>Demmarage script torrent</h2>
<form action="search.php" method="POST">
<input type="text" name="input_value">
<input type="submit" name="submit">
<?php
echo "<br>";
if (isset($_POST['submit'])){
$findme = $_POST['input_value'];
$findme1 = str_replace (" ", ".", $findme);
$savedarr = unserialize(file_get_contents('torrent.bin'));
foreach ($savedarr as $val1){
$mystring = $val1['title'];
if((stripos($mystring, $findme) !== false) or (stripos($mystring, $findme1) !== false)) {
echo "Show trouve: ";
echo $mystring;
?>
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
<?php
echo "<br>";
}
}
}
if (isset($_POST['editId'])){
//Here i want to import the value of the pressed button to do something
echo "download start";
}
?>
you should use a hidden field, like
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
After the submit you will be able to get the value with
$_POST['link']
Also, this should be in a form,otherwise the submit will post EVERY field ...
like
foreach(...)
{?>
<form action="" method="POST">
<input type="hidden" name="link" id="link" value="<?php echo $val1['link'] ?>" />
<input type="submit" />
</form>
<?php}
The problem is that you are setting the value twice by <button>value1</button> and by <button value="value2">... remember that the value of a submit button is always the text that shows up inside the button.
You should do replace this:
<button type="submit" value="<?php echo $val1['link']; ?>" name="editId">Telecharger</button>
with this:
<form method="POST">
<input type="hidden" value="<?=$val1['link']?>" name="editId" />
<input type="submit" value="Telecharger" />
</form>

Categories