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
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;

Link to comment
Share on other sites

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 <">.

Link to comment
Share on other sites

$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">

 

 

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.