Dynamically changing form action - php

I'm trying to change the form action on the fly, but think I'm having a syntax problem, that I just can't figure out.
This works:
<?php
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action='linktogoto.php'\">";
?>
This doesn't work:
<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action=$variable\">";
?>
I need to eventually change $variable depending on the situation, so need to get this to work.

<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action=$variable\">";
?>
Should be:
<?php
$variable = "linktogoto.php";
echo "<input type='submit' id='answerbutton' name='$buttonname'
value='$buttonvalue' onclick=\"this.form.action='$variable'\">";
?>
Notice the ' around $variable.

Related

Wrong value of input number returned

I'm doing this assignment where I have to do website. I created a input number for the quantity of the product with a button all this in a form. When I try to get the quantity by putting in the URL. The problem is I keep getting the wrong quantity. Not the quantity that I had in my input.
<?php
foreach ($productList as $product){
?>
<form action="index.php" method="post">
<?php echo "<ul class='row'>"?>
<?php echo "<li>"?>
<?php echo "<img src ='".$product->getImage()."'class=image'>" ?>
<?php echo "<div class='overlay'>"?>
<?php echo '<div class="text">'.$product->__toString()."</div>"?>
<?php echo "</div>"?>
<?php
echo"<p id ='titleQte'>Quantity </p>";
echo"<div class='qteProduct'>";
echo "<input type='number' name='qte' value='1' min = '1'/>";
echo"</div>";
echo"<a href=index.php?action=addCartProduct&id=".$product->getId().'&quantity='.((isset($_POST['qte'])) && (isset($_POST['btnAjout'])) ? $_POST['qte']:1).'>'."<button type='button' name='btnAdd' id='add' class='btn btn-success'>Add to Cart</button></a>";
?>
<?php echo "</li>"?>
<?php echo "</ul>"?>
</form>
<?php
}
?>
It gives me 1 instead of the actual value of the input.
Some help would be appreciated.
Thank you.
You are passing the value in the tag as value = '1' so this takes precedence over the actual input. Remove it and pass
echo "<input type='number' name='qte' min = '1'/>";
Hope this helps !
Just remove the value attribute from this line:
echo "<input type='number' name='qte' value='1' min = '1'/>";
into
echo "<input type='number' name='qte' min = '1'/>";
I hope this would be helpful.

How can I use PHP variables inside HTML tags?

I have the following doubt. I want to use php code to create a HTML form which outputs three values. And I want to use that values on a php function, something alike the following:
<?php
function modcreate(){
echo "<input type='text' name='dato'>";
echo "<input type='text' name='nvalor'>";
echo "<input type='text' name='identif'>";
echo "<input type='button' onclick=mod($GET['dato'], $GET['nvalor'],
$GET['identif']>";
}
?>
Any suggestions or cleaner ways to do this? Thank you beforehand
Try this one
<?php
function modcreate(){
echo "<input type='text' name='dato'>";
echo "<input type='text' name='nvalor'>";
echo "<input type='text' name='identif'>";
echo "<input type='button' onclick=" . mod($GET['dato'], $GET['nvalor'],
$GET['identif'] . ">";
}
?>
You were actually inserting php code inside double quotes which was behaving like a simple string. So close your double quotes before adding php tag
you dont need to echo html in php
function modcreate(){
?>
<input type='text' name='dato'>
<input type='text' name='nvalor'>
<input type='text' name='identif'>
<input type='button' onclick="mod(<?php (implode(',',[$GET['dato'],$GET['nvalor'],$GET['identif']);?>)">;
<?php
}
For a better syntax of how to handle html without echo is:
<?php
function modcreate(){ ?>
<input type='text' name='dato'>
<input type='text' name='nvalor'>
<input type='text' name='identif'>
<input type='button'
onclick="mod(
<?php (implode(',',[$GET['dato'],$GET['nvalor'],$GET['identif']); ?>
)">
<?php } ?>

How to pass a textbox value in to url parameter

i have some problem in here. I have textbox value inside table, and when i clicked link "Valid", i can pass the textbox value to another page.
This is my textbox code in table
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "</form>";
This is my button
echo "<td class='center'>";
echo "<a class='btn btn-primary btn-sm' href='validasi.php?idorder=" . $row['id_order'] . "&pilih=" . $_GET['pilih'] . "'>
<i class='fa fa-check-square-o'>Valid</i></a>
echo "</td>";
Maybe someone can give me a solution, Thank you and have a nice day!!
In the page validasi.php add this:
$IDorder = $_GET['idorder'];
$Pilih = $_GET['pilih'];
Now you have two variables on validasi.php called IDorder and Pilih from your form
Or is it the nomortempat you want?
I think you can just add
$nomortempat = $_GET['nomortempat'];
to the validasi page and it should work. If I understand your code it should be sent
EDIT I was wrong, you need to add a sumbmit button to your form.
Add session start at the top of your pages: session_start();.
Then add this to your first page:
$_SESSION["idorder"] = $idorder;
$_SESSION["pilih"] = $pilih;
Then this code to your form:
echo "<form name='nomor' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' name='submit' value='Submit'></input>";
echo "</form>";
Then on validasi page add this:
$nomortempat = $_GET['nomortempat']; // and:
$IDorder = $_SESSION["idorder"];
$Pilih = $_SESSION["pilih"];
Now you have all three values on validasi.php
with GET form, you could try this:
echo "<form name='nomor' action='action.php' role='form' method='get'>";
echo "<input type='text' name='nomortempat' class='form-control input-sm'></input>";
echo "<input type='submit' value='valid'>";
echo "</form>";
and in action.php page, to catch value in textbox name='nomortempat', code is below:
if (isset($_GET['nomortempat'])){
echo $_GET['nomortempat'];
}
Of course, you could add more input fields whenever you want to inside form with different names.

Echo Submit Button in PHP

I am trying to echo a submit form in my php code:
I am trying the following:
// Display results
foreach ($media->data as $data) {
echo "<a href=\"{$data->link}\"</a>";
echo "<h4>Photo by: {$data->user->username}</h6>";
echo $pictureImage = "<img src=\"{$data->images->thumbnail->url}\">";
echo "<h5>Like Count for Photo: {$data->likes->count}</h5>";
echo "<form>";
echo "<form action='tag.php' method='post'>";
echo "<input type='submit' name='submit'>";
echo "</form>";
}
Then:
if(isset($_POST['submit'])) {
echo "hello";
}
This doesn't seem to echo "hello";
Any help would be appreciated
You're missing a closing parenthesis:
if(isset($_POST['submit'])) {
^
Here
You're missing a value on the input tag. Change it to:
echo "<input type='submit' name='submit' value='Submit'>";
echo '<input type="submit" class="btn btn-primary" value="Active">';
It should be
if(isset($_POST['submit'])) {
^
echo "hello";
}
you missed a parenthesis.
Edit: Also it should be:
<input type='submit' name='submit' value='submit'>
Else $_POST['submit'] will not get set.

how to insert selected options into a database

I am building a php based quiz for website. I am a beginner. I coded for getting the questions and options from database and display them in the form in my page.when the user selects the option i want the the answers to be inserted to my database.I wrote some bsic code but its not working pls help. Here is my code
The table for question is having 7rows(qid,question,optiona,optionb,optionc,optiond,answeroption),answers table is having simple2rows(qid,answer)
if(isset($_POST['next']))
{
$a=$_POST['a'];
}
if(!isset($a))
{
$a=0;
}
include('connection.php');
mysql_query("INSERT INTO answers (username,qid, option)
VALUES ($username,a-1,'$_POST('option'))");
$sql1="SELECT * FROM exam1 LIMIT 1 OFFSET $a";
$result=mysql_query($sql1);
echo "<form method='post' action='quiz.php'>";
while ($row = mysql_fetch_array($result))
{
echo $row['question']. "<br/>";
echo "<input type='radio' value='optiona' name='option'>" .$row['optiona'];
echo "<input type='radio' value='optionb' name='option'>" .$row['optionb'];
echo "<input type='radio' value='optionc' name='option'>" .$row['optionc'];
echo "<input type='radio' value='optiond' name='option'>" .$row['optiond']; "<br/>";
}
$c=$a-1;
$b=$a+1;
echo "<input type='hidden' value='$c' name='a'>";
echo "<input type='submit' name='previous' value='previous'> ";
echo "<input type='hidden' value='$b' name='a'>";
echo "<input type='submit' name='next' value='next'> ";
echo "<input type='reset' name='reset' value='Reset'>";
echo "</form>";
?>
You need to study up on basic PHP syntax. $_POST is not a function. It's an array. e.g.
$var = $_POST['var'];
^-- ^--- note the bracketing.
Even if the code DID work, you'd be wide open to SQL injection attacks.
Big big atention! I hope this is just an test code note final one!
Your code is Inject Vulnerably here $a=$_POST['a'];
I recomend to replace with this one $a= filter_input(INPUT_POST, 'a', FILTER_SANITIZE_STRING); this is an escaped from attacks!
I hope it hepls you

Categories