Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have a form with an input type number and a textarea, named valoare and textarea
I have 5 radiobuttons,each with name option and value 1,2,3,4,5
so lets say for first radio button I want to display the number of words from textarea.
This is the code:
if (!empty($_POST['textarea']) && !empty($_POST['valoare']))
{
$option = "option";
switch($option)
{
case 1:
if(isset($_POST['submit']))
{
$count =str_word_count($_POST['textarea']);
echo $count;
break;
}
}
}
what's wrong with this ? I don't see any result.
Thanks
That code is correct, but as you have manually set $option to the value of option, it is not 1 so the code in the case 1 statement does not run.
You should place the break; outside of the if statement though in case you add more options.
You can also add a default option at the end:
switch($option)
{
case 1:
if(isset($_POST['submit']))
{
$count =str_word_count($_POST['textarea']);
echo $count;
}
break;
default:
echo "Option is: " . $option;
}
By the way, if I understand you correctly, you probably want:
$option = $_POST['valoare'];
instead of:
$option = "option";
$option = "option";
switch($option)
{
case 1:
how can you switch option if option is already defined? if you define $option="option" $option will never be 1.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I declared an array
$CLISTS=array("Add_Product"=>"products.php","Payment_Type"=>"payment.php","Shipping"=>"shipping.php");
and I defined variables
<?php
define("Add_Product",TRUE);
define("Payment_Type",FALSE);
define("Shipping",FALSE);
foreach($CLISTS as $lists=>$page)
{
if($lists==TRUE)
{
?>
<div class='alert' style="text-decoration:line-through;"><?php echo str_replace("_"," ",$lists);?></div>
<?php }
else
{
?>
<div class='alert'><?php echo str_replace("_"," ",$lists);?></div>
<?php }
}
?>
Its not working. All the div is strikes. What I did mistake?
DEFINE does not do what you think it does. Define creates a named constant.
And you cannot change your array variables with it.
Simply do:
$CLISTS['Add_Product'] = true;
$CLISTS['Payment_Type'] = false;
$CLISTS['Shipping'] = false;
To change your array variables.
You can write the logic like
foreach($CLISTS as $lists=>$page)
{
if($lists == 'Add_Product')
{
?>
Or even you can use === like
foreach($CLISTS as $lists=>$page)
{
if($lists === TRUE)
{
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to call a function by passing argument on a HTML link in the same page
My code:
<?php
#array loop
foreach($new_array as $value){
#pass the array to the function
$input = inputFiles($value);
# call the function on the click of link
echo 'Input Link';
# output of the function in the div
echo '<div id="inputLinkOutput" ></div>';
}
function inputFiles($value){
# get the value and do the rest work
}
?>
Please help.
So you pass $_GET variable via url like that:
<!-- lcoalhost/myPage.php -->
<a href='myPage.php?var1=123&var2=abc+bcd'>Link here</a>
And in PHP:
$var1 = $_GET['var1']; // => (string) '123'
$var2 = $_GET['var2']; // => (string) 'abc bcd'
You need to use something like Ajax to achieve this. Here how you do this.
Create a ajax function which calls a method in a php file. Along with the call pass a parameter with a value. Because this will help in the server side to execute which function.
Then inside the PHP file create set of functions which you think will execute. (PHP function has been listed below)
Introduce a switch statement to navigate to specific function.
$param = $_GET['val'];
switch($param){
case '1' : methodA(); break;
case '2' : methodB(); break;
//and so on
}
methodA(){
//do some logic
}
methodB(){
//do some logic
}
Hope this helps.
Cheers!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
This is more of a hypothetical, since I just want to use the most efficient way to go about this.
Say you have a page with over 100 textboxes, and a submit button that bring you to a next page and displays what you've written.
How would I go about only printing the textboxes that have been filled in?
I would know how to do this with a lot of if statements, or a very long switch statement, but perhaps there's a simpler way?
Thank you!
yes, there is.
simply call them numbering them.
for example you'll have textbox1, textbox2, textbox3...
in this way, on the next page you just have to build a for loop.
for ($index = 1; $index < 100; $index++) {
if (!empty($_REQUEST["textbox" . $index]))
{
echo "textbox number {$index} isn't empty!";
}
}
in this way you will get each box is full or not.
Something like
<?php
for($i=0; $i<100; $i++){
if(!empty($_POST['textbox'.$i]))
echo $_POST['textbox'.$i].'<br>';
}
?>
should work I guess.
I did not test it
if the name itself is not important i would give them the name txtbox[] (name="txtbox[]")
This way you can choose one name for all the boxes that belong together, have a specific array to loop through $_POST['txtbox'] . Then just echo them all
foreach($_POST['txtbox'] as $key => $tb) {
echo "<br>Box " . $key . ": " . $tb;
}
Give them names like txtbox_1 to txtbox_100
foreach ($_POST as $key => $value) {
if (is_integer(strpos($key, 'txtbox_')) && $value != '') {
echo "<br />$key = $value ";
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Hi I'm trying to change the picture for facebook thumbnail by having php set the image in the meta tag depending on the page. I have this code but for some reason when I go to Facebook and debug it it actually shows the variable and not what the value of the variable is. Here is my code please help thank you!
<?php
if($ifpage == 'picture.php')
{
$metaimage = '<meta property="og:image" content="http://www.quickdailylaugh.com/photos/print $picture" />';
print($metaimage);
}
?>
Your variable is inside of a string enclosed with single quotes. Single quotes will take the literal value of the string $varname and not translate the variable to it's value. You need to use double quotes. Example:
$var1 = 'test';
$foo = 'The value of var1 is: $var1'; // The value of var1 is: $var1
$bar = "The value of var1 is: $var1"; // The value of var1 is: test
To interpret the varible you must use double quotes.
<?php
if($ifpage == 'picture.php')
{
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/print $picture\" />";
print($metaimage);
}
?>
I had to do another query for it to show... Thank you all for the answers here is what i used...
<?php
if($ifpage == 'picture.php')
{
$photoid = $_GET['picture'];
$sql = "SELECT * FROM photos WHERE id=$photoid";
$pic = mysql_query($sql);
while($row = mysql_fetch_array($pic))
{
$picture=$row['picture'];
}
$metaimage = "<meta property=\"og:image\" content=\"http://www.quickdailylaugh.com/photos/$picture\" />";
print($metaimage);
}else{
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a special form I have been making that uses some cusotm post types in wordpress. At one point I need to echo a variable $i into an if statement.
There is some validation stuff at the top that will look like this and the code in the loop is below. Pretty much I have been trying to get the majorCause1Error to be majorCause $i Error if you know what I mean, so all up it will be like 1-13
Edit: Sorry If it is hard to see what I am asking, I am finding it really hard to word my problem.
So there is a loop running around the li tags and it echos $i into the name etc so it becomes majorCause1 then next one majorCause2 and the next one magjorCause3 etc etc
Under the labels there is an if statement that is like - if($majorCause1Error !='') { do something } - I want this to be like if($majorCause1Error !=''){} and then the next one be like if($majorCause2Error !=''){} and then if($majorCause3Error !=''){}
Does this make more sense?
Here is a link to the site http://www.foresightaus.com.au/form/
if(trim($_POST['majorCause1']) === '') {
$majorCause1Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause1 = trim($_POST['majorCause1']);
}
if(trim($_POST['majorCause2']) === '') {
$majorCause2Error = "Please enter a major cause.";
$hasError = true;
} else {
$majorCause2 = trim($_POST['majorCause2']);
}
<li class="fill-in">
<label for="majorCause<?php echo($i); ?>"><?php echo($j); ?>. State one major cause:</label>
<input type="text" name="majorCause<?php echo($i); ?>" id="majorCause<?php echo($i); ?>" value=""/>
<?php if($majorCause1Error != '') { ?>
<span class="error"><?=$majorCause1Error;?></span>
<?php } ?>
</li>
You probably want to be using an array but what you are referencing is called a variable variable and is supported by PHP!
Something like this should do it
${"majorCause{$i}Error"}