I have following code:
$boxId = 1;
$explainationBox='<input type="button" id="<?php echo $boxId; ?>" value="send" onmousedown="javascript:callthis(<?php echo $buttonId; ?>);" class="button" />';
echo $explainationBox;
I am trying to save these values as html button inside of php variable explainationBox. But its not saving actual value of $boxId. It is just saving it as $boxId. As boxId is inside a for loop and will keep on changing. How can i do this?
You do not nedd <?php tag when this tag is already opened
Try this
$boxId = 1;
$explainationBox='<input type="button" id="'.$boxId.'" value="send"
onmousedown="javascript:callthis('.$buttonId.');" class="button" />';
echo $explainationBox;
PHP tags in a string are not parsed (unless given to some functions such as eval()).
Use string concatenation.
Change this...
"<?php echo $boxId; ?>"
...to...
"' . $boxId . '"
You may find it useful to enter and exit the php environment within the loop so you don't have so save that string as a variable at all.
for($i=1; $i<10; $i++){?>
<input type="button" id="ID<?=$i?>" value="send"
onmousedown="javascript:callthis('<?=$i?>');" class="button" />
<?php } ?>
So what we are doing is leaving the php environment as we open the loop (?>) then we give some raw html that will be plopped into the page as shown, no variable needed. Then while we are outside the php environment we use the <?= $variable ?> syntax to drop a php variable into the html language. And finally we re-enter the php environment by reopenning the php tags (<?php).
Note: That last ?> would go wherever you wanted to re-exit php again.
This code is working:
$boxId = 1;
$explainationBox='';
echo $explainationBox;
except for: "javascript:callthis('.$buttonId.');" call. In order to make this whole code work here is a solution for those who are looking:
$boxId = 1;
$explainationBox='';
echo $explainationBox;
Related
how it looksI need to set an input value in a php script
<php
<input value="<?php echo $_SESSION['logged_user']->login; ?>">
?>
As you might understand my does not work
Your PHP syntax is very confused. While inside of a <?php ?> section you're trying to directly write HTML, and trying to open another <?php ?> section.
Either get rid of the enclosing section:
<input value="<?php echo $_SESSION['logged_user']->login; ?>">
Or keep the enclosing section and use PHP code to output:
<php
echo '<input value="' . $_SESSION['logged_user']->login . '">';
?>
I have the following code, but the echo $jsonData[$i] line gives an error. I know you are not supposed to execute PHP within PHP. What is the correct way to accomplish this?
<?php
if( !is_user_logged_in() ){
echo 'Im online. Login to Chat';
} else {
echo '<input type=\"submit\" onclick=\"javascript:jqcc.cometchat.chatWith(' <?php echo $jsonData[$i]; ?> ');\" value=\"Chat Now\" class=\"success button small\"
\" >';
}
?>
You use the concatenation operator . instead of echo when you're already within the scope of a string:
echo '<input type="submit" onclick="javascript:jqcc.cometchat.chatWith(' . $jsonData[$i] . ');" value="Chat Now" class="success button small">';
Also, you don't need the backslashes unless you're using the same quotes to start and end the string. In your case you open/close with single quotes, so double quotes don't need to be escaped.
Once you're in PHP mode (i.e. reach the opening <?php tag), everything is read as code until you exit PHP mode (i.e. reach the closing ?> tag). So there's no need to reopen those tags when they've already been open. See the basic syntax section of the manual for more details about this.
One of PHP's best features is that it allows you to embed code in HTML, rather than having to embed HTML in code. It's actually much easier to read your code when you write it like this, for example...
<input type="submit" onclick="javascript:jqcc.cometchat.chatWith(<?= $jsonData[$i]; ?>);" value="Chat Now" class="success button small">
Notice we only use PHP to print the value of $jsonData[$i] where it's needed and everything else is just plain/text or HTML that gets printed.
Also, note I'm using the short-hand form of <?php echo which is just <?=.
To expand on this a little, one of the reasons this idea of embeding code in HTML is so useful, is that it allows you to easily and transparently separate code from data.
<?php
$loggedIn = is_user_logged_in();
if ($loggedIn) {
?>
<input type="submit" onclick="javascript:jqcc.cometchat.chatWith(<?= $jsonData[$i]; ?>);" value="Chat Now" class="success button small">
<?php
} else {
?>
<h1>ohnoes, you're not logged in :(</h1>
<?php
}
I wrote this code but the problem is that when I press the "Change background" button, nothing changes but I should be able to see some part of contents after I pressed it.
<form name="change "action="index.php" method="get">
<center><button type="button">REFRESH THE PAGE!!</button></center><br/>
<center><b>WELCOME NOTE!!</b></center><br/>
<center><textarea readonly="readonly" name="textarea" rows="6" cols="50" style="color:blue; font-size:15pt">Each day holds a surprise. But only if we expect it can we see, hear, or feel it when it comes to us. Let's not be afraid to receive each day's surprise, whether it comes to us as sorrow or as joy It will open a new place in our hearts, a place where we can welcome new friends and celebrate more fully our shared humanity.</textarea></center>
<br/>
<?php
mysql_connect("localhost","DB","password") or die("ERROR!!");
mysql_select_db("DB") or die("COULDN'T FIND IT!!") or die("COULDN'T FIND DB");
$sql = mysql_query("SELECT * FROM background");
$id = 'ID';
$Blue = 'blue';
$White = 'white';
$Silver = 'silver';
$Red = 'red';
$text=$_GET['textarea'];
while($rows = mysql_fetch_assoc($sql)){
if (isset( $_SESSION['CurrentUser'])){
echo '<center><button type="button" name="background">Change background</button>';
echo '<button type="button" name="color">Change font color</button>';
echo '<button type="button" name="size">Change font size</button></center><br/>';
if (isset( $_GET['background'])){
echo '<span>Choose background color</span><br/>';
echo '<img src="red.png">';
echo '<img src="white.jpg">';
echo '<img src="silver.jpg">';
echo '<img src="red.png">'; }
}
}
?>
</form>
</td></tr></table>
Program doesn't see this part;
if (isset( $_GET['background'])){
echo '<span>Choose background color</span><br/>';
echo '<img src="red.png">';
echo '<img src="white.jpg">';
echo '<img src="silver.jpg">';
echo '<img src="red.png">'; }
It doesn't work because form cannot be submited without submit button:
Replace <button type="button" with <button type="submit"
BTW use mysqli_ instead of mysql_ because it is deprecated
EDIT:
Change
if (isset( $_GET['background'])){
TO
if (isset( $_GET['colour'])){
and see what happens.
END EDIT.
Quick question...
does your link have the "background" variable defined in it?
...page.php?background=1;
If the program doesn't see that part then you need to work your way up through the conditions and see what condition it isn't meeting.
Either...
A) ?background=1 is not set in the link
B) session CurrentUser was not set
C)Your mysql is returning 0 rows
Are you getting background variable to the page. Check url once. You can debug it by using the length of variable by strlen($_GET['background']) . Also print_r($_GET) will print all the variables received by the page
Aim of my Program:
On my index.php file, an image is displayed.
I want that, when i user clicks that image, a new image should be displayed.
And when he clicks the new image, another new image should appear.
What have i done till now ?
<?php
$mycolor = array("red.jpg", "green.jpg", "blue.jpg");
$i = 0;
$cc = $mycolor[$i++];
?>
<form method="post" action="index2.php">
<input type="image" src="<?php echo $cc; ?>">
</form>
I know what the error is. Whenever, the page is reloaded, the variable $i is initialized to ZERO. How, do i fix that. How can I retain the incremented value after the image is clicked ?
Also, I have no Javascript Knowledge. So, if possible explain me in terms of php.
You have different possibilities to remember $i. e.g:
$_GET: http://php.net/manual/en/reserved.variables.get.php
Cookies: http://php.net/manual/en/function.setcookie.php
Sessions: http://php.net/manual/en/features.sessions.php
There is also no necessity to use a form for this problem. Just wrap the image with a hyperlink and modify the url by incrementing the parameter (index.php?i=1, index.php?i=2, index.php?i=3 and so on).
<?php
$mycolor = array("red.jpg", "green.jpg", "blue.jpg");
if (isset($_POST['i'])) { // Check if the form has been posted
$i = (int)$_POST['i'] + 1; // if so add 1 to it - also (see (int)) protect against code injection
} else {
$i = 0; // Otherwise set it to 0
}
$cc = $mycolor[$i]; // Self explanatory
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="image" src="<?php echo $cc; ?>">
<input type="hidden" name="i" value="<?php echo $i; ?>"> <!-- Here is where you set i for the post -->
</form>
You can either use sessions, cookies or a POST variable to keep track of the index, but some how you need to remember the last index so you can +1 it. Here's an example using another (hidden) post variable:
<?php
// list of possible colors
$mycolor = array('red.jpg', 'green.jpg', 'blue.jpg');
// if a previous index was supplied then use it and +1, otherwise
// start at 0.
$i = isset($_POST['i']) ? (int)$_POST['i'] + 1 : 0;
// reference the $mycolor using the index
// I used `% count($mycolor)` to avoid going beyond the array's
// capacity.
$cc = $mycolor[$i % count($mycolor)];
?>
<form method="POST" action="<?=$_SERVER['PHP_SELF'];?>">
<!-- Pass the current index back to the server on submit -->
<input type="hidden" name="id" value="<?=$i;?>" />
<!-- display current image -->
<input type="button" src="<?=$cc;?>" />
</form>
I need to get a value from a text box, but I'm not using a form, therefore I can't use $_POST or $_GET. Is there any method to get that value from the text box? Below is the code that I'm using
<input name="txtQty" type="text" id="txtQty" size="5" value="<?php echo $qty; ?>" onKeyUp="checkNumber(this);"> //here I'm the displaying the quantity, then the user can change it, and I need to get that value to pass it throught this link:
<input name="btnEdit" type="button" value="Edit" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=update&cid=$shoppingCartId&qty=$qty //here I need that new value from the textbox"; ?>';"
Thanks for your help
If you're doing this without a page refresh you need javascript. If you do have a page refresh, you need a form.
Without form or javascript controlled request - no you can't
Ajax must be used to Pass client data to sever if not wish to refresh the page. Check below code, which was written with simple changes.
<input name="txtQty" type="text" id="txtQty" size="5" value="<?php echo $qty; ?>">
<input name="btnEdit" type="button" value="Edit" id="btnEdit" onclick="redirectUser()" />
<script>
function redirectUser(){
var qty = document.getElementById('txtQty').value;
location.replace('<?php echo $_SERVER['PHP_SELF']; ?>action=update&cid=<?php echo $shoppingCartId; ?>&qty='+qty);
}
</script>
PHP only runs on the server, so the only way to get the values from your inputs would be to somehow pass them to the server.
You could do it in a number of ways, but the easiest would likely be to put your inputs into a form, and post the form to a PHP script.
You can use javascript:
document.getElementById("txtQty").value
Why are you saving text box box variable in php?
If you are passing value to text box in php, then you can echo php value over there.
<input name="btnEdit" type="button" value="Edit" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=update&cid=$shoppingCartId&qty="+<?php echo $qty; ?>';"
If not then you can use directly form item as value over there.
<input name="btnEdit" type="button" value="Edit" onClick="window.location.href='<?php echo $_SERVER['PHP_SELF'] . "?action=update&cid=$shoppingCartId&qty="+document.getElementById("txtQty").value';"