Jump to content

[SOLVED] Setting a Variable


rich11

Recommended Posts

Hello,

 

I was wondering how I set variable with two variables coming from a form and I would like to seperate them with a comma.

 

I understand how to set a variable.

$totaltext = "hello my friend!" ;

 

I want to take these two variables from a form and put them together in one variable seperated by a comma

echo $_POST["C1"];

echo $_POST["C2"];

 

 

$totaltext = echo $_POST["C1"] "," echo $_POST["C2"];

 

I know that is not the syntax, but I tried it and it didn;'t work.

 

Thx,

 

Rich

Link to comment
https://forums.phpfreaks.com/topic/74488-solved-setting-a-variable/
Share on other sites

Hello,

 

I was wondering how I set variable with two variables coming from a form and I would like to seperate them with a comma.

 

I understand how to set a variable.

$totaltext = "hello my friend!" ;

 

I want to take these two variables from a form and put them together in one variable seperated by a comma

echo $_POST["C1"];

echo $_POST["C2"];

 

 

$totaltext = echo $_POST["C1"] "," echo $_POST["C2"];

 

I know that is not the syntax, but I tried it and it didn;'t work.

 

thanks,

 

Rich

 

$totaltext = $_POST["C1"].",".$_POST["C2"];

echo $totaltext;

ok here's some tips:

 

shortest way to get what you want is:

$myvar = "$_POST[C1], $_POST[C2]";

 

you can do that because you can insert variables inside strings. Like:

 

$n = 5;

echo "n = $n"; // this would print "n = 5"

 

now, with arrays it's the same, just that you MUST NOT put the <"> because it won't work

 

$fullname = array("first" => "Bill", "second" => "Gates");

echo "My name is $fullname[first] $fullname[second]"; // prints "My name is Bill Gates"

 

 

tips:

when using strings you should be aware of the difference between <"> and <'>. The <"> is used for string that contain variables or escaped characters, like: "Today is $date. Have a nice day.\n". Notice the variable $date and the escaped character \n at the end, which is called the "new line character".

 

If you use <'> then the variables and escaped characters are not inserted. This is faster that <"> and usefull if you use \ a lot.

 

So you should generally use <'> unless you need to use variables inside in which case you should use <">.

$checkboxes = $_POST['checkboxes'];

if(count($checkboxes) >= 2){
//allow
}else {
echo "you must select 2 or more to continue";
}

 

<input type="checkbox" name="checkboxes[]" value="1">
<input type="checkbox" name="checkboxes[]" value="2">
<input type="checkbox" name="checkboxes[]" value="3">
<input type="checkbox" name="checkboxes[]" value="4">

 

 

<SCRIPT LANGUAGE="JavaScript">
function anyCheck(form) {
var total = 0;
var max = form.checkboxes.length;
for (var idx = 0; idx < max; idx++) {
if (eval("document.form.checkboxes[" + idx + "].checked") == true) {
    total += 1;
   }
}
if(total >= 2) {
return TRUE;
}else {
alert("Please select atleast two boxes")}
}
}
</script>

<form method="post" name="test" onSubmit="anyCheck(this.form)">
1<input type=checkbox name="checkboxes[]">
<br>2<input type=checkbox name="checkboxes[]">
<br>3<input type=checkbox name="checkboxes[]">
<br>4<input type=checkbox name="checkboxes[]">
<br>5<input type=checkbox name="checkboxes[]">
<br>6<input type=checkbox name="checkboxes[]">
<br>7<input type=checkbox name="checkboxes[]">
<br>8<input type=checkbox name="checkboxes[]">
<br>9<input type=checkbox name="checkboxes[]">
<input type="submit">
</form>

 

you could try that.

http://javascript.internet.com/forms/checkbox-counter.html

For some reason.. it is not picking up the javascript.  It just goes to the next page.

 

Here is the code for the start of my form.  Do you see any mistakes??

 

<form method="post"  name="form" action="eerasinsert.php" onSubmit="anyCheck(this.form)">

 

 

 

Rich

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.