Using a Toggled button to set a href, using mysql and php - php

Hy all,
I am new programming on php. Basically in my school project I am sending to my navbar all the names of cities from my database as "city". Those cities are linked to a "name" that send to a respective webpage. For each city you can go for two different page by a toggle button, I mean if the button is on you go for namerent.php, if the button is off you go for name.php. As I see in my code when I click on the toggle button it is going to only one way.
<div class="navbar">
<ul>
<li class="current">Home</li>
<li><a>Location</a>
<ul>
<li><?php while($row2 = mysqli_fetch_array($result2) and $row3 = mysqli_fetch_array($result3)) {?><a href="<?php echo utf8_encode($row2["name"]);?>
Here when the togBtn changes it should set the correct end of the name of the webpage.
<?php if(isset($_POST['togBtn']) AND isset($_POST['on'])){echo "rent.php";}else{ echo ".php";}?>">
<?php echo utf8_encode($row3["city"]);?><?php }?></a></li>
</ul>
</li>
</ul>
</div>
Form for a Rounded switch
<form method="post" action="index.php">
<label class="switch">
<input type="checkbox" id="togBtn" class="togBtn" method="post">
<div class="slider round" id="btn">
<span class="on" name="on" id="on" value="on">rent</span>
<span class="off" name="off" id="off" value="off">sell</span>
</div>
</label>
</form>

If you are trying to Create A button to switch between 2 pages try this. No need for PHP with this.
Page1
<body>
<form method="post" action="page2.php">
<input type="submit" name="submit" value="rent">
</form>
sale page Content
</body>
Page2
<body>
<form method="post" action="page1.php">
<input type="submit" name="submitb" value="sale">
</form>
rent page content.
</body>

Related

Passing href link to php form

I've got a small form where I store information regarding some user choices. I'm not sure though how I can pass href links that are different for each user.
Here is a small example of the code:
<form method="post" action="save.php" class="form">
<fieldset>
<input type="checkbox" name="checkthing" value="g1">
<label>Option 1</label>
</fieldset>
<fieldset>
<input type="checkbox" name="checkthing" value="g2">
<label>Option 2</label>
</fieldset>
<fieldset>
<input type="checkbox" name="checkthing" value="e1">
<label>Option 3</label>
</fieldset>
<fieldset>
<input type="checkbox" name="checkthing" value="e2">
<label>Option 4</label>
</fieldset>
<a data-column="1" href="link here" style="color: green;">Text</a> <br>
<a data-column="2" href="link here">Text</a> <br>
<a data-column="3" href="link here" style="color: green;">Text</a> <br>
<a data-column="4" href="link here">Text</a> <br>
<button type="submit" name="save">
<i class="ft-check"></i> Save
</button>
</form>
When href got a style, it means that is customized for the user. What I want is to store the data-column ID for that user but I am not sure how I can achieve that through PHP.
So for the above example I would store the checked checkboxes + data-column 1 + 3.
You could create a hidden form element with javascript like this, and pull it off when the form is submitted as $_POST['style_ele']
$("form.form").find("a").each(function(){
if($(this).attr("style")!==undefined){
$("form.form").append("<input type='hidden' name='style_ele[]' value='"+$(this).data('column')+"'/>");
}
});
But, if you are generating that style attribute from server-side code like php, you could just create additional hidden input along with the a tag

radio button multiple selection

I have many radio buttons with different name attribute and I am facing one problem. Every time I click on each radio button, I can select all. I am using the radio buttons for redirecting to other pages.
I don't want to have multiple selection just only one. I am using twitter bootstrap tabs for showing multiple content on one page just by switching the tabs if that has something to do with the prob. Can someone help me out?
php
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['cleat']) && !empty($_POST['cleat'])) {
header("location: sporting_goods");
exit();
}
if (isset($_POST['cell']) && !empty($_POST['cell'])) {
header("location: cellphones");
exit();
}
}
?>
css
.radto{vertical-align:top;}
.sell_ali{display:inline-block;}
html
<form action="" method="post">
<div class="tabbable tabs-left" style="margin-bottom: 18px;">
<ul class="nav nav-tabs">
<li class="active">Electronics</li>
<li>Sporting Goods</li>
</ul>
<div class="tab-content" style="padding-bottom: 9px; border-bottom: 1px solid #ddd;">
<div class="tab-pane" id="tab2">
<div class="sell-ali">
<li><input type="radio" class="radto" name="camera"/> Cameras</li>
<li><input type="radio" class="radto" name="cell"/> Cell Phones</li>
<li><input type="radio" class="radto" name="cell_cover"/> Cell Phone Covers</li>
</div>
</div>
<div class="tab-pane" id="tab2">
<div class="sell-ali">
<li><input type="radio" class="radto" name="ball"> Balls</li>
<li><input type="radio" class="radto" name="cleat"> Cleats</li>
<li><input type="radio" class="radto" name="jersey"> Jersey</li>
</div>
</div>
</div>
<input type="submit" value="next" name="submit">
</form>
Give then the same name. That is how you create a radio button group.
Use the value for the piece of data that the user is picking from that group.
If you want to allow just one choice you should give them same name buddy. then set the value you want to send to server for each one.

Form Posting data

I have a 3 page registration page. After the user selects an option on the first page and clicks submit, the form transforms into the next form using jquery's animate method, meaning it stays on the same page. The question I have is how to get the data from the first form because the content of the 2nd forms is dependent on that information. Here's my html:
<div id="Registration" style="display:none;">
<div class="box">
<form id="frmtype1" action="#" name="frmtype1" method="post">
<header>Registration Options</header><br/>
<label for="Reg_type1"><input type="radio" name="Reg_type" id="Reg_type1" value="1"/> Option 1</label> <br/><br/>
<label for="Reg_type2"><input type="radio" name="Reg_type" id="Reg_type2" value="2"/> Option 2</label><br/><br/>
<label for="Reg_type3"><input type="radio" name="Reg_type" id="Reg_type3" value="3"/> Option 3</label><br/><br/>
<p id="error_message" style="display:none">Please choose an option</p><input type="submit" class="button" name="Submit" value="Submit"/>
</form>
<form name="everything" id="everything" action="#" method="post">
<header>Registration Information</header><br/>
<label>First Name<font color="red">*</font>: <input type="text" name="fname" id="fname" /> </label><br/>
Last Name*: <input type="text" name="lname" id="lname" /> <br/>
Address*: <input type="text" name="address" id="address" /> <br/>
</form>
</div>
</div>
So once an option is selected, the first form disappears and the next one appears. So how do I get the data of which option they selected? Thanks
Since they depend on information on one form, the "pages" should really be the same form. You use the jQuery/JavaScript to show/hide the "current page". This will allow you to submit all the data in one go.
Wrap all the input elements in one html form tag
For each form "segment" you will need to use your js to hide/show the wrapping HTML container. The default being "page 1" of the form and the rest hidden.
Change your submit button to just a button and have a on click event on it. When the user clicks the button the input is validated and then the jQuery unhides "page 2".
On your last "page" have a normal submit button and then all the form data is posted in one.
For example, the html might look like this:
<script type="text/javascript">
$(document).ready(function{
$(".next-page").click(function(){
$(".box-wrapper").hide();
$("#page-" + $(this).data("page")).show();
});
});
</script>
<div class="registration">
<form name="regform" action="" method="post">
<!-- Page 1 -->
<div class="box-wrapper" id="page-1">
<div class="box">
<!-- form inputs go here -->
<input type="button" name="next-page" class="next-page" value="Continue" data-page="2"/>
</div>
</div>
<!-- Page 2 -->
<div class="box-wrapper" id="page-2" style="display: none;">
<div class="box">
<!-- form inputs go here -->
<input type="button" name="next-page" class="next-page" value="Continue" data-page="3"/>
</div>
</div>
<!-- Page 3 -->
<div class="box-wrapper" id="page-3" style="display: none;">
<div class="box">
<!-- form inputs go here -->
<input type="submit" name="submit" class="submit" value="Complete Registration"/>
</div>
</div>
</form>
</div>
Using jquery --- $('input[name=Reg_type]:checked').val() this will give you the selected value.

I want to link 2 php files together by clicking a button

I have 2 php files and I want to link the two files by clicking a button .. so if I click a button in the first php file it should transfer my to the second php file ..
1st php file and here I want to click s & P Database button :
<html>
<body>
<h1> My System </h1>
<form action="">
<input type="button" value="S & P Database ">
<input type="button" value="Generate Report ">
</form>
</body>
</html>
2nd php file that I should have it when the button is clicked :
<html>
<body>
<form action="">
<h4><b>Please choose one of the following options below : </b> </h4>
<input type="radio" name="option" value="search" /> Search<br/>
<input type="radio" name="option" value="open database" /> Open Database<br/>
<input type="radio" name="option" value="administrative page "/> Administrative Page <br/>
</form>
// This button doesn't appear in the web page =( .. dunno why
<form action="">
<input type="button" value="Choose ">
</form>
</body>
</html>
If I understand correctly, you want to redirect the user to the second page when the button on the first page is clicked.
To simply redirect the user, you can use the button's onclick:
<input type="button" value="S & P Database" onclick="window.location.href='page2.php'" />
<input type="button" onClick="window.location='/2nd.php'" value="S & P Database ">​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​
This will redirect you to /2nd.php
You need to add link to php nahdler in action attribute
so it will be <form action="http://www.website.com/1.php">
Input type should be "image" or "submit", button will not submit your form.
You can also submit form by JS, but i will not describe how to do that here.
Use right document structure and doctype: http://www.rantiev.com/doctypes/
Check your code with validator: http://validator.w3.org/#validate_by_input
When some elements dissapear it can be that HTML have errors.
There are several ways to do this:
2 forms submitting to different locations
<html>
<body>
<h1> My System </h1>
<form action="page2.php" method="get">
<input type="button" name="pressed" value="S & P Database ">
</form>
<form action="page3.php" method="get">
<input type="button" name="pressed" value="Generate Report ">
</form>
</body>
</html>
2 links to different locations
<html>
<body>
<h1> My System </h1>
S & P Database
Generate Report
</body>
</html>
1 form with name and values of buttons. But your script (page2.php) will need to check the value of $_GET['pressed'] too
<html>
<body>
<h1> My System </h1>
<form action="page2.php" method="get">
<input type="button" name="pressed" value="S & P Database ">
<input type="button" name="pressed" value="Generate Report ">
</form>
</body>
</html>
BUT dont forget to urldecode($_GET['pressed'])) as the values will be encoded.
simple Put
method="post" or method="Get"
and in action type the name of 2nd file which in your case will be
action="2ndfile.php(what ever its name is)"
also in cahnage your button to submit
keep in mind that this action script is for your 1st file.

how to extract checkbox information and store it in database with codeigniter

I have this code in the php file. it lists all the interests as checkboxes and allows to input a text field "other". The user should select several of these interests and save. Now, when the user clicks on the submit button it should take it to the controller and controller should extract them and store it in the database. I have one file thats coded to do that, but its no good and too confusing.
Here is the form in the normal php file
<form method="post" action="<?php echo site_url("userProfile/update_bio");?>" name="editForm" id="edit-form">
<div> <!-- description start -->
<div> <p <p class="special-p">Name:</p> <hr> </div>
<p class="used-p"> <input type="text" name="name" value="<?php echo $this->dx_auth->get_username(); ?>"></textarea> </p> <!-- should be real name -->
</div> <!-- description end -->
<div> <!-- description start -->
<div> <p <p class="special-p">Description:</p> <hr> </div>
<p class="used-p"> <textarea name="description"></textarea> </p>
</div> <!-- description end -->
<div> <!-- interests start -->
<div> <p class="special-p">Interests:</p> <hr class="special-hr2"> </div>
<p class="used-p">I am interested in:</p>
<ul>
<?php
foreach ($allInterests->result() as $row){
echo '<li><input type="checkbox" name="checks1[]" value="'.$row->id.'"/> '.$row->name.'</li>';
}
?>
<li><input type="checkbox" name="checks1[]" value="other"/> Other: <input type="text" name="otherText"/></li>
</ul>
</div> <!-- interests start -->
<div>
<input id="editbutton" type="submit" value="Save" name="editBioButton" class="small green button"/>
</div>
</form>
The controller function is empty. The problem is I don't know how will it receive the checkboxes.
Thanx in advance
foreach($this->input->post(checks1) as $check)
{
echo $check;
}
But I believe that if the checkbox isn't checked, it won't appear in the $_POST array. To combat this, give each checkbox a specific index:
<?php
$count=0;
foreach ($allInterests->result() as $row){
echo '<li><input type="checkbox"
name="checks1[' . $count . ']" value="'.$row->id.'"/> '.$row->name.'</li>';
$count++;
}
may do what you require. Your controller would be:
foreach($this->input->post(checks1) as $key => $value)
{
echo $key . ' - ' . $value;
}

Categories