I'm trying to echo a value in a form based on number of rows returned from database query. Keep getting error Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';'
As you can probably tell I'm pretty new to this. Can anyone help me echo the variable? I know that $num_rows is returning a value as using var_dump shows. Thanks
<?
if($num_rows <= 10) {
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';
}
if($num_rows >10) {
echo '</br></br><form id="h2" class="rounded" action="4.php"
target="_blank" method="post"/>
<input type="submit" name="submit" class="button" value="11"/><BR>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';
}?>
In both of your code blocks, you repeat the command echo instead of either concatenating the output or using two statements. You have done this:
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="'echo $num_rows;'"/>
</form>';
which is a syntax error. Instead, you can do this:
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="' . $num_rows . '"/>
</form>';
or this:
echo '</br></br><form id="h1" class="rounded" action="4.php" target=""
method="post"/>
<input type="submit" name="submit" class="button" value="10" /><br>
<input type="text" name="number_of_tests" value="';
echo $num_rows . '"/>';
echo '</form>';
This is the code you should use to concatenate strings and output the result
echo ' some value ' . $variable . ' other text ';
The echo function outputs a string, while the dot (.) operator concatenates strings. This is the kind of wrong code
echo 'value="'echo $num_rows;'"/>';
When you want to insert the value of a variable this is the way
$a_string = 'I\'m a string';
echo "I'm a double quoted string and can contain a variable: $a_string";
This works with arrays too
$an_array = array('one', 'two', 'three');
echo "The first element of the array is {$an_array[0]}"
See the PHP manual
Related
I'm new to PHP and coding in general.
Currently I'm trying to make a Simple Calculator.
For this I have the two scripts calculator.php and array.php
My Idea is to store all the inputs the user gives in an array so I can calculate them using the eval() function. Because I need a string for this I converted the array using a foreach loop.
But when I try to use the variable outside the foreach loop I get the following errors:
Notice: Array to string conversion on line 17
Array
Parse error: syntax error, unexpected ';', expecting '(' in /Applications/XAMPP/xamppfiles/htdocs/dashboard/array.php(22) : eval()'d code on line 1
What can I do to fix this problem?
array.php
<?php
session_start();
if (!is_array($_SESSION)) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['button'])) {
$_SESSION['persistentValues'][] = $_POST['button'];
}
foreach ($_SESSION as $value) {
echo $value;
}
if (isset($_POST["result"])) {
$p = eval($value.";");
echo $p;
}
?>
calculator.php
<?php
include ('./array.php');
?>
<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="">
Enter value: <input type="text" name="value"> <br> <br>
<div style="padding-left: 105px">
<input type="submit" value="9" name="button">
<input type="submit" value="8" name="button">
<input type="submit" value="7" name="button">
<input type="submit" value="+" name="button">
<br>
<input type="submit" value="6" name="button">
<input type="submit" value="5" name="button">
<input type="submit" value="4" name="button">
<input type="submit" value="-" name="button" style="padding-left: 9px">
<br>
<input type="submit" value="3" name="button">
<input type="submit" value="2" name="button">
<input type="submit" value="1" name="button">
<input type="submit" value="/" name="button" style="padding-left: 9px">
<br>
<input type="submit" value="0" name="button" style="padding-left:33px">
<input type="submit" value="." name="button" style="padding-right:9px">
<input type="submit" value="x" name="button" style="padding-left: 7px">
<br>
</div>
<br>
<input type="submit" value="Calculate" name="result">
</form>
</div>
You're repeatedly confusing the whole $_SESSION variable with $_SESSION['persistentValues']. The latter is where you're holding the buttons as you submit them.
The value of $value is an array because you're looping over $_SESSION to set it, and the value of the session variable is an array. The argument to eval() is a string, and when you convert an array to a string it becomes the string Array, which isn't an expression that can be evaluated.
You can use implode() to concatenate all the elements of an array into a string. So if the session array contains ['9', '3', '+', '8'] it will set $value to '93+8', which you can then pass to eval().
The argument to eval() needs to be a statement. So you should put the echo command in the argument.
<?php
session_start();
if (!is_array($_SESSION['persistentValues'])) {
$_SESSION['persistentValues'] = array();
}
if (isset($_POST['button'])) {
$_SESSION['persistentValues'][] = $_POST['button'];
}
$value = implode('', $_SESSION['persistentValues']);
echo $value;
if (isset($_POST["result"])) {
eval("echo $value;");
}
?>
i have this code here. it gives me an error of Syntax error, unexpected T_STRING, expecting ',' or ';'. i want to be able to display a success message on the same page rather than opening another page. please help.
} else {
$query = "SELECT title, author, post, id FROM news_posts WHERE id=$id";
$result = mysql_query($query);
$num = mysql_num_rows($result);
$row = mysql_fetch_array ($result, MYSQL_NUM);
$title = $row['0'];
$name = $row['1'];
$message = $row['2'];
if ($num == 1) {
echo '
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<p><b>Post Title :</b><br />
<input type="text" class="form-control" name="title" value="'.$title.'" /></p>
<p><input type="hidden" name="name" size="15" maxlength="255" value="'.$name.'" /></p>
<p><b>Post Message :</b><br /><textarea rows="7" class="form-control" name="message">'.$message.'</textarea></p>
<p><input type="button" value="Back" onclick="history.back()"> <input type="submit" name="submit" value="Submit" /></p>
<input type="hidden" name="submitted" value="TRUE" /></p>
<input type="hidden" name="id" value="'.$id.'" /></form>';
} else {
echo 'News post could not be edited, please try again.';
}
}
?>
It's because the single quotes around PHP_SELF are interrupting the single quotes in your echo. Try this instead:
<form action="' . $_SERVER['PHP_SELF'] . '" method="post">
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
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>
I'm trying to echo $url in the hidden field but I can't get it to work. I'm already inside an echo and I've tried escaping but that just gives me a parse error.
Sorry if this is a bit vague, Please can somebody help me to echo $url in that value???
echo'<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="echo $url;" readonly/>
</form>';
Thanks
1) Don't echo so much using PHP, you are literally torturing it
2) You are using ' so whatever you write between will be treated as a literal string
3) If it's so, you need to concatenate the string using a period .
4) Use double quotes " when you are using variables inside a string
?>
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="<?php echo $url; ?>" readonly/>
</form>
<?php
echo'
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="'.$url.'" readonly/>
</form>
';
This is the correct usage.
Try like this
<input type="hidden" name="current_url" value="'.$url.'" readonly/>
or simple do like this
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="<?php echo $url;?>" readonly/>
</form>
Don't echo inside echo
echo'
<form name="login" method="post" action="checklogin.php">
<input type="hidden" name="current_url" value="', $url, '" readonly/>
</form>
';
Just off topic: Why commas instead of . for concatenation.
You can't echo an echo. You're already echoing out the string so all you need to do is add your variable to it:
change
value="echo $url;"
to
value="' . $url . '"
You could simply use double quotes and let the variable expand in the string, just so:
$url = 'http://www.example.com';
echo "Hello World! This is my url $url";