passing data using php and html - php

I am trying to pass a certain data which I found in my table using the search to another php page . here is my code
echo "
1<form action="adm_edit.php?product_code=$record[0]" method="POST">
2<input type=submit value=Edit>
3</form>
4<form action="adm_edit.php?product_code=$record[0]" method="POST">
5<input type=submit value=Delete>
6</form>
";
my search function is working fine and record[0] is contain desired data but I am getting this error when I run this code:
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in search.php on line 1
I put numbers on lines in above code for ease of reading
So could you please help me?
Thank you

Take care when using quotes from html elements in echos, also when using variables!
When using ' instead of ", you also have to put quotes in front of the variable and that way you stop echoing a string and can start with echoing a variable. You need to concatenate the var and the string with a . !
This will work :
echo '
<form action="adm_edit.php?product_code='.$record[0].'" method="POST">
<input type=submit value=Edit>
</form>
<form action="adm_edit.php?product_code='.$record[0].'" method="POST">
<input type=submit value=Delete>
</form>
';

<form action="adm_edit.php?product_code=<?php echo $record[0]; ?>" method="POST">
<input type=submit value=Edit>
</form>
<form action="adm_edit.php?product_code=<?php echo $record[0]; ?>" method="POST">
<input type=submit value=Delete>
</form>

Related

Php send post vars in form

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. :-)

PLS HELP PHP syntax

Here's my form
<form id="place-bid" action="placebid.php?placeBidProductId='.$row['product_id'].'" method="post" >
Your Bid:<br>
<input type="text" placeholder="$$$" name="money">
<input type="Submit" class="button_bid" name="submit" value="Place Bid">
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
</form>
and here is my placebid.php
<?php
// $product_id=$_GET['placeBidProductId'];
$product_id=$_POST['row_id'];
echo " Id Produs : ".$product_id;
?>
My problem: I can't get the value from $row['product_id'], it will only echo the string "$row['product_id'] .$product_id;"
I'm guessing that before you are outputting that form you are doing some sort of query of a database. If that is the case then you likely want to change this line
<input type=hidden id='rowid' value=".row['product_id']." name='row_id'>
to
<input type=hidden id='rowid' value="<?php echo row['product_id'];>" name='row_id'>
Use print_r($_POST) for all form post value on form action page i.e. placebid.php. it will show you all value in form. Then make you business logic accordingly.

Trouble getting echo syntax correct for html code

I am returning text results from a PHP FOREACH loop, and I want to add the links add and delete for each result where it looks like this: ADD | DELETE. The trick is, I need each link to be a form that is styled as a link so it looks like text links but I can use Post when the links are clicked. I can style the links with the class name "formlink", but my question is, how will the statement syntax look? I keep getting T_variable errors. Can someone help show how the code below so it will display error-free on the page?
<?php
echo "<hr>" . $results[text]
<form action="" method="post"><button type="submit" class="formlink">Approve</button>
</form> | <form action="" method="post"><button type="submit"
class="formlink">Delete</button></form>
That's one messy code though. Well I dunno, I could give you a brief idea, and the rest is up to you.
<?php
// Check clicked button
if (isset($_POST['btn_approve'])) {
$username = $_POST['username'];
}
if (isset($_POST['btn_disapprove'])) {
// Do something
}
?>
<form action="" method="post">
<input type="text" name="username" value="<?php echo (isset($username) ? $username : ''; ?>">
<input type="submit" name="btn_approve" class="form-link" value="Approve">
<input type="submit" name="btn_disapprove" class="form-link" value="Disapprove">
</form>
You could check isset() and How to write ternary operator.
<?php
echo "<hr>" . $results[text];
?>
<form action="" method="post">
<button type="submit" class="formlink">Approve</button>
</form> |
<form action="" method="post">
<button type="submit" class="formlink">Delete</button>
</form>

HTML form's "action" doesnt carry over my $_GET data

I have a HTML form that acts as a "confirm deletion?" page and my "Back" button is playing up.
$string = "foo.php?id=" . $_POST['fooid'] . "&id2=" . $_POST['barid'];
<!-- ^^ this ends up being "foo.php?id=1&id2=2" -->
<form action='<?php echo $string; ?>'>
<input type='submit' value='Back'>
</form>
the problem is, when the button is pressed it links to foo.php without any of the $_GET data in my string, even though the string contains "id=1&id2=2"
P.S I changed the code above so people could better understand it, here is the raw code:
<?php
$string = "xrays.php?id=" . $_POST['visitid'] . "&id2=" . $_POST['patientid'];
?>
<form action='delete.php' method='post'>
<input type='hidden' name='xrayid' value='<?php $_POST['xrayid']?>'>
<input type='submit' name='submit' value='Confirm Delete?'>
</form>
<form action='<?php echo $string; ?>'>
<input type='submit' value='Back'>
</form>
Maybe it's not the best answer but I would like do something like that:
$string = "foo.php?id=" . $_POST['fooid'] . "&id2=" . $_POST['barid'];
<!-- ^^ this ends up being "foo.php?id=1&id2=2" -->
<form action='foo.php'>
<input type="hidden" name="fooid" value ="<php echo $_POST['fooid']; ?>" />
<input type="hidden" name="barid" value ="<php echo $_POST['barid']; ?>" />
<input type='submit' value='Back'>
</form>
This should work properly.
EDIT: change $_GET na $_POST
You need to put the get variable in the form hidden inputs, like so:
<form action='foo.php'>
<input type="hidden" name="id" value="1" />
<input type="hidden" name="id2" value="2" />
<input type='submit' value='Back'>
</form>
Or you could use a link:
Back
Let's start with form submission in general. W3C: Form Submission
Next, let's review $_GET and $_POST. PHP Manual: $_GET | PHP Manual: $_POST
In summary, inside of your <form> tag, use either method="get" or method="post". Only one of the superglobal arrays will be populated by successful controls, based upon your method of sending the data. I believe the query string must result from a GET request url (which may be the default), not just a plain string slapped into the action="" attribute. I could be wrong about the query string, but you have another problem. You are using two forms on one page. Presently, I think only one form's controls can be submitted successful at a time.
<form action='delete.php' method='post'> <!-- FORM 1 -->
<input type='hidden' name='xrayid' value='<?php $_POST['xrayid']?>'>
<input type='submit' name='submit' value='Confirm Delete?'>
</form>
<form action='<?php echo $string; ?>'> <!-- FORM 2, add a method="" attribute -->
<input type='submit' value='Back'>
</form>
Upon adding a method="get" to form two, it should become clear that a composite $_POST + $_GET request is not possible in the two form approach, and that you need to start with making a single, monolithic form instead of two modular ones. Using the type="hidden" attribute of an <input /> tag, inside of one form, as in #machineaddict's answer, will help. However, what will really help is if you explicitly use all the correct attributes of each tag so that you can spot errors like this in the future.
In a situation like this, it is helpful to know that the $_SERVER['QUERY_STRING'] element would hold the complete query string if your web server received one.
PHP Manual: $_SERVER

syntax error, unexpected '<' using eval and base64

I'm trying to encode a simple code but I'm getting this error:
Parse error: syntax error, unexpected '<' in C:\wamp\www\test.php(2) : eval()'d code on line 1
This is the code:
<form method="POST" action="">
Enter your command: <input type='text' name='cmd'> <input type='submit' name='execute' value='Execute'>
</form>
echo $_POST['cmd'];
This is the base64_encode with eval:
<?
eval(base64_decode('PGZvcm0gbWV0aG9kPSJQT1NUIiBhY3Rpb249IiI+CkVudGVyIHlvdXIgY29tbWFuZDogPGlu cHV0IHR5cGU9J3RleHQnIG5hbWU9J2NtZCc+IDxpbnB1dCB0eXBlPSdzdWJtaXQnIG5hbWU9J2V4ZWN1dGUnIHZhbHVl PSdFeGVjdXRlJz4KPC9mb3JtPgoKCmVjaG8gJF9QT1NUWydjbWQnXTsK'));
?>
That is because you are trying to eval non-php code (html form)
You forgot the PHP tags around your code:
<?php
// Any PHP code as needed
?>
<form method="POST" action="">
Enter your command: <input type='text' name='cmd'> <input type='submit' name='execute' value='Execute'>
</form>
<?php
echo $_POST['cmd'];
?>

Categories