JS function to php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
NewbieET
Forum Newbie
Posts: 6
Joined: Thu May 02, 2013 8:04 am

JS function to php

Post by NewbieET »

Im tryng to convert a js script validation to be a php script I need assistance

I tried using

function confirm($msg)
{
echo "<script langauge=\"javascript\">alert(\"".$msg."\");</script>";
}//end function

// to validate
function save()
{
$first_name = '';
$last_name = '';
$email = '';
$id = '';


$first_name = $_POST['last_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$id = $_POST['id'];

//Then I used this alert to validate missing fields However it not respnnding to da php script it work only with JS script below.... any assistance please

if (($first_name=='')||($last_name=='')||($email==''))
{
$msg = "Some data from the form was forgotten. Please fill in the entire form.";
confirm($msg);

}//end if

}

Code: Select all

<?php
<script type="text/javascript">
function save()
	{
		if (document.form1.first_name.value=="")
		{
			alert("Opponent's Name must be filled.");
			form1.first_name.focus();
			return false;
			
		}
		if (document.form1.last_name.value=="")
		{
			alert("last_name must be filled.");
			form1.last_name.focus();
			return false;
		}
		if (document.form1.email.value=="")
		{
			alert("email must be filled.");
			form1.email.focus();
			return false;
		}
		var confirm_del= confirm ("Do you want to save this fixture ?")
		if (confirm_del)
		{
			document.form1.action.value="Save";                              
			document.form1.action="test.php";
			document.form1.method="post";
			document.form1.submit();
		}
	}
	function editrecord(i)
	{
		if(Number(form1.rcount.value)>1)
		{
			form1.id.value=form1.hid_id[i].value;
			form1.first_name.value=form1.hid_first_name[i].value;
			form1.last_name.value=form1.hid_last_name[i].value;
			form1.email.value=form1.hid_email[i].value;
						
		}
		else
		{
			form1.id.value=form1.hid_id.value;
			form1.first_name.value=form1.hid_first_name.value;
			form1.last_name.value=form1.hid_last_name.value;
			form1.email.value=form1.hid_email.value;
		}
	}
	function del_record()
	{
		if (document.form1.id.value=="0")
		{
			alert("Nothing to delete.");
			form1.txtOppenents.focus();
			return false;
		}
		var confirm_del= confirm ("Do you want to really delete this user ?")
		if (confirm_del)
		{
			document.form1.f_action.value="delete";
			document.form1.action="test.php";
			document.form1.method="post";
			document.form1.submit();
		}
	}
</script>
</head>
<body>
<form Name="form1" action="test.php" Method="post">
<TABLE border="0" width="750" align="center">
<tr>
<td align="center">
<font face="verdana" size="2" color="#000000"><STRONG>User Details</STRONG>
</font></td>
</tr>
</table>
<table border="0" width="600" align="center" CELLPADDING="0" CELLSPACING="0">
<tr> <td>
<table border="0" width="600" align="center" CELLPADDING="0" CELLSPACING="0">
<input type="hidden" value="1" name="submit">
<input type="hidden" name="action">
<input type="hidden" name="id" value="0">
<tr bgcolor="#fff5ee">
<td height="10" colspan="6"></td>
</tr>
<tr bgcolor="#fff5ee">
<td align="right" width="40%">
<font face="verdana" size="2" color="#000000">First Name
</font></td>
<td align="center" width="5%">
<font face="verdana" size="2" color="#000000">:
</font></td>
<td align="left" width="55%">
<input type="text" size="40" class=in name="first_name"></td>
</tr>
<tr bgcolor="#fff5ee">
<td align="right" width="40%" valign="top">
<font face="verdana" size="2" color="#000000">LastName
</font></td>
<td align="center" width="5%" valign="top">
<font face="verdana" size="2" color="#000000">:
</font></td>
<td align="left" width="55%">
<input type="text" size="40" class=in name="last_name"></td>
</tr>
<tr bgcolor="#fff5ee">
<td align="right" width="40%">
<font face="verdana" size="2" color="#000000">Email
</font></td>
<td align="center" width="5%">
<font face="verdana" size="2" color="#000000">:
</font></td>
<td align="left" width="55%">
<input type="text" size="40" class=in name="email"></td>
</tr>
<tr bgcolor="#fff5ee">
<td height="10" colspan="6"></td>
</tr>
<tr bgcolor="#fff5ee">
<td colspan="4" align="center">
<input type="button" value="Add" onClick="save()">
<input type="button" value="DEL" onClick="del_record()">
</td>
</tr>
<tr bgcolor="#fff5ee">
<td height="10" colspan="6"></td>
</tr>
</table> </td>
</tr>
</table>
<TABLE border="0" width="750" align="center">
<tr>
<td align="center">
</font></td>
</tr>
</table>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: JS function to php

Post by social_experiment »

Code: Select all

<?php
// to validate
function save() 
{ 
$first_name = false; 
$last_name = false; 
$email = false; 
$id = false; 

$first_name = $_POST['last_name']; // <-- shouldn't this be $_POST['first_name'] ?
$last_name = $_POST['last_name']; 
$email = $_POST['email']; 
$id = $_POST['id']; 

 if (($first_name=='')||($last_name=='')||($email=='')) 
 { 
  $msg = "Some data from the form was forgotten. Please fill in the entire form."; 
  echo "<script type=\"text/javascript\">alert(\"".$msg."\");</script>"; 
 }
 else {
  echo "<script type=\"text/javascript\">alert('Something wrong');</script>"; 
 }
}
?>
use the script above to test; it's a good idea when testing to have a alternate condition for the if look because it helps with error checking; currently you're not sure what's happening because there isn't any feedback. Also since you're only using 1 line to create the error message you might want to create a single function or reference an external javascript file containing the code for the error message.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
NewbieET
Forum Newbie
Posts: 6
Joined: Thu May 02, 2013 8:04 am

Re: JS function to php

Post by NewbieET »

I have managed to get oit to run however I doesnt wanna update any suggestions?

Code: Select all




$sql=mysql_query("select * from user where id='$id'");
 
   while ($row=mysql_fetch_array($sql)) 
 {                                                                                                                                                                                                                                                      
$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
 	      $result=mysql_query("UPDATE user SET first_name='$first_name',last_name='$last_name',email='$email' WHERE id=$id");
 	      $sql = mysql_query($result) or die (mysql_error());
           
echo("<form name='edit' method='post' action='?action=edit'>");
echo("<input type='hidden' name='?action=edit'>");
echo("<table class=main cellspacing=0 cellpadding=5 width=50%>");
echo("<tr><td>Name: </td><td align='right'><input type='text'  name='opponents' value='$first_name'></td></tr>");
echo("<tr><td>Surname: </td><td align='right'><input type='text' name='date' value='$last_name'></td></tr>");
echo("<tr><td>Email: </td><td align='right'><input type='text'  name='venue' value='$email'></td></tr>");
echo("<tr><td></td><td><div align='right'><input type='submit'></div></td></tr>");
echo("</table>");
     }    
       }

     
NewbieET
Forum Newbie
Posts: 6
Joined: Thu May 02, 2013 8:04 am

Re: JS function to php

Post by NewbieET »

sorry the table is

Code: Select all

echo("<form name='edit' method='post' action='?action=edit'>");
echo("<input type='hidden' name='?action=edit'>");
echo("<table class=main cellspacing=0 cellpadding=5 width=50%>");
echo("<tr><td>Name: </td><td align='right'><input type='text'  name='first_name' value='$first_name'></td></tr>");
echo("<tr><td>Surname: </td><td align='right'><input type='text' name='last_name' value='$last_name'></td></tr>");
echo("<tr><td>Email: </td><td align='right'><input type='text'  name='email' value='$email'></td></tr>");
echo("<tr><td></td><td><div align='right'><input type='submit'></div></td></tr>");
echo("</table>");
Post Reply